You know, there's a reason why code is supposed to be posted using the # button. Without the code tags, stuff in the code is interpreted as markup data, which clearly mangles the code, making it unreadable.
You will need to modify your post, using the # button to repost the code.
The byte stream into the Arduino will need to be collected into a char array, NULL terminated after each character is added. Then, the strtok() function can be used to parse the data. Each token needs to be converted to an int, before being stored.
Keep in mind that serial data delivery follows the USPS guarantee, not the UPS guarantee. UPS guarantees to deliver your package. USPS guarantees to try to deliver your package.
With that caveat, you need to deal with possible data loss. Any given byte can fail to be delivered. This can be an x value, a y value, a delimiter, etc. You need to plan for this loss. What happens if you don't get 35 x, y, and state values? What happens if the start of packet marker (the *) is lost? What happens if the end of packet marker is lost?
Finally, is there some reason to send x and y? If the values are sent in some defined order, the Arduino can store them in the array in the same order that the values are sent, with 1/3 the effort.
Thanks for the info, I was absolutely clueless about how Serial data was to be handled in the Arduino. Seems like the key is in looking into c++... not the Arduino references..
I'm completely new to serial data handling, are there some basic approach to prevent data loss?
are there some basic approach to prevent data loss?
Not really. The best you can do is to implement error handling, and recognize when the packet sent was not complete.
Of course, some methods and baud rates are more susceptible to loss than others. Serial data at reasonable volumes and rates through a wire seldom results in a loss. High volumes and rates and wireless transmission introduce more opportunities for not getting the transmission sent/received just right.
Ok!, I'm no longer sending x,y,0 , i'm only sending 0s and 1s and fitting them in my array in my arduino, thank you PaulS, it's a lot easier. I struggled with timing, when Serial.available() was greater than 0, my arduino looped too fast for the data to be stored in the buffer.. I guess..so it just stopped reading as there was nothing to read.. but I got it to work by adding a short delay in my Serial to Array loop. Now let's send those pixels from my kinect to processing to my led matrix and see if it works
Oh, and concerning data loss.. My array stores 35 int and I limitted the values to 0s and 1s, so I guess the worst case would be a random pixel being lit(or OFF) until I receive another flawless stream of data.. and now array access is controlled by the arduino... I was so clueless about how to handle this that I wanted my array acces to be controlled by the incoming stream... I read reading and writing to undefined array location isn't a good thing to do.. anyway..
You could speed the data transmission up. Currently, you are sending 35 bytes to send all the values. Since the values are all either one or zero, you could pack the data for each column into one byte, using bit shifting, and send just 5 bytes - one per column.
Bitwise operation will probably be the next thing I'll look into. But I'm pretty sure I'll have to learn what the arduino is reading in the buffer,.. Serial.read() reads the 1st Byte? bitRead() reads the 1st bit? From processing.. Serial.write(1), Serial.write("1"), Serial.write('1')...int? char? char*? string? I'm confused.. I don't know how to send bits anyway. but I got it to work sending a string and we can clearly see my arm on the LED matrix... but everytime I send a string (when the array changes) the LEDs blink.. I guess bitRead could reduce the Off time to something like 1/7th.. which would be great.
Serial.write(1) writes the byte 0x01 (B00000001) to the serial port
Serial.write("1") writes the byte 0x31 (B00110001) to the serial port (this is the character '1')
Serial.write('1') writes the byte 0x31 (B001100001) to the serial port
Serial.read() reads a single byte of data from the buffer. To break it into bits, you would use bitread:
byte b = Serial.read();
// read a bit
bool bitIsOn = bitRead(b, bitNum);
To send bits, pack them into bytes:
Pseudocode:
for (int i = 0; i < columns; i++) {
byte toSend = 0;
for (int j = 0; j < rows; j++) {
if (IsTurnedOn)
toSend |= (1 << j);
}
}