serial.read

Hello,

I have added a simple serail sender and serial receive program.

When I receive serial data the val is set to zero.

if the ledPin is on, it will still be turned off during the serial data input.

The queston is: like a tip how to solve this problem

// Sender

const int potPin = A1;

unsigned int val = 0;

void setup() {

Serial2.begin(38400);
}

void loop() {
val = map ( analogRead(potPin),0,1023,0,255);

Serial2.write(highByte( val));
Serial2.write(lowByte(val));
delay(5);

}
// Receiver
const int ledPin = 42;
unsigned int highByte, lowByte;
int val = 0; 
boolean newData = false;

//byte h = Serial.read();
//  highByte = Serial.read();

//int l = 0;

void setup() {
Serial.begin(38400);
pinMode(ledPin, OUTPUT);
}

void loop() {

if (Serial.available()){
   val = Serial.read();
   newData = true;
  }
 
if (newData == true ){
    if (val >= 100){
         digitalWrite( ledPin, HIGH);
        newData = false;
     }
     else
    {
      digitalWrite( ledPin, LOW);
      newData = false;
    }
   }
 }

Where is your wiring schematic ?, are you using 2 arduino DUE boards ?

Yes I use two arduino boards ( from Arduino DUE board to Arduino DUE board )

Connection

Sender TX2 to Receiver RX0
,, RX2 to ,, TX0
,, Gnd to ,, Gnd

It won't work, you have to use Serial (RX/TX) to print on Serial Monitor and e.g. Serial1 for data exchange with Serial2 of the second board.

Ok, I changed port0 to port1 now it works. ( I had not thought here for a moment )

Thanks for the information.

Hello,

I still have a question if I removed IC10 from the arduino DUE slave and use RX0 and TX0 is it then possible to communicate between the Arduino due's. ( for master I want use RX2 and TX2 )

Keep RX (RX0) and TX(TX0) to communicate with the serial monitor.

You have Serial1 (RX1/TX1), Serial2(RX2/TX2), Serial3(RX3/TX3) and Serial4 (with 3 more lines of code) as hadware serial.

Ok thanks again for te information.