Pages: [1]   Go Down
Author Topic: Arduino to Processing odd values  (Read 584 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

I connected a LDR sensor to the arduino, Everything is going well, the serial in the arduino monitor is correct.
but when i listen to serial in processing the values are constantly jumping and have nothing to do with the LDR values.

Any idea what might be going wrong? My code is as follows

Arduino

Code:
int ldr;

void setup(){
  Serial.begin(9600);
}

void loop(){
  ldr = analogRead(A0);
  Serial.println(ldr);
}


Processing

Code:
import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup()
{
  size(200, 200);
  myPort = new Serial(this, "COM5", 9600);
}

void draw(){
  background(0);
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  println(val);
}

And here's the serial output

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49385
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are jamming data at Processing pretty quickly. Is it really necessary to do that?

Unlike the loop() function on the Arduino, the draw() function in Processing is not called as fast as possible. It is called as needed to meet a defined frame rate. If you are jamming data at it faster than that, you need to deal with that data outside of draw().

There is a serialEvent() method that you can override, and read the serial data there.

Finally, you are sending ASCII data to the Processing application, and assuming that it is binary data there. It isn't. You should be using the string read functions, and converting the string to an int.
Logged

Pages: [1]   Go Up
Jump to: