i've written a small sketch that reads data from a Flash application over the serial port using TinkerProxy. everything works as it should from the software: i can connect the device, disconnected the device and send the appropriate data. the data i'm sending from Flash that is read by the Arduino is a simple byte array of 3 values (3 colors) from 0-255.
the bizarre problem i am seeing is that if i press the reset button on the Arduino and it restars WHILE Flash is still quickly sending the data, the byte array will always shift up one position. so instead of the red value going to rgb.element[0], it goes to rgb.element[1]. it's also cyclic, so the last element[3], becomes the first element[0].
why is this happening?
of course, if i wait a few seconds before sending data after it resets, the array is normal. however, i would like the ability to press the reset button during the execution of my Flash program and have the data read appropriately when the board is finished resetting.
What happens if there are 4 bytes in the buffer before this call is made? It should be >= 3, not == 3.
The way that you are sending data now is just a stream of bytes, not a series of rgb values. You need to include some sort of start and end of packet markers, so the Arduino knows WHICH byte is the r value.
If the sending program send , etc., there would be no ambiguity as to which value was for r, which was for g, and which was for b.
so to my knowledge i'm always sending 3 bytes at a time and i haven't set up any data buffer in my sketch. therefore, i believe my if statement is correct, although i'm not completely convinced and wide open for corrections.
how would you code the data so that it's no ambiguous, and hopefully solve the array shift that is caused by resetting the board?
I'm not familiar with flash or it's socket methods, but, you need to write some end of packet marker. Right now, the data stream you are writing looks like
rgbrgbrgbrgbrgb
which you interpret as
rgb rgb rgb rgb rgb
and assume that the values are r, g, and b.
During a reset, some of the data is lost. You end up with
brgbrgbrgb
which you interpret as
brg brg brg b
and assume that the values are r, g, and b.
With some sort of end of packet marker, you could determine if you received 3 bytes prior to the end of packet marker.
If you were sending
rgb;rgb;rgb;rgb;rgb;
and interpreting as rgb rgb rgb, you'd be OK.
If some data got lost, and you read only b;rgb;rgb;rgb;, you'd know that the 1st packet was incomplete, and you'd not use any of the data in the packet.
therefore, i believe my if statement is correct
What triggers you to send more data? What is to prevent sending more data before the previous data has all been read? Does the Arduino tell the flash application that it is ready for more data? If not, there is a possibility for there to be more than 3 bytes in the serial buffer. If that happens, your code will stop working. Once there are more than 3 bytes in the buffer, nothing will ever happen to reduce that count.
ok, i understand now, so i'll use >= in my if statement.
still trying to figure out the best way to read/ignore data packets, but in the meantime i've notice that placing a small delay(250) at the end of the setup() method corrects this problem. however, the colors array will still be out of place, but the delay seems to put it back into the correct order.