Hey people,
What I'm trying to do is to read input from the Serial port on Arduino Mega2560 and store it in an array whenever there is data. The array will be used as soon as it's populated and then when there is more data available on the serial comms, it can be reused.
The problem is the size of the array, which is not known since it's the same as the size of the data on the serial com.
if(Serial.available())
{
SerialAvailable = Serial.available();
Serial.write(SerialAvailable);
Serial.println();
uint8_t payload[SerialAvailable];
while(SerialAvailable > 0)
{
payload[counter] = Serial.read();
Serial.write(payload[counter]);
counter++;
SerialAvailable--;
}
This code doesn't give any errors at compiling, but when the I run it with a for-loop to print the elements of the array, I get random stuff every time I send something over the serial. And they're not consistent, so the data could be "123" and the printed data is something random, and then the second time I send "123" and I get something completely different than the first one.
Any suggestions on how to do this would be much appreciated.