You are on page 1of 31

Hello Arduino Community,

Currently I'm working on a Geocache cube. there is a (capacitive)


touchpad made from € coins. When a coin lights up you need to touch it.
Now another coins lights up and you touch it,and another etc etc.. if you are
fast enough and finish all the rounds the cube opens (servo lever touches
open mechanism). when you are to slow the servo touches a mechanical
endswitch and cuts the power and you have to start over from round 1.

I'm using two arduino nano's that are communicate via the serial port
(TX>RX and RX>TX).

the first nano is connected to the touchpad and sends characters


A,B,C,D,E,F,G,H,I to the second nano. A when led/coin 1 is touched, B
when led/coin 2 is touched etc. (see first sketch)

the second nano processes the input from nano 1 and also controls the
LED's and Servo. (see second sketch)

everything went well but now I got stuck on the following:

In round 1 the characters A,B,C are sent to the second nano. When round
2 starts the last character send is still in the serial buffer from the second
nano (see screenshot serial monitor nano 2).

so the statement below kicks in, the servo cuts the power and the game
ends without a mistake being made.

if (incomingByte == 'A' || incomingByte == 'B' || incomingByte == 'C' ||


incomingByte == 'D' || incomingByte == 'E' || incomingByte == 'F')

{
Serial.print(incomingByte);
servo1.write(20);
}

is there a way to clear the serial buffer after round one? ore does someone
know another smart solutions to make this game work?

MB
Sketch 1#

Code: [Select]
#include <CapacitiveSensor.h>

CapacitiveSensor cs_2_3 = CapacitiveSensor(2,3);


CapacitiveSensor cs_2_4 = CapacitiveSensor(2,4);
CapacitiveSensor cs_2_5 = CapacitiveSensor(2,5);
CapacitiveSensor cs_2_6 = CapacitiveSensor(2,6);
CapacitiveSensor cs_2_7 = CapacitiveSensor(2,7);
CapacitiveSensor cs_2_8 = CapacitiveSensor(2,8);
CapacitiveSensor cs_2_9= CapacitiveSensor(2,9);
CapacitiveSensor cs_2_10= CapacitiveSensor(2,10);
CapacitiveSensor cs_2_11= CapacitiveSensor(2,11);

void setup()
{
cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF);
Serial.begin(9600);
}

void loop()
{
long start = millis();
long total1 = cs_2_3.capacitiveSensor(30);
long total2 = cs_2_4.capacitiveSensor(30);
long total3 = cs_2_5.capacitiveSensor(30);
long total4 = cs_2_6.capacitiveSensor(30);
long total5 = cs_2_7.capacitiveSensor(30);
long total6 = cs_2_8.capacitiveSensor(30);
long total7 = cs_2_9.capacitiveSensor(30);
long total8 = cs_2_10.capacitiveSensor(30);
long total9 = cs_2_11.capacitiveSensor(30);

if(total1 > 200){


Serial.print('A');
delay(1);
}
if(total2 > 200){
Serial.print('B');
delay(1);
}
if(total3 > 200){
Serial.print('C');
delay(1);
}
if(total4 > 200){
Serial.print('D');
delay(1);
}
if(total5 > 200){
Serial.print('E');
delay(1);
}
if(total6 > 200){
Serial.print('F');
delay(1);
}
if(total7 > 200){
Serial.print('G');
delay(1);
}
if(total8 > 200){
Serial.print('H');
delay(1);
}
if(total9 > 200){
Serial.print('I');
delay(1);
}
}

Xxxxx

Sketch 2#

Code: [Select]
char incomingByte;

void setup() {

// first round of the game, LED's 1,2 and 3 start burning. Touch buttons 1,2
and 3 need to be pushed to proceed

digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);

while(true){

if (Serial.available() > 0) {
incomingByte = Serial.read(); // check for incoming data

if (incomingByte == 'A') {
digitalWrite(LED1, LOW);
}
if (incomingByte == 'B') {
digitalWrite(LED2, LOW);
}
if (incomingByte == 'C') {
digitalWrite(LED3, LOW);
}
if(!digitalRead(LED1)&!digitalRead(LED2)&!digitalRead(LED3))
{
break;
}
}
}

// second round of the game. LED's 7,8,9 start burning. Touch buttons 7,8
and 9 need to be pushed to proceed.
//when a mistake is made (touch button 1,2,3,4,5 or 6 are touched) the
servo rotates and cuts the power to both arduino's to stop the game.

