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);
}
}
}
1 Answer 1
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.
-
generally speaking, if a given answer is satisfying you then you should mark it as accepted. See also faq about accepting answers. Thank you.foivaras– foivaras05/05/2016 08:07:43Commented May 5, 2016 at 8:07
redeclaration of uint16_t gondola1
error.Setup()
should besetup()
in the first sketch? What exactly isBanana
? 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 byit didn't work for me
.