My team got the Arduino communicating with the roborio over I2C using the following code on the roborio
Code:
static I2C Wire = new I2C(Port.kOnboard, 4);
if (Global.driver.Buttons.Back.changedDown) {
String WriteString = "go";
char[] CharArray = WriteString.toCharArray();
byte[] WriteData = new byte[CharArray.length];
for (int i = 0; i < CharArray.length; i++) {
WriteData[i] = (byte) CharArray[i];
}
Wire.transaction(WriteData, WriteData.length, null, 0);
}
And this code on the Arduino
Code:
#include <Wire.h>
void setup()
{
pinMode (13, OUTPUT);
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
}
void loop()
{
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
String LED = "";
while ( Wire.available() > 0 )
{
char n=(char)Wire.read();
if(((int)n)>((int)(' ')))
LED += n;
}
if (LED == "go")
{
digitalWrite (13, HIGH);
}
}