Reading Analog Sensors Over Serial

Hello,

I am trying to send three analog values over a serial connection from one arduino uno to an arduino duemilanove. I have 3 10k pots connected to three analog inputs of the UNO using ground and 3.3V. Then on the DUEMILANOVE I have the PMW outupts conneccted to three leds. Eventually I will send these values using an XBEE but for right now I have the RX pin of the UNO to the TX Pin of the DUEMILANOVE and the TX pin of the UNO to the RX Pin of the DUEMILANOVE... My codes works but a minute in one of my LEDS will continue to blink even when it should be off.

The code for the uno is

int analogValue5, val5; int analogValue2, val2; int analogValue0, val0;

void setup() { Serial.begin(115200); }

void loop() { analogValue5 = analogRead(5); analogValue2 = analogRead(2); analogValue0 = analogRead(0);

val5 = map(analogValue5, 0, 1023, 0, 255); val2 = map(analogValue2, 0 ,1023, 0, 255); val0 = map(analogValue0, 0, 1023, 0, 255);

Serial.write('A'); Serial.write(val0); delay(10);

Serial.write('d'); Serial.write(val2); delay(10);

Serial.write('C'); Serial.write(val5); delay(10); }

The code for DUEMILANOVE

byte incomingByte; byte sensor1, sensor2; byte sensor3;

void setup() { Serial.begin(115200);

pinMode ( 9, OUTPUT); pinMode (10, OUTPUT); pinMode ( 6, OUTPUT); }

void loop() { if (Serial.available()) { incomingByte = Serial.read();

Serial.print(int(incomingByte));

if ((int(incomingByte) == 'C')) { sensor1 = Serial.read(); Serial.print(int(sensor1)); delay(10); }

if ((int(incomingByte) == 'd')) { sensor2 = Serial.read(); Serial.print(int(sensor2)); delay(10);

} if ((int(incomingByte) == 'A')) { sensor3 = Serial.read(); Serial.print(int(sensor3)); delay(10);

} }

analogWrite( 9, sensor1); analogWrite( 10, sensor2); analogWrite( 6, sensor3); // delay(10); }

Thnks for the help in advance.

Have you connected the grounds of the Arduinos? Every signal needs a reference to be recognized.

Yes I did connect them to ground.

It would help if you explained which LED behaves incorrectly, and what the inputs are leading up to it.

Anyway, it looks like you read the second byte from the serial port without checking to see if there is a byte available -- a risky practice. This is the sort of thing that can easily be masked by the time it takes to print debug values. Perhaps only occasionally it is getting to the second serial read too fast, and getting a -1 as the return value?

Does it work more reliably if you test to see if Serial.available() >= 2 in loop()?

-br

After trying to do some troubleshooting i believe the problem comes from the code on the Arduino UNO.

Where the code says:

Serial.write('A'); Serial.write(val0); delay(10);

Serial.write('d'); Serial.write(val2); delay(10);

** Serial.write('C');** Serial.write(val5); delay(10); }

whenever I switch the char for the identifier in the last Serial.write where it is bolded the led connected to that identifier blinks in a sequence when it is suppose to be off.

billroy, I tried your suggestion and it did not change what the arduino did. Thanks for help.

Think the first thing I should do is remove the delay(10) at the receiving end. Furthermore you shoud check if there is a byte before you read over serial. You do that for the first byte (ABC) but not for the value. As the DUE is faster it might disrupt the synchronization between the 2.

easiest way to fix ...

void loop() {
  if (Serial.available() >=2)
  {
    incomingByte = Serial.read();
    
    Serial.print(int(incomingByte));

    if ((int(incomingByte) == 'C'))
    {
      sensor1 = Serial.read();
      Serial.print(int(sensor1));
      delay(10);
  }
  ...etc