Adding an Arduino requires code on both the roborio side and the Arduinio side to handle communication between the two. Communication can be over the Serial Port, I2C port, or Digital In/Out pins. The simplest is with Digital In/Out.
1. Roborio side code using Digital output:
Create a class to drive the Arduino. We are converting the integer to its binary representation so we can send it over the 3 digital output pins. This limits us to 2^3, or 8, values. Add more pins for higher values of i.
package frc.robot;
import edu.wpi.first.wpilibj.DigitalOutput;;
public class LEDdriver{
DigitalOutput do1 = new DigitalOutput(1);
DigitalOutput do2 = new DigitalOutput(2);
DigitalOutput do3 = new DigitalOutput(3);
public LEDdriver(){
}
// split an integer i into individual bits and send to the Arduino
// maximum i = 2^n - 1
public void sendData(int i){
boolean b1=false,b2=false,b3=false;
do1.set( ((i>>2)&1)==1 );
do2.set( ((i>>1)&1)==1);
do3.set(i&1 == 1);
}
b) Wherever you want to call this code from, you need to create an instance and then call it with the appropriate input:
LEDDriver led = new LEDdriver;
'''
led.sendData(4);
2. Arduino Side Code
a) Read the input into an integerY. ou can use whatever digital input pins that are convenient.
int inPin1 = 7;
int inPin2 = 8;
int inPin3 = 9;
int val = 0; // variable to store the read value
void setup() {
pinMode(inPin1, INPUT);
pinMode(inPin2, INPUT);
pinMode(inPin, INPUT);
}
void loop() {
// read the input pins and convert to integer
val = 4*digitalRead(inPin3)+2*digitalRead(inPin2)+digitalRead(inPin1);
digitalWrite(ledPin, val); // sets the LED to the button's value
}
b) from here you need to use that value to set the state of the flashing LED pattern. One possibly method is to look for a change in the value being sent.
3. Wiring
Using PWM's , wire the 3 digital IO pins on the roborio to the corresponding digital IO pins on the arduino. You can use 22 or 24 gauge wire. These are all signal wires, so don't use black or red.