Professional Documents
Culture Documents
I'm using two arduino nano's that are communicate via the serial port
(TX>RX and RX>TX).
the second nano processes the input from nano 1 and also controls the
LED's and Servo. (see second sketch)
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.
{
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>
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);
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;
}
{
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
}
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
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.
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);
}
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);
}
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.
if(Serial.available())
{
char c = Serial.read();
Serial.println(c);
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′ 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
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.
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.
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);
}
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.
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);
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.
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.
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
#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(2, 3); // RX, TX
void setup()
{
SoftSerial.begin(9600);
}
void loop()
{
// simple debounce
buttonState1 = digitalRead(buttonSwitch1); delay(1);
buttonState2 = digitalRead(buttonSwitch1); delay(1);
buttonState3 = digitalRead(buttonSwitch1); delay(1);
}
}
}
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.
#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(11, 12); // RX, TX
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.
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.
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();
}
}
}
}
//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
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
open the mega's serial monitor and send some characters. Check that
you receive back data only when you send a 'Q' character.
Xxxxxxxxxxxxxxxxx