Serial.print(incomingByte);
Serial.println();
digitalWrite(LED7,HIGH);
digitalWrite(LED8,HIGH);
digitalWrite(LED9,HIGH);

while(true){

if (Serial.available() > 0) {
incomingByte = Serial.read();

if (incomingByte == 'G') {
digitalWrite(LED7, LOW);
}
if (incomingByte == 'H') {
digitalWrite(LED8, LOW);
}
if (incomingByte == 'I') {
digitalWrite(LED9, LOW);
}

if(!digitalRead(LED7)&!digitalRead(LED8)&!digitalRead(LED9))
{
break;
}

if (incomingByte == 'A' || incomingByte == 'B' || incomingByte == 'C' ||


incomingByte == 'D' || incomingByte == 'E' || incomingByte == 'F')

{
Serial.print(incomingByte);
servo1.write(20);
}

void loop() {

}
Just do

Code: [Select]
while(Serial.available) // while there are bytes in the serial buffer
{
Serial.read(); //read the bytes and throw them away
}

That will empty the serial buffer.

I am curious why you need 2 Nanos. If you are short on pins, note that the
analog inputs can be used as digital pins (6 pins).

And why all the code in setup with a while(1). That is what loop() is for.
Adding your suggestion did, to my surprise, not change anything. So to
problem had to be somewhere else than the serial buffer.

The thing I couldn't explain was that the second row from the serial monitor
screenshot had multiple C's in it.

Code: [Select]
if (incomingByte == 'A' || incomingByte == 'B' || incomingByte == 'C' ||
incomingByte == 'D' || incomingByte == 'E' || incomingByte == 'F')

{
Serial.print(incomingByte);
servo1.write(20);
}

looking at the statement above I finally became clear that the Character C
was stilling being send on the moment this part of code ran. Increasing the
delay time in the first sketch from 1 to 100 milliseconds did solve this issue.

Code: [Select]
if(total3 > 200){
Serial.print('C');
delay(100);
}
xxxxxxxxxxxxxxx

Arduino Serial Part 3: Getting started


with serial communication
Posted on December 24, 2018

In the last post I briefly talked about different data formats and how I
recommend keeping things as simple as possible. With this is mind for a
first project let’s create a simple blinking LED. We will have one Arduino
controlling an LED on a second Arduino. Controls to turn the LED on or off
will be sent via serial from the first Arduino to the second Arduino. This is as
basic as it gets. Arduino Blink by remote control. The LED has only two
states so simple control codes can be used and to start I am using 1 of on
and 0 for off.

In these examples I am using Arduino Nanos but any kind of Arduino can
be used and for this series I am using Arduino to Arduino communication.
The techniques are exactly the same for any UART to UART device. For
example, in Arduino to Arduino by Bluetooth I use exactly the same serial
communication techniques wirelessly over Bluetooth.

Arduino Serial Example #1: Remote Control Blink 1


Simply circuit consisting of 2 Arduinos with the following connections:
Pin 0 (RX) on the Arduino #1 goes to pin 1 (TX) on Arduino #2.
Pin 1 (TX) on the Arduino #1 goes to pin 0 (RX) on Arduino #2.
Arduino #1 GND to Arduino #2 GND
Arduino #2 D2 to LED + Resistor (220 or 330 ohm)
Arduino Serial Example #1 Remote Control Blink: Master

The master Arduino transmits the commands:

// Arduino Serial Example #1 Remote Control Blink -


Master

void setup()
{
Serial.begin(9600);
// wait for the serial port to connect. Required
for Leonardo/Micro native USB port only
while (!Serial) { ; }
}

void loop()
{
Serial.print(1);
delay(1000);
Serial.print(0);
delay(1000);
}

Arduino Serial Example #1 Remote Control Blink: Slave

The slave Arduino receives the commands. If it receives a 1 it turns on the


lED and if it receives a 0 it turns off the LED.

// Arduino Serial Example #1 Remote Control Blink -


Slave

char c = ' ';


byte LED = 2;

void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("START");
}

void loop()
{
if(Serial.available())
{
char c = Serial.read();
if (c=='0') { digitalWrite(LED, LOW); }
if (c=='1') { digitalWrite(LED, HIGH); }
Serial.println(c);
}

This is as simple as it gets.


Arduino #1 transmits a “1” waits a second then transmits a “0” then waits
another second and starts again. It is worth mentioning that the master
Arduino will happily keep transmitting the values regardless of whether or
not there is anything listening.

Remember when you use Serial.print() numbers are converted to ascii. This
means the actual values transited are 48 for 0 and 49 for 1. This is
important to know for when you create the code for the slave Arduino.

The sketch on Arduino #2 initialises a hardware Serial channel, prints a


“START” message to the serial monitor, and then keeps checking for
incoming data. The loop() function is where the magic happens.

if(Serial.available())
{
char c = Serial.read();
Serial.println(c);

if (c=='0') { digitalWrite(LED, LOW); }


if (c=='1') { digitalWrite(LED, HIGH); }
}

First a check is made to see if there is any data in the Serial buffer

if(Serial.available())

If there is data in the buffer a single character is read and copied to the
char variable called “c”. If you are not familiar with char and byte look up
Arduino data types.

char c = Serial.read();
At this point we have a value in “c”. We are only interested is “0” and “1” so
the next bit checks for these values.

if (c=='0') { digitalWrite(LED, LOW); }


if (c=='1') { digitalWrite(LED, HIGH); }

If c==’0′ the LED is turned off with digitalWrite(LED, LOW); and if c==’1′ the
LED is turned on with digitalWrite(LED, HIGH);. Any other value is ignored.
Notice that 0 and 1 are inside single quotes. This is because they are the
ascii characters and I am using a char variable. With chars use single quotes
‘0’ and ‘1’ not double quotes such as “0” and “1”. Because I am using a char
i could have also checked the actual value rather than the ascii character

if (c==48) { digitalWrite(LED, LOW); }


if (c==49) { digitalWrite(LED, HIGH); }

This works in exactly the same way but is slightly harder to read.

Since we are only interested is “1”s and “0”s everything else is ignored. It is
possible for the master device to send any character but the slave only acts
on the “1”s and “0”s.

If you have already built the circuit you will need to remove the serial
connections before uploading the sketches. If you forget to remove the
serial connections the IDE will attempt to upload, fail and eventually
timeout. Of course, after the upload you need to reconnect. This is one of
the problems with using hardware serial and the disconnection /
reconnection quickly gets annoying.
Upload the sketches and you should have Remote Blink. Remember it is the
master Arduino that is controlling the LED.

You can also check the received data by opening th3e serial monitor on the
slave Arduino.

Arduino Serial Example #2: Remote Control Blink 2


Using this technique it is fairly simply to add a second LED so let’s try it.
Connect a second LED (plus resistor) to the Slave Arduino on pin D3.

Arduino Serial Example #2 Remote Control Blink: Master

// Arduino Serial Example #2 Remote Control Blink -


Master

void setup()
{
Serial.begin(9600);
// wait for the serial port to connect. Required
for Leonardo/Micro native USB port only
while (!Serial) { ; }
}

void loop()
{
Serial.print(1);
Serial.print(3);
delay(1000);
Serial.print(0);
Serial.print(2);
delay(1000);
}

You should see in the above that I have add 2 more commands that are
used to control the second LED:
“3” to turn on the LED, and
“2” to turn it off.

Arduino Serial Example #2 Remote Control Blink: Slave

// Arduino Serial Example #2 Remote Control Blink -


Slave

char c = ' ';


byte LED1 = 2;
byte LED2 = 3;

void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);

Serial.begin(9600);
Serial.println("START");
}

