I want to thank the community for all of these great posts on this forum! I have been reading code and learning for weeks now and don't know what I would have done without this forum.
Still working on passing integer values from Arduino Uno that is monitoring sensors and relaying information back to Mega 2560.
After a week I finally got the Mega to read data using "Serial1" and looking at info on "Serial" from the Uno sending on rx-tx. Uno has wire from pin 1 (tx) to Mega pin 19 (rx1) and they are grounded together.
I found a sketch on here by Mr Gammon that sends bytes with delimiters on the front and back of the byte and it is working great. I am sending 5 bytes from Uno to Mega. They come out of Uno Serial screen like this.
etc..
My problem is I can read the correct value out from Mega on com serial screen right when the number gets read, but I can't assign a value to a variable to pass the info into the program so I can do something with it. I have tried pointers, counters, arrays, integers, functions and about everything else I can possibly think of, and still can't get the values be able to use them in my code. Would like to be able to call on a function "getdata" and have it pass what is read from Uno to useable data in Mega scetch. Sure could use some help, thanks
This code displays the correct values on Mega com screen.
const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter = '>';
int counter = 0;
int cdata[5];
void setup(){
Serial1.begin(9600);
Serial.begin(9600);
}
void loop() {
static long receivedNumber = 0;
static boolean negative = false;
byte c = Serial1.read ();
switch (c) {
case endOfNumberDelimiter:
if (negative){
cdata[counter] = (- receivedNumber);
Serial.println(receivedNumber);
counter = counter + 1;
if(counter > 4)
counter = 0;
}
else{
cdata[counter] = receivedNumber;
Serial.println(cdata[counter]);
counter = counter + 1;
if(counter > 4)
counter = 0;
}
case startOfNumberDelimiter:
receivedNumber = 0;
negative = false;
break;
case '0' ... '9':
receivedNumber *= 10;
receivedNumber += c - '0';
break;
case '-':
negative = true;
break;
}
}
This code does not work, how can I get these bytes read from Uno to be able to use them?
const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter = '>';
int counter = 0;
int cdata[5];
void setup(){
Serial1.begin(9600);
Serial.begin(9600);
}
void loop() {
static long receivedNumber = 0;
static boolean negative = false;
byte c = Serial1.read ();
switch (c) {
case endOfNumberDelimiter:
if (negative){
cdata[counter] = (- receivedNumber);
counter = counter + 1;
if(counter > 4)
counter = 0;
}
else{
cdata[counter] = receivedNumber;
counter = counter + 1;
if(counter > 4)
counter = 0;
}
case startOfNumberDelimiter:
receivedNumber = 0;
negative = false;
break;
case '0' ... '9':
receivedNumber *= 10;
receivedNumber += c - '0';
break;
case '-':
negative = true;
break;
}
Serial.println(cdata[0]);
Serial.println(cdata[1]);
Serial.println(cdata[2]);
Serial.println(cdata[3]);
Serial.println(cdata[4]);
Serial.println();
delay(2000);
}