I have 32 IR sensors and when one is activated it prints the number to serial, and the serial monitor of the Arduino reads the sensor number from 0-31. This works perfect and consistently. I have a Processing program that is supposed to read this value from serial and play a one of 32 songs using the minim library. After I read the value from serial, I print the value to the Processing serial monitor. The first sensor reads 48 on Processing, the second reads 49 and so on. Up to 57 works fine, but then when I get to the next sensor which should be 58, it reads 4948 on the Processing serial monitor, which plays the song associated with 48. Then the next sensor is 4949, then 4950, up to 4957, then it reads 5048, 5049, 5050 and so on, and plays the song associated with the last 2 digits. So I am only able to play 10 different songs. I have no idea why this is happening. Any thoughts? Below is part of my Processing code:
We need to see that Arduino code. It looks like it is sending "0", "1", ..., "10", ... , "31" for the 32 sensor IDs.
Your Processing application expects something else. The value 48 is the ASCII value for '0'. The 57 is the ASCII value for '9'. The ASCII value for "10" is not 58. It is 49 and 48.
You need to send something besides the sensor ID, from the Arduino, like a carriage return/line feed.
Then, have Processing read all the serial data until that marker arrives. It has a bufferUntil() method in the Serial class that makes this easy.
myPort.bufferUntil('\n');
Then, implement the serialEvent() function, if you haven't already. That function will called when there is serial data that was delimited by the bufferUntil character.
You are exactly right in that the Arduino is sending "0" , "1" , and so on. I didn't realize that this would work only up to 57 in Processing. My Arduino code is below where d is the assigned value of the sensor from 0-31.
ledOut is a 32 bit array where if the sensor is activated, a 1 is put into the corresponding bit of the array. That is why it is checking for ledOut to be greater than 0.
Thanks for your help. I ended up creating a 32 character array in the Arduino code and printed the character to serial based on the sensor that was activated. I then used myPort.readChar() in Processing to read the character. Everything is now working great.