Go Down

Topic: Pd OSC ints > Processing > Arduino > shif (Read 2 times) previous topic - next topic


Is anyone aware of how arduino interprets the port.write(int); method in processing... ie does it recognise the byte as an integer or do I have to convert it with atoi?

Processing sends data as strings. You need, on the Arduino side, to collect all the characters for a value into an array, and pass that (NULL terminated) array to atoi to reconstruct the integer value.


Hi Paul,

Thanks again for the point in the right direction! I think I am almost there... trying to adapt a bit of AWOL's code from another post to mine but it seems to be concatenating values through the serial monitor. if i type a 1 first its fine... if i then type 33 the next number is 13... then a 3 will pop up. Any ideas on where I'm going wrong?


char val [10];
int index = 0;

void loop() {
 long now = millis();
 if (Serial.available() > 0) {
   inByte = Serial.read();
   // if (now % 1000){
   val[index++] = inByte;
   val[index] = '\0';
   int a = atoi(val);
     if (a > 0 && a < 60){
       fadeall(50,1023,0,0,0,0,1023); //from red to blue
     else if (a > 60){
       fadeall(50,0,0,1023,1023,0,0); //from blue to red
     else {
     if (inByte <= '0' || inByte >= '9') {
       index = 0;
   a = 0;
   //  }


if i type a 1 first its fine... if i then type 33 the next number is 13... then a 3 will pop up. Any ideas on where I'm going wrong?

You need some kind of end of packet marker. The Arduino needs to read all serial data available, on every pass through loop, not just one byte.

In addition, the Arduino should not be calling atoi until the end of packet marker arrives. When the end of packet marker arrives, do stuff with the serial data, and then re-initialize everything in preparation for the next packet.


I suggest the SSC "protocol". It's probably the absolute simplest possible, and seems to work well. A packet of data always starts with byte value of 255. All your other data must be less than 255; sacrificing that last bit seems worth the simplicity.

The processing logic is really easy. Look at the bytes as they come in; if you see 255, then reset an index counter and maybe set a flag that you're getting new data. From there, you can either wait for a known number of bytes, time out, or send another 255 to indicate end of transmission. You can put whatever you want in between. Set the first byte as a function type, or channel designator, etc.
Unique RGB LED Modules and Arduino shields: http://www.macetech.com/store


Jan 19, 2011, 08:57 pm Last Edit: Jan 20, 2011, 06:47 pm by NickJPM Reason: 1
Hi guys,

Firstly, Garet thank you for the SSC suggestion, I think that will do nicely as long as I can manipulate the data I'm receiving before passing it through to one of your shiftbrites (ie multiply the value by 100).

I have tried to implement the ssc protocol and can feel I am really close but cannot see where I am going wrong. Whenever I send for example "226" in the serial monitor I get the following:

colour = -1
I received: ΓΏ
this has an ASCII value of -1
Serial reset to: 0
colour = 54
I received: 6
this has an ASCII value of 54
Serial reset to: 0

So it's only seeing the 3rd byte I'm sending (I think this is supposed to happen?)

Arduino Code:

byte s = 0;
int colour = 0;
int SerialCount = 0;

void loop() {
 long now = millis();

 while (Serial.available() > 0) {
   byte s = Serial.read(); //1st byte received of message will be 255
   if (s == 255) {
     SerialCount = 0;
     byte s = Serial.read();
   else if (s != 255 && SerialCount == 0) { //now read the colour value (expected 0-9)
     colour = Serial.read();
     Serial.print("colour = ");
     //colour = colour * 100; //multiply the colour back up to usable number

     fadeall(50,1023,0,0,1023,0,colour); //from red to a purple of somesort
     Serial.print("I received: ");
     Serial.println(colour, BYTE); //what byte value did I receive
     Serial.print("this has an ASCII value of ");
     Serial.println(colour,DEC); //what is this in ASCII format
   if (SerialCount > 0 && SerialCount < 2){
   SerialCount = 0; //reset the counter
   Serial.println("Serial reset to: ");
   Serial.print(SerialCount); //confirm it's been reset

I did post something up earlier but after reading it it was a bit humiliating on my part! Unfortunately I can't seem to get over this last hurdle. Everyone's help is greatly appreciated and tbh I wouldn't have gotten this far without it :D I am trying to multiply that number by 100 but it always returns -100.


Go Up