We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Porblem (Read 358 times)
Porblem
Mar 30th, 2009, 11:27pm
 
What I try to do is read from two sensors: light and slider
after received at the serial port into reading from light sensor (valP_light) and reading from slider sensor (valP_slider)
But why is this not working?


import processing.serial.*;
Serial port; // Create object from Serial class

int valP_slider; // Data received from the serial port - variable to store the slider sensor reading
int valP_light; // Data received from the serial port - variable to store the light sensor reading
PImage image_a, image_b;

byte[] inBuffer = new byte[2046]; //size of the serial buffer to allow for end of data characters and all chars (see arduino code)


void setup() {
size(800, 600); //size of window
noStroke();
frameRate(60); // Run 60 frames per second


// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this, "COM5", 9600);
}
void draw() {
if (0 < port.available()) { // If data is available to read,

   port.readBytesUntil('&', inBuffer);  //read in all data until '&' is encountered
   if (inBuffer != null) {
     String myString = new String(inBuffer);
     
     String[] p = splitTokens(myString, "&");  //p is all sensor data (with a's and b's) ('&' is eliminated)
     
           
     String[] slider_sensor = splitTokens(p[0], "b");  //get slider sensor reading
       valP_slider= int(slider_sensor[1]);
   
   print(valP_slider);
   println(" ");  
   
  String[] light_sensor=splitTokens(p[0], "a");  //get light sensor reading
      valP_light= int (light_sensor[1]);
   
   print(valP_light);
   println(" ");
   
image_a = loadImage("Background.jpg");
tint(255,255,valP_light);
image(image_a, 0, 0);

}
}
}
Page Index Toggle Pages: 1