arduino - processing question

I have an array of 56 pressure pads and have written code for them that produces a serial.println of any that have changed state (from on to off, or vice versa) every 0.25 seconds

all i want to do is send this info to processing: something along the lines of:

(pad no.) 25 (state) 0 (pad no.) 36 (state) 1

etc etc

i have never done this before but it must be simple - how do i go about it?



You could keep it dead simple by just sending three characters per pad, the first giving the state (say "O" (oh) for "On" or "F" for "oFf"), and the following two the number of the pad, with leading zeroes not suppressed (ie. "01", "02", "03"...). (this makes decode easier)

With 56 pads, thats 3 * 56 = 168 chars = 1680 bits per quarter second = 6720 bits per second, so 9600 bps should handle it.

Decoding two ASCII decimal digits is simple : ((tensDigit - '0') * 10) + (unitsDigits - '0')

plus also not all the pads will be firing at once - so only 5 say; this helps with what you describe above

is there any sample code that i can adapt to do what you say? i am experienced in both processing and arduino but a complete novice at making them talk

finally i am running 8 stepper motors from the arduino at the same time... they step once each loop: hence i have wrapped up the pad output to only every quarter second. will the time taken to fire the serial out be an issue at all? i guess i just need to code it up and test it.


There are lots of ways to do this but one easy way is to send two bytes to indicate the pad and state. Because the state can only be high or low, you can also use this byte as a 'header' to indicate the start of a valid message.

For example, your arduino code could have this function to send a header byte followed by a byte indicating the pad number

void sendPad( int pad, boolean state)
   if(state == HIGH)

Here is a Processing sketch that decodes this:

import processing.serial.*;

Serial myPort;        // Create object from Serial class
short portIndex = 0;  // select the com port, 0 is the first port

char HEADER_H = 'H'; 
char HEADER_L = 'L'; 
int pad;         // Data received from the serial port

void setup() 
  size(200, 200); 
  // Open whatever serial port is connected to Arduino.
  String portName = Serial.list()[portIndex];
  println(" Connecting to -> " + Serial.list()[portIndex]);
  myPort = new Serial(this, portName, 9600);

void draw()
  // read the header and pad number:
  if ( myPort.available() >= 2)  // If at least 2 bytes are available,
    if( == HEADER_H) // is this the High header
      pad =;
      println("pad " + pad + "is high");  
    else if( == HEADER_L) // is this the Low header
      pad =;
      println("pad " + pad + "is low");  

I have not run the code but it should be fast enough for what you want.