Friday 28 January 2011

Processing (Pyroelectric PIR Part3)





I have decided to post the Processing code to render the data from the Arduino. This my first processing project. I have kept the code very simple and with that flexible. This code does not have to be used with a PIR sensor it could be used with any analog sensor on 2 servos.  


// GR0B Basic draw   - Robert Sturzbecher
// This is my first Processing project. The purpose of this project is to render the values received back from a PIR sensor on 2 servos to give a crude image
// server stepping on the Arduino is 3/180 if you use some thing different 
// I have made the code very generic and flexable to suit any project that outputs (X Y Value)


import processing.serial.*;

Serial myPort;                                   // Create object from Serial class
int val;                                         // Data received from the serial port
int ServoStepping = 3;                           // Change this to match the Arduino servo stepping  


void setup() 
{
  size(555, 550);                                //    ((180x3)+10)
  String portName = Serial.list()[0];            // you will get a error here if port not found or in use. [0] is first serial port found
  myPort = new Serial(this, portName, 19200);      
  background(0); 
}


void draw()
{
 String xString = myPort.readStringUntil('\n');  // read the serial port until a new line
  if (xString != null) {                         // if theres data in between the new lines
    fill(0);                                     // set fill to black

    String[] SerialLine = split(xString,' ');    // split serial string line into array
                                                 // string from arduino should line like this "X: 90 Y: 80 Val: 154"
    int PosX = int(SerialLine[2]);
    int PosY = int(SerialLine[4]);
    int PIRVal = int(SerialLine[6]);
    PosX = PosX *3;                               // zoom pixel size. 1/180 now equles a 3x3 size
    PosY = PosY *3;

    // define Color   
    int R = (PIRVal - 120);                   // you will need to customise these values to your project
    int G = (PIRVal - 130);
    int B = 25;
    
    fill(R,G,B);                                 // set color     
    rect(PosX, PosY, ServoStepping*3, ServoStepping*3);                      // Draw colored square  
    //print some debug info to console
    print(xString);    
    print("RGB: "+R+" "+G+" "+B);
    println(" PIR:"+PIRVal);     
  }
}

No comments:

Post a Comment