Go Down

Topic: Making Two Arduinos Talk (Read 671 times) previous topic - next topic

grindylow

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:
Code: [Select]
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:
Code: [Select]
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

MarkT

The second read in receiveData() may fail.  You need to check that enough bytes are ready before reading I think.

Code: [Select]
if (Serial.available() >= 2)
[ I won't respond to messages, use the forum please ]

grindylow

Quote
if (Serial.available() >= 2)

Nothing really improved just made the blink faster.

I made some corrections in sendData() (but it still doesn't solve the problem) here's the updated one:
Code: [Select]
void sendData()
{
 for(int y=0; y<6; y++)
 {
   serial_dataH = (unsigned char)(readings[y] >> 8);
   serial_dataL = (unsigned char)readings[y];
   Serial.write(serial_dataH);    
   Serial.write(serial_dataL);
   y++;
 }
}


I wonder what order is serial data sent and received

grindylow

Ok, I've worked it out now. Here's the updated code for those encountering the same problem:
Code: [Select]
void sendData()
{
 int x=0;
 for(int y=0; y<=7; y+=2)
 {
   serial_data[y] = (unsigned char)(readings[x] >> 8);
   serial_data[y+1] = (unsigned char)readings[x];
   x++;
 }
 serial_data[8] = readings[4];
 serial_data[9] = readings[5];
 
 for(x=0;x<=9;x++)
   Serial.write(serial_data[x]);
}


Code: [Select]
void receiveData()
{
 int x = 0;
 int y = 0;
 if( Serial.available() >= 10 )
 {
   for(x=0;x<=9;x++)
     inData[x] = Serial.read();
     
   for(x=0;x<=7;x+=2) {
     controlData[y] = (inData[x] << 8) + inData[x+1];
     y++;
   }
   controlData[4] = inData[8];
   controlData[5] = inData[9];
 }
 
 if(x != 0) processMotors();
}


Go Up