I need help with communication between the roborio and an arduino board. Basically when my robot enters disabled mode, it calls the constructor of an arduino class which basically sends an I2C message.
package main.java.frc.utils;
import edu.wpi.first.wpilibj.I2C;
public class ArduinoI2C {
public ArduinoI2C() {
I2C arduino = new I2C(I2C.Port.kOnboard, 4);
String WriteString = "test"; // "test" will be the message we will send to the arduino
char[] CharArray = WriteString.toCharArray(); //Turns the message into an array
byte[] WriteData = new byte[CharArray.length]; //Turns the array into a byte
for (int i = 0; i < CharArray.length; i++) {
WriteData[i] = (byte) CharArray[i];
}
arduino.transaction(WriteData, WriteData.length, null, 0); //Send the message "test" to the Arduino
};
}
And when the Arduino receives the message, an LED lights up on the Arduino
#include <Wire.h>
#define LED_PIN 9
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
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 == "test")
{
for(int x = 0; x<10; x++) {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}
}
}
When I tested this, nothing happened. Can anyone tell me why? Any recommendations? something wrong with my code? (Note: I am not good with coding )**