void loop()
{
if(Serial.available())
{
char c = Serial.read();
if (c=='0') { digitalWrite(LED1, LOW); }
if (c=='1') { digitalWrite(LED1, HIGH); }
if (c=='2') { digitalWrite(LED2, LOW); }
if (c=='3') { digitalWrite(LED2, HIGH); }
Serial.println(c);
}

The two new commands are taken care of with

if (c=='2') { digitalWrite(LED2, LOW); }


if (c=='3') { digitalWrite(LED2, HIGH); }

You should be able to see that more devices, like LEDS, can be added
simply by using more commands.
Arduino Serial Example #3: Remote Control Blink 3
The above works and is reliable but if this is all you want to do is turn
something on and off at regular intervals there is no need to use two
Arduinos. Here we add a couple of switches to control the LEDs.

To the existing circuit, on Arduino #1 add a button switch on pin A0/D14


and A1/D15. If you don’t already know, the analogue pins (except A5 & A6
which are analogue only) can be used as digital pins. I actually picked these
pins so the circuit diagram looked neat. No other reason.
I don’t spend much time on explaining the button switch code, if you are
interested in learning more about this see the switching things on and off
post.

Arduino Serial Example #3 Remote Control Blink: Master

// Arduino Serial Example #3 Remote Control Blink -


Master

byte buttonSwitch1 = 14;


byte buttonSwitch2 = 15;

boolean buttonState1 = false;


boolean buttonState2 = false;
boolean buttonState3 = false;

boolean buttonSwitch1_State_old = false;


boolean buttonSwitch2_State_old = false;

void setup()
{
Serial.begin(9600);
// wait for the serial port to connect. Required
for Leonardo native USB port only
while (!Serial) { ; }
Serial.print("Start");

void loop()
{
// simple debounce
buttonState1 = digitalRead(buttonSwitch1); delay(1);
buttonState2 = digitalRead(buttonSwitch1); delay(1);
buttonState3 = digitalRead(buttonSwitch1); delay(1);

if ( (buttonState1 == buttonState2) && (buttonState1


== buttonState3) )
{
// has the button switch state changed?
if (buttonState1 != buttonSwitch1_State_old)
{
buttonSwitch1_State_old = buttonState1;
if (buttonSwitch1_State_old == HIGH) {
Serial.print(1);} else { Serial.print(0);}
}
}

buttonState1 = digitalRead(buttonSwitch2); delay(1);


buttonState2 = digitalRead(buttonSwitch2); delay(1);
buttonState3 = digitalRead(buttonSwitch2); delay(1);

if ( (buttonState1 == buttonState2) && (buttonState1


== buttonState3) )
{
// has the button switch state changed?
if (buttonState1 != buttonSwitch2_State_old)
{
buttonSwitch2_State_old = buttonState1;
if (buttonSwitch2_State_old == HIGH) {
Serial.print(3);} else { Serial.print(2);}
}
}

The state of the button switches are checked and if the state has changed
the control code is sent. The main bit is that the codes are only sent when
the switch position changes.

Arduino Serial Example #3 Remote Control Blink: Slave

The slave skecth has not changed.

// Arduino Serial Example #3 Remote Control Blink -


Slave

char c = ' ';


byte LED1 = 2;
byte LED2 = 3;

void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);

Serial.begin(9600);
Serial.println("START");
}

void loop()
{
if(Serial.available())
{
char c = Serial.read();
if (c=='0') { digitalWrite(LED1, LOW); }
if (c=='1') { digitalWrite(LED1, HIGH); }
if (c=='2') { digitalWrite(LED2, LOW); }
if (c=='3') { digitalWrite(LED2, HIGH); }
Serial.println(c);
}

Now, instead of using a timer to send the commands button switches are
used. Close a switch and an LED comes on. Open the switch and the LED
turns off.

I find it a little annoying that the wrong switch turns on the wrong LED but I
will leave you to change it if you so wish.

Arduino Serial Example #4: Remote Control Blink Using


Software Serial
If you followed along with the examples I suspect you were, at least a little,
annoyed that you had to keep removing and reconnecting the wires to the
serial pins. When writing this guide I forgot at least a couple of times.
Although hardware serial is always the best choice if it is available to you it
can be frustrating when developing the code. In the rest of the examples I
will start using one of the software serial libraries. If you remember from
part 1, the best option is AltSoftSerial followed by NeoSerial with the
default SoftwareSerial at the end. In this example I change the example 3
from hardware serial to software serial using the software serial library that
comes with the Arduino IDE. Not the best choice but useful is this scenario.

Technically you can use pins 0 and 1 for software serial but that would
defeat what I am wanting to do, so, on the master Arduino I am using pins
2 and 3 (2 for TX and 3 for RX) and on the slave Arduino I am using pins 11
and 12 (11 for RX and 12 for TX).
Arduino Serial Example #4 Remote Control Blink Using Software Serial:
Master

// Arduino Serial Example #4 Remote Control Blink Using


Software Serial - Master

#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(2, 3); // RX, TX

byte buttonSwitch1 = 14;


byte buttonSwitch2 = 15;

boolean buttonState1 = false;


boolean buttonState2 = false;
boolean buttonState3 = false;

boolean buttonSwitch1_State_old = false;


boolean buttonSwitch2_State_old = false;

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

void loop()
{
// simple debounce
buttonState1 = digitalRead(buttonSwitch1); delay(1);
buttonState2 = digitalRead(buttonSwitch1); delay(1);
buttonState3 = digitalRead(buttonSwitch1); delay(1);

if ( (buttonState1 == buttonState2) && (buttonState1


== buttonState3) )
{
// has the button switch state changed?
if (buttonState1 != buttonSwitch1_State_old)
{
buttonSwitch1_State_old = buttonState1;
if (buttonSwitch1_State_old == HIGH) {
SoftSerial.print(1);} else { SoftSerial.print(0);}
}
}

buttonState1 = digitalRead(buttonSwitch2); delay(1);


buttonState2 = digitalRead(buttonSwitch2); delay(1);
buttonState3 = digitalRead(buttonSwitch2); delay(1);

if ( (buttonState1 == buttonState2) && (buttonState1


== buttonState3) )
{
// has the button switch state changed?
if (buttonState1 != buttonSwitch2_State_old)
{
buttonSwitch2_State_old = buttonState1;
if (buttonSwitch2_State_old == HIGH) {
SoftSerial.print(3);} else { SoftSerial.print(2);}

}
}
}

You should be able to see that I have removed the line that initialised the
hardware serial and replaced it with

SoftSerial.begin(9600);

Where did “SoftSerial” come from? It is what I called the instance of the
software serial I am using and this is done when you declare the library

#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(2, 3); // RX, TX

I could have used any name (within reason), for example SWS or sSerial.
When declaring the software serial library you need to specify what pins it
is to use. Here I am telling it to use pin D2 for receive and D3 for transmit.

Arduino Serial Example #4 Remote Control Blink Using Software Serial:


Slave

// Arduino Serial Example #4 Remote Control Blink Using


Software Serial - Slave

#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(11, 12); // RX, TX

char c = ' ';


byte LED1 = 2;
byte LED2 = 3;

void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
SoftSerial.begin(9600);
}

void loop()
{
if(SoftSerial.available())
{
char c = SoftSerial.read();
if (c=='0') { digitalWrite(LED1, LOW); }
if (c=='1') { digitalWrite(LED1, HIGH); }
if (c=='2') { digitalWrite(LED2, LOW); }
if (c=='3') { digitalWrite(LED2, HIGH); }
}

In the slave sketch, pins D2 and D3 are already used (bad planning I know)
so I have used D11 and D12

#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(11, 12); // RX, TX

If you give this a try you will find it works exactly the same as example 3
above. The difference is we do not need to remove the serial connections
to upload a new sketch.

This has been a very gentle introduction in to using serial communication


on the Arduino. Using single character codes or controls allows you to keep
the code simple. A single character can be loaded in to a char or byte
variable which is easy to compare or check, a simple ==. There is no need
for char arrays or strings. If the character is not something we want it can
be ignored and the next character read.
if(SoftSerial.available())
{
char c = SoftSerial.read();
if (c=='0') { digitalWrite(LED1, LOW); }
if (c=='1') { digitalWrite(LED1, HIGH); }
if (c=='2') { digitalWrite(LED2, LOW); }
if (c=='3') { digitalWrite(LED2, HIGH); }
}

A key point to highlight is that the serial buffer is checked for data before
attempting to read from it. This allows for non-blocking code that
continues to work until there is data to process.

In the next post we look at using more complex commands.

Xxxxxxxxxxxxxxxxx

Can anyone help? I'm trying to develop two sketches, where data can only
be sent when a request is made for it between two arduinos. A simple hello
world message can suffice. I tried out the Serial Ascii Call and Response on
the arduino IDE but it didn't work for me???. Do find below the code
sketches

//Arduino Sketch 1
uint16_t alpha;
uint16_t lambo;
Banana nanas; //to hold the Banana object
uint16_t dataArray[2];
void Setup(){
Serial.begin(9600);
establishContact();
}

void loop(){
alpha=nanas.getRad;
lambo = nanas.getBars;
dataArray[0] = alpha;
dataArray[1] = lambo;
Serial.write((uint8_t*)dataArray, sizeof(dataArray));
Serial.flush(); //clear the serial port
}

void establishContact()
{
if (Serial.available())
{
while (Serial.available() > 0)
{
int newByte = Serial.read();
if (newByte == 'Q')
{
Serial.write(newByte);
Serial.flush();
}
}
}
}

The sketch for the other arduino is shown below

//Arduino sketch 2
static char buffer[2];
void setup(){
Serial.begin(9600);
Serial1.begin(9600);
establishContact();
}
void loop(){
if(Serial1.available())
{
while(Serial1.available() > 0){
uint16_t gondola1 = Serial1.read();
uint16_t gondola2 = Serial1.read();
uint16_t crixus = gondola2 >> 8;
crixus = (crixus | gondola1);

gondola1 = Serial1.read();
gondola2 = Serial1.read();
uint16_t mario = gondola2 >> 8;
mario = (mario | gondola1);
buffer[0] = crixus;
buffer[1] = mario;
Serial.write(buffer, sizeof(buffer);
Serial1.flush();
}
}
}
void establishContact(){
if (Serial.available()){
uint16_t newByte = Serial.read();
if(newByte == 'Q')
{
Serial.flush();
Serial.write(newByte);
}
}
}
arduino-uno serial arduino-mega

shareimprove this question

edited May 4 '16 at 22:59

asked May 4 '16 at 19:38


PayneLaidBack

1511 gold badge11 silver badge55 bronze badges

 Please elaborate. What boards do you use? What libraries do you use?(code does not compile
as it is) What connections you have made between the boards? Also, I cannot really understand
what exactly you want to happen when which board recieves what from the other board? Send
a message back to the sender's board? Send a message elsewhere? – foivaras May 4 '16 at 20:44


 @foivaras..the boards in question are an arduino uno which the sensor is connected to and an
arduino mega which is used to receive the data being sent, The connections are the usual tx/rx
from the uno to serial1 on mega. there are no other libraries included. The Mega request for
data from the uno..uno sees the request command and then sends data .. mega receives data
and request for more data. Thats it in a nutshell – PayneLaidBack May 4 '16 at 21:57
 if you do not use any library then your code does not compile. Sketch one has an undeclared
Servo type. Sketch two gives a redeclaration of uint16_t gondola1 error. – foivaras
May 4 '16 at 22:52
 @foivaras the Servo class I used is just a random name I adopted ...in hindsight I should have
changed it to something else..i just wanted to show that there is a class that an object can be
declared from, to call its respective methods. I'll edit the sketches out now – PayneLaidBack May
4 '16 at 22:56
 1

Setup() should be setup() in the first sketch? What exactly is Banana? Why complicate
things, if all you want to do is send 'Hello world' on request? If that isnt your goal, then edit your
post and say exactly what the sketches are for and exactly what you mean by it didn't work
for me. – TisteAndii May 5 '16 at 0:09

add a comment

1 Answer
active oldest votes

it is not really easy to follow your code but here is a "hello world"
procedure to do an on demand data send between two arduinos:

 upload this code to arduino uno before you make any connection on
pins 0 and 1 of the board:
 /**
 * Code for server side (arduino uno)
 */


 uint8_t data2send = 'A'; //data to send on a received request
 uint8_t incomingByte;

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

 void loop() {
 // send data only when you receive data:
 if (Serial.available() > 0) {

 // read the incoming byte:
 incomingByte = Serial.read();

 if(incomingByte=='Q'){ //if incoming data is the expected
request (request code = 'Q' in this example)
 Serial.write(data2send);
 }
 }
}

 connect:
 UNO | MEGA
 ------------
 pin0 | pin18
 pin1 | pin19
GND | GND

 upload to arduino mega this code:


 /**
 * code for arduino mega.
 *
 * proxy between Serial0 and Serial1
 *
 * send data to Serial0 to forward it to Serial1.
 * when data is received from Serial1 it will be forwarded to Serial0.
 */

 void setup() {
 // initialize both serial ports:
 Serial.begin(9600);
 Serial1.begin(9600);
 }

 void loop() {
 // read from port 1, send to port 0:
 if (Serial1.available()) {
 int inByte = Serial1.read();
 Serial.write(inByte);
 }

 // read from port 0, send to port 1:
 if (Serial.available()) {
 int inByte = Serial.read();
 Serial1.write(inByte);
 }
}

 open the mega's serial monitor and send some characters. Check that
you receive back data only when you send a 'Q' character.

Xxxxxxxxxxxxxxxxx

You might also like