Serial Communication

Need to send one char from one Arduino to another. My current code can successfully transfer the char and display it on the receiving Arduino, however the message keeps getting printed every loop. Looking for a simple way to have one message displayed on the receiving Arduino. So far I've tried Serial.flush(), and writing a new value to the variable outside of the range of incoming messages. Ideally, i would prefer to set Serial.Available = or < 0 so the system will only act if there is a new incoming message waiting to be read. Any help would be appreciated. Code is listed below.

Transmitting Code

void setup() {
  Serial.begin(9600);
  delay(1000);
  Serial.write("z");
}

void loop() {
}

Recieving Code

char message;
void setup() {
  Serial.begin(9600);
}
void loop() {
  if (Serial.available() > 0) {
    message = Serial.read();
    Serial.println(message);
    Serial.println("DONE");
    Serial.flush();
  }
}

Serial.read() removes the character from the buffer...
Such that Serial.available() will return 0 when there are no more characters.

So you are saying the receiver prints more than one “z”?

Your transmitter must be rebooting, or you are not running the code you posted, or something else is wrong.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

And you in fact find that communication between two Arduinos is easier using I2C. See this Arduino to Arduino I2C Tutorial

...R

I am getting z only once!
smdf.png

smdf.png

Think i just figured out the problem! It had nothing to do with the code. I had the tx tied to the rx and vice versa going to each board along with a common ground. Since the code would print out the char value after 1 second i removed the USB cable from the transmitter and plugged in the corresponding TX / RX cable into it. Since the board was getting power, i assumed nothing of it. By plugging in an external power source into the transmitter along with the TX RX GND leads, the code is working fine now with no repeating values. Thanks for the insight!

cjr7315:
Think i just figured out the problem! It had nothing to do with the code. I had the tx tied to the rx and vice versa going to each board along with a common ground. Since the code would print out the char value after 1 second i removed the USB cable from the transmitter and plugged in the corresponding TX / RX cable into it. Since the board was getting power, i assumed nothing of it. By plugging in an external power source into the transmitter along with the TX RX GND leads, the code is working fine now with no repeating values. Thanks for the insight!

Not convincing arguments!