I am currently trying to drive LED Strips using data from the rio. I am having issues with code, and I also want to know what the best method would be for comms between the two devices. I tried serial over USB but I couldn’t get a response from the arduino, I can post both sections of code if wanted, and I am currently trying to see how I2C would work.
Anyone have wisdom concerning this topic? I am using addressable LEDs right now, so that makes coding it a little different.
Recently, I was tasked with the same problem. I tried USB Serial comm to see how reliable it is, and it actually worked pretty well. Below is the code I used. There may be an advantage to I2C that I’m not aware of, but we’ve used Serial for two way communication and it worked for us great.
private SerialPort arduino;
try {
arduino = new SerialPort(9600, SerialPort.Port.kUSB);
arduino.setTimeout(0.001);
}
catch (UncleanStatusException e){
Logger.log("ERROR: Unable to connect to Arduino");
}
arduino.writeString("C");
String read = arduino.readString(3);
The problem may be not in the Serial communication itself but rather in the way you’re storing/interpreting the data. To isolate the USB serial variable, I recommend you try the following:
Simplify the roborio code to just have it send over one of two simple things (i.e. either ‘a’ or ‘b’) in a loop. You should be able to control what it sends through a joystick or something simple.
Simplify the arduino code so that all it’s doing is reading from Serial and acting accordingly in a way you can spot (i.e. if ‘a’ is received then turn on an LED, if ‘b’ is receieved then turn it off).
Once you run this experiment, you should have further insight as to whether the problem is in the communication path or just the surrounding code (in which case you should debug each variable to make sure it’s what you want it to be).
If you still are running into issues and want to give I2C a try, we may be able to help. We are using I2C to pass pixy data from the Arduino to the RoboRIO and it’s been working pretty well. We have a GitHub repo that is just code to talk between the RoboRIO and Arduino, with a few pixy lines in there. The only thing is we never actually had a need to write data to the Arduino but the functions are there and should work. They’ve just never been tested.
Personally, I prefer to use the Analog Out function of the RoboRIO (hidden in the MXP). It interfaces with the Arduino extremely well and doesn’t require the complexity of the SPI port or proper Serial. Additionally, It can be set up in code as easily as
AnalogOut analogOut = new AnalogOut(0);
analogOut.setVoltage( Voltage between 0 and 5 );
Just to verify - are you using a Leonardo or other ATmega 32U4 Arduino? Most non 32U4 boards have Serial mapped to communication over a pair of digital pins.
The last time my team did this we had trouble using i2c and didn’t have the time to figure out the issue (it was right before a regional). With all of the DIO and PWM ports on our RoboRio already taken up for sensors and ESCs, we found out you could use the relay ports (roborio) to send signals to the DIO ports (arduino) with essentially 4 bits of data (4 ports). This allowed us to run 16 different modes on the LEDs that year and provided a quick and easy fix.
I am pretty sureSerial.println() on the Uno will push data out to the Serial-USB converter as well as the hardware serial on 0/1. In other words, I think ports 0 and 1 are connected directly to the Serial-USB converter. If you need additional Serial ports, they can be simulated with SoftwareSerial.