Go Down

Topic: Arduino to Processing odd values (Read 700 times) previous topic - next topic

notflip

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: [Select]

int ldr;

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

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



Processing


Code: [Select]

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



PaulS

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.

Go Up