Hello people! I have a problem regarding serial communication between arduinos. I want to control servo motors from the receiving end and then get data from nunchuck from the transmitting end. Prior to this, I successfully controlled LED from RX end with a pushbutton at TX end. But now, I'm sending multiple bytes of data.
here is the TX code:
void sendData()
{
int y = 0;
for(int x=0; x<11; x+=2)
{
serial_data[x] = (unsigned char)readings[y];
Serial.write(serial_data[x]);
serial_data[x+1] = (unsigned char)(readings[y] >> 8);
Serial.write(serial_data[x+1]);
y++;
}
}
here is RX code:
void setup()
{
Serial.begin(9600);
servo.attach(12);
pinMode(ledPin, OUTPUT);
pinMode(ledPin, OUTPUT);
for(int j=0; j<=6; j++) {
inData[j] = 0;
lastIn[j] = 0;
}
delay(1000);
}
void loop()
{
receiveData();
processMotors();
delay(10);
}
void receiveData()
{
int x = 0;
while( x < 6 )
{
if(Serial.available() > 0)
{
inData[x] = Serial.read();
inData[x] = (inData[x] << 8) + Serial.read();
x++;
}
}
}
void processMotors()
{
if( inData[4] != lastIn[4] )
{
if( inData[4] == 1 )
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
lastIn[4] = inData[4];
}
if( inData[5] != lastIn[5] )
{
if( inData[5] == 1 )
digitalWrite(ledPin0, HIGH);
else
digitalWrite(ledPin0, LOW);
}
}
what I get when pressing Z button is the LED for C button blink fast