Serial comm. problem between two Arduinos

I'm trying to send 2 different numbers from 1 arduino to another. The code is working fine when sending 1 digit number but when sending 2 or 3 digit number the numbers are received correctly but the communication just hangs after seconds. Is this because I send much faster than I read so the serial buffer over flows? I don't know but I think this should be easy :) Thanks in advance ;) Transmitter

void setup() {
  Serial.begin(9600) ; // open the serial port at 9600 bps:   

}

void loop() {  
  
Serial.write('Q'); \\ header char
Serial.write(112); \\any byte number that will be received from a potentiometer
Serial.write('Z'); \\ end char

Serial.write('S'); \\ header char
Serial.write(222);  \\any byte number that will be received from a potentiometer
Serial.write('J');  \\ end char

}

Receiver

char x;
char y;
char e;
char w;
byte num;
byte num1;
void setup() {
  Serial.begin(9600);   // open the serial port at 9600 bps:    
   
}

void loop() {  

if(Serial.available()>6)
{

  x=Serial.read();
  if(x=='Q'){
    
    num=byte(Serial.read());
    y=Serial.read();
    if(y=='Z')
    {
   Serial.println(num);
  
    }
  }
    
   
     e=Serial.read();
if(e=='S'){
    num1=byte(Serial.read());
    w=Serial.read();
    if(w=='J')
    {
   Serial.println(num1);
   
    }
    }
    
  }

}

The sender is sending 6 bytes, over and over again, as fast as possible. Why?

The receiver is waiting until there are more than 6 bytes before it reads 6 bytes. Why? Why is it not waiting for 6 or more bytes?

When you Serial.print() on the receiver, where are you expecting that data to go? The device on the other end isn't reading anything.

The sender is sending 6 bytes, over and over again, as fast as possible. Why?

because this what should happen when the arduino receives the analogue input from 2 potentiometers & send the values to the receiver to analogewrite the values PWM. should I delay between them? I was just testing the output on serial monitor.

The receiver is waiting until there are more than 6 bytes before it reads 6 bytes. Why? Why is it not waiting for 6 or more bytes?

what would be the difference? 1 more byte?

When you Serial.print() on the receiver, where are you expecting that data to go? The device on the other end isn’t reading anything.

Serial monitor through USB cable connected to the receiver arduino.

please note that this code works perfect when the numbers is 1 & 2 for example. weird :~

How do you have the receiving arduino connected to both the sending arduino and to a monitor?

Try a simpler experiment, on the receiver just do 6 serial reads and monitor the output, see if all 6 bytes are actually making it over.

I would expect some interference with the sending arduino and the serial monitor as described both connected to the receiver's Rx line.

I would definitely add a delay on the sender. It’s sending them very fast you will build up a back log. I sent an analog number over xbee’s and used 200 milliseconds delay and got pretty good reaction time from a joystick to a motor.

How do you have the receiving arduino connected to both the sending arduino and to a monitor?

Try a simpler experiment, on the receiver just do 6 serial reads and monitor the output, see if all 6 bytes are actually making it over.

I would expect some interference with the sending arduino and the serial monitor as described both connected to the receiver's Rx line.

I always do that with no problem I think. I understand what u mean but if I do 6 reads there would be no problem & this is not what I want. All I want to connect 2 potentiometers to the transmitters arduino and send their values to the receiver to drive a motor. I don't know why this could happen with 2 or 3 digit numbers only and why it hangs also. Is there's any other way I can send the data by?

I would definitely add a delay on the sender. It's sending them very fast you will build up a back log. I sent an analog number over xbee's and used 200 milliseconds delay and got pretty good reaction time from a joystick to a motor.

OK I will try this but the problem is that it works well with single digit numbers so why this is happening?!!

When it hangs & I reset the transmitter 3 or 4 times it sends the data say for 3 seconds then 2 seconds then 1 second then it stops even if I reset it again.

May be it doesn't hang but the transmitter stops sending data & TX led turns off :roll_eyes:

This is my code for using the up down of a analog joystick to change brightness of LED. I see how you want to use two numbers and thats my next step so let me know how you resolved it.

Sender:

int UD = 0;
int LR = 0;

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

void loop() {
  UD = analogRead(0);
  LR = analogRead(1);
 // Serial.print("UD = ");
 // Serial.print(UD, DEC);
 // Serial.print(", LR = ");
 // Serial.println(LR, DEC);
 int value = map(UD,0,1023,0,255);
 Serial.write(UD);
 delay(200);
}

Receiver:

#include <Servo.h>
#include <LiquidCrystal.h>
int LED = 9;
LiquidCrystal lcd(12,11,5,4,3,2);

void setup()
{
  lcd.begin(16,2);
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}
void loop()
{
  lcd.clear();
  while(Serial.available() == 0);
  int data = Serial.read();
  analogWrite(LED,data);
  lcd.setCursor(0,1);
  lcd.print(data);
  Serial.flush();
  delay(1000);
}

I used an LCD to ouput the numbers so i could run them without the computer hooked up to environment.

I did this before & it worked pretty well my code was almost like yours but when sending 2 numbers it's just totally different :roll_eyes: I will keep you updated ;)

Solved. I just delayed between the transmitted data. Thanks to every one

Is it normal for the ATMega of the transmitter arduino to warm up little high but works fine?

That's usually a sign that either an IO pin has been damaged, or you have a lot of current going thru the chip for other reasons.

Today my mega 2560 was gone. It powers on normally but it doesn't accept any uploading. Tried all solutions but in vain. Uploading just times out

Does the loopback test work?

http://arduino.cc/forum/index.php/topic,73748.0.html

I tried it now. No it's not working.... the computer sees the board & everything but no echo for anything. note: I have another mega 2560 which is working fine on the same computer but I didn't try this test for it & I'm not going to try it :stuck_out_tongue_closed_eyes:

What could be the reason for a damaged I/O pin & this heating issue?

Usually too much current flow from the pin, as in driving an LED with no current limit resistor. Also touching a pin to 12V accidentally would do it.