I'm trying to pass interger data from an array across to another Arduino for display on an LCD.
Just running on the bench if I enable the serial.print statements I can see all of the readings on the serial monitor. If I run it like it is, it appears to be sending all of the same number over and over again. When I connect the jumpers to the second Arduino Mega I get 0's and the start byte value.
My sender is set up like this.
// serial data output
int lfoutput = average / 4; //these are 1023 analog inputs
int rfoutput = average1 / 4;
int lroutput = average2 / 4;
int rroutput = average3 / 4;
int manout = (manual * 100); //this is a decimal factor eg. 1.65
int readings[] = {
0x7fff,lfoutput,rfoutput,lroutput,rroutput,lfpwm,rfpwm,lrpwm,rrpwm,manout,0x7eee };
for (int a=0; a<11; a++){
Serial.print(readings[a]);}
//Serial.print(lfpwm);
//Serial.print("\t");
//Serial.print(average);
//Serial.print("\t");
//Serial.print(rfpwm);
//Serial.print("\t");
//Serial.print(average1);
//Serial.print("\t");
//Serial.print(lrpwm);
//Serial.print("\t");
//Serial.print(average2);
//Serial.print("\t");
//Serial.print(rrpwm);
//Serial.print("\t");
//Serial.print(average3);
//Serial.print("\t");
//Serial.print(manual);
//Serial.print("\t");
//Serial.println("end");
}
My reciever is
int suspData[11]; // create array
int i;
void setup(){
Serial.begin(9600);
Serial1.begin(9600);
}
void loop(){
byte val = 0;
if (Serial1.available() > 0){
if ((val = Serial1.read()) ==0x7fff) { //check for begin byte
for (i = 0; i<11; i++){
suspData[i] = Serial.read();
}}
if (suspData[10] = 0x7eee){ //check for end byte
Serial.print (suspData[0]);
Serial.print ('\t');
Serial.print (suspData[1]);
Serial.print ('\t');
Serial.print (suspData[2]);
Serial.print ('\t');
Serial.print (suspData[3]);
Serial.print ('\t');
Serial.print (suspData[4]);
Serial.print ('\t');
Serial.print (suspData[5]);
Serial.print ('\t');
Serial.print (suspData[6]);
Serial.print ('\t');
Serial.print (suspData[7]);
Serial.print ('\t');
Serial.print (suspData[8]);
Serial.print ('\t');
Serial.print (suspData[9]);
Serial.print ('\t');
Serial.print (suspData[10]);
Serial.print ('\n');
i = 0;
}
}
}
on the serial monitor I get, repeating:
0 0 0 0 0 0 0 0 0 0 32767
I want to start adding bytes to the array after the startbyte arrives and end when the end byte comes in. What am I doing wrong?
if (Serial.available() >= 2)
{
int val = Serial.read () ;
val = (val << 8) + Serial.read () ;
....
}
However using a binary format like this reduces redundancy and if comms gets out of sync how are you going to even detect it?
Perhaps Serial.print (readings [i]) ; Serial.print (",") ; is a better approach?
if (Serial1.available() > 0){
if ((val = Serial1.read()) ==0x7fff) { //check for begin byte
for (i = 0; i<11; i++){
suspData[i] = Serial.read();
}}
First. You check Serial1's buffer but you are filling suspData[] with Serial. Notice the difference?
Second. You aren't checking to make sure there are any characters available (in either Serial1 or Serial) before reading from the buffer. Your single "Serial1.available() > 0" only guarantees 1 character is in the buffer. You probably meant to check to see if 10 characters are in the buffer, before proceeding...
Third. This is almost certainly not what you meant: (val = Serial1.read()) ==0x7fff).
val = Serial1.read() is an assignment operation. You don't use val anywhere, so why bother assigning it anything.
edit: removed incorrect statement about assignments and if=statements
operator= returns the value (this is why you can do a=b=0), so that statement could be valid. However, HardwareSerial::read() returns one byte, so it will never equal 0x7fff
int average = 1;
int average1 = 524;
int average2 = 1023;
int average3 = 892;
int lfpwm = 253;
int rfpwm = 25;
int lrpwm = 125;
int rrpwm = 5;
int target = 520;
float manual = 6.52;
void setup()
{
Serial.begin(9600);
}
void loop()
{
// serial data output
byte lfoutput = average /4;
byte rfoutput = average1 /4;
byte lroutput = average2 /4;
byte rroutput = average3 /4;
byte manout = ((manual * 100) / 4);
byte lfpwm1 = lfpwm;
byte rfpwm1 = rfpwm;
byte lrpwm1 = lrpwm;
byte rrpwm1= rrpwm;
if (lfoutput <2){
lfoutput = 2;}
if (rfoutput <2){
rfoutput = 2;}
if (lroutput <2){
lroutput = 2;}
if (rroutput <2){
rroutput = 2;}
if (manout <2){
manout = 2;}
char readings[] = {
1,lfoutput,rfoutput,lroutput,rroutput,lfpwm1,rfpwm1,lrpwm1,rrpwm1,manout,0 };
for (int a=0; a<11; a++){
Serial.write(readings[a]);}
}
I now have the data numbers appearing on the reciever but I cannot keep the index running correctly. The data shifts position in the array. How do I fix that?
edit: this reciever code keeps the data in line but every 8-9 seconds it misses the 9th byte
edited code
#include "Arduino.h"
char suspData1[11];
int suspData[11]; // create array
String inData[11];
void setup(){
Serial.begin(9600);
Serial1.begin(9600);
}
void loop(){
byte inByte =0;
if (Serial1.available() > 11){
if ((inByte = Serial1.read()) == 0) { //check for end byte, begins with next byte
for (int i = 0; i<11; i++){
suspData[i] = Serial1.read();
}
}
//if (suspData[i] == 0){ //check for end byte
Serial.print (suspData[0]);
Serial.print ('\t');
Serial.print (suspData[1]);
Serial.print ('\t');
Serial.print (suspData[2]);
Serial.print ('\t');
Serial.print (suspData[3]);
Serial.print ('\t');
Serial.print (suspData[4]);
Serial.print ('\t');
Serial.print (suspData[5]);
Serial.print ('\t');
Serial.print (suspData[6]);
Serial.print ('\t');
Serial.print (suspData[7]);
Serial.print ('\t');
Serial.print (suspData[8]);
Serial.print ('\t');
Serial.print (suspData[9]);
Serial.print ('\t');
Serial.print (suspData[10]);
Serial.print ('\n');
}
}
You have been told about this before why are you ignoring what people have told you.
Serial is slow so when only one byte has been received you go and read all 11 bytes. This is before the others have arrived.
Okay so you ignored comments about how to use Serial.Available(). You need to fix that before anything will work.
You should also use the IDE's command: Tools -> Auto Format. You have some strange formatting which is going to bite you eventually (e.g. two close braces on the same line).
if ((val = Serial1.read()) == 1) {
Why do you insist on assigning something to "val" when it isn't used anywhere?
I missed the serial available. Changed that now. Did the autoformat.
Right now I want it to find byte 1 and use that to begin the array as a start byte. I also have made certain no values the sender transmits will be a 1 or 0 so I can use them for start, stop to keep the array aligned. I am not certain how to do this correctly. As of now my data columns shift position every half second or so.
edit: I made a change to the code. Now it is keeping the columns aligned but misses the 9th byte from time to time, causing the 10th byte to fall in the 9th column. Found that if I check for the array[10] position to be 0, my endbyte before serial.print that I never see the missed data.
I did look at EasyTransfer but I was hoping to be able to do it with less variable defining. Also I have another project in mind where a electronic module sends ascii strings out its serial port when running. I would like to be able to read those strings and pull one number out for the arduino to react to. EastTransfer will not help me learn how to do that.
i tried this and the problem did move with the chip.
So assuming all the members with crystal balls or ESP are busy, if your new code dosn't work then post it and say what it does and what you expect it to do.