Go Down

Topic: Arduino to Processing odd values (Read 1 time) previous topic - next topic



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


Code: [Select]

int ldr;

void setup(){

void loop(){
  ldr = analogRead(A0);


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(){
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val

And here's the serial output


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.
The art of getting good answers lies in asking good questions.

Go Up