Go Down

### Topic: multiple variables to processing (Read 1 time)previous topic - next topic

#### khmay

##### Jun 04, 2011, 06:52 pm
Hi, I have an array of data from 6 PIR sensors and I want to use their states (HIGH/LOW) in a processing sketch.  Can someone point me in the direction of a forum topic or tutorial for a simplistic method of sending multiple variables to processing from sensors that are giving all similar values (I dont know how to differentiate between each of the 6 sensors).
Thanks

code on the arduino end is
Code: [Select]
`#define SIZE 6int relay[SIZE] = {    8, 9, 10, 11, 12, 13 };                                    // output digital pins to solid state relays  (120VAC LIGHTS)int pir[SIZE] = {    2, 3, 4, 5, 6, 7 };                                           // input digital pins from Parallax PIR sensorint time= 20000;                                               //length to stay on after sensor is triggeredunsigned long startTime[SIZE ] = {    0,0,0,0,0,0 };                                                // time relay switched ONunsigned long duration[SIZE ] = {   time, time, time, time, time, 20000  };               // for how long in milli-seconds (note this is minimum time)void setup(){  Serial.begin (9600);                             for (int i=0; i < SIZE; i++)  {    pinMode(relay[i], OUTPUT);       //set digital pins for relays as output    pinMode(pir[i], INPUT);              //set digital pins for PIRs as input  } }void loop(){  for (int i =0; i<SIZE; i++)  {    if (digitalRead(pir[i]) == HIGH)               // check PIR state    {      digitalWrite(relay[i], HIGH);                // switch relay ON if HIGH state      startTime[i] = millis();      Serial.print(digitalRead(pir[i]));      // SOMETHING IN HERE TO PROCESSING....?      Serial.print(",");    }    if((millis() - startTime[i]) >= duration[i])    {      digitalWrite(relay[i], LOW); // switch relay OFF after x    }  }}`

#### PaulS

#1
##### Jun 05, 2011, 04:26 am
If you sent a start and/or end marker (or both) to delineate the start and end of each packet, you would have no trouble telling that the first value after the start of packet marker was for the first sensor, etc.

You could even count values received between the start and end of packet markers was correct before using any values.

Finally, since you are only sending 6 bits of information (a sensor is either HIGH or LOW), you could pack them all into one byte (using bitSet() or bitWrite()), and send just one byte containing all 6 values. No need parse anything then. Use bit-shifting and/or bit masking on the Processing side to extract the individual values.

#### rbtying

#2
##### Jun 05, 2011, 05:57 am
If you just want to use the high/low value, you could do something like this:

Code: [Select]
`// assuming your high/low state is saved in an array of booleans, and is updated regularly:bool data[6];void sendData() {  uint8_t toSend = 0;  for (uint8_t i = 0; i < 6; i++) {    toSend |= data[i] << i;  }  Serial.write(toSend);}`

and receive as a single unsigned char:

Code: [Select]
`unsigned char recv = Serial.read();boolean data[6];for (int i = 0; i < data.length; i++) {  data[i] = recv & (1 << i);}`

That way you don't need start/end markers, since it all fits into a single byte.

#### johnmchilton

#3
##### Jun 05, 2011, 06:33 am
I just got done writing a library that reads and writes messages encapsulated in envelopes.

It uses messages of the format (C) or (C,123,123,...) where C is a message type identifier.

I think you can apply this to your code, maybe. Disclaimer, I used the Serial class from Processing.

I've uploaded the code to my website where I'll keep it for probably a year or so until I figure nobody else is going to find it.

The pertinent code to your project is:
+ in the Arduino folder, where I have the library plus a library it relies on, and an example
+ in the Java folder, in the comms package. The Dashboard class in the uwheel package implements the MessageHandler interface, and responds to both a single-item command plus a command with parameters and therefore is a good example of how to use this.

It should be pretty easy to use this code in your Processing project since Processing is in Java. You'll just have to figure out how to install the comms package as a library in Processing (maybe remove the package declarations), then implement MessageHandler.

In case you are a beginner: Start with Dashboard.java and the Arduino example. You shouldn't need to touch much else, since I have tested both sides.

http://johnmchilton.com/media/uwheel_src.zip

#### johnmchilton

#4
##### Jun 05, 2011, 06:43 am
Upon inspection, it appears this is probably overkill for whatever you're doing, but there it is anyway.

#### khmay

#5
##### Jun 06, 2011, 01:53 am
Thanks Everyone!  That's great, I'll try it out. I really appreciate the help.

Keenan

Go Up