Hey everyone
So as the title suggests, I have been trying to Serial.print a specific element from an array that is fed into by a serial device but when I run the following code the serial monitor returns multiple items from the array.
The serial monitor output ranges from outputing all 12 bytes from the array to minimum of 2 bytes of the data i need plus the next element on the array when using a delay function.
The data in comes from bypassing a controller and lcd connection that outputs 12 bytes, 1 by 1 with the start byte of 65.
In the following code I try to test just outputing the 1st byte with Serial.println(contrlrData[0]); but with no delay i get all the data on the serial monitor, and with a delay of 10ms, I can 3 elements including the first bit.
Watched and read almost every resource online on C++ arrays and using serial first before bothering you fine folk but can't seem to find a solution. Please help.
const int arraylength = 12; //controller sends 12 bytes to LCD, first is a start byte is 65 or "A"
int contrlrData[arraylength];
int t = 10;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600); // test data on monitor
Serial2.begin(9600); // open port to receive data from controller
for (int i = 0; i <= arraylength-1; i++) //set initial array elements to 0
contrlrData[i] = 0;
}
void loop() // run over and over
{
if (Serial2.available()) //check if rx from controller is sending start byte i.e. 65
{
for (int j = 0; j < arraylength-1; j++) //if yes save byte by byte to contrlrData array
contrlrData[j] = Serial2.read(); // assign incoming data to contrlrData array
Serial.println(contrlrData[0]); //print 1st element in contrlrData array
for (int i = 0; i <= arraylength-1; i++) //reset array elemetns to 0
contrlrData[i] = 0;
delay(t);
}
}
Serial monitor output for the above code is
65
16
0
65
16
0
65
16
0
65
16
0
and so on... increasing or reducing the delay doesn't help.