jlui1200
YaBB Newbies
Offline
Posts: 4
CA
Arduino to Processing, via Serial
Jan 20th , 2009, 9:21pm
I'm having problems using an IR sensor to send values from Arduino to Processing. The Serial library is being used. The IR sensor is connected to an Arduino board, hooked to my MacBook via USB, with Processing also running on the same computer. In the Arduino sketch window, the sensor values (output by Serial.println) look correct, but when they are sent them to the Processing sketch, the println values come out in the Processing window as: -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ... etc I also noticed that the Serial.println values in the Arduino sketch vary depending on whether I have 'Serial.print(range, DEC);' active or not. When that line is on, the numbers I get range from 99 to about 2600. When the line is off, the range is from 7 to about 80 (similar to the actual sensor distances in centimeters). Here's my code (adjusted from existing sample files): -------------------------------------- ARDUINO void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); } void loop() { sensorValue = analogRead(sensorPin); // read the pot value // the sensor actually gives results that aren't linear. // this formula converts the results to a linear range. int range = (6787 / (sensorValue - 3)) - 4; //Serial.print(range, DEC); // print the sensor value to Processing Serial.println(range, DEC); // print the sensor value delay(10); // wait 10 milliseconds // before the next loop } -------------------------------------- -------------------------------------- PROCESSING import processing.serial.*; Serial port; // Create object from Serial class int val; // Data received from the serial port void setup() { //size(200, 200); frameRate(10); // Open the port that the board is connected to and use the same speed (9600 bps) port = new Serial(this, 9600); } void draw() { //if (0 < port.available()) { // If data is available, val = port.read(); // read it and store it in val println(val); //} /* background(255); // Set background to white if (val == 0) { // If the serial value is 0, fill(0); // set fill to black } else { // If the serial value is not 0, fill(204); // set fill to light gray } rect(50, 50, 100, 100); */ } -------------------------------------- I suspect there's a problem with how the Serial parts of the code are written, somehow Arduino is not talking to Processing. (I'm not a great programmer). Please let me know if you have any advice, thanks.