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


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


int ldr;

void setup(){

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


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 =;         // read it and store it in val

And here's the serial output


Seattle, WA USA
Online Online
Brattain Member
Karma: 637
Posts: 50299
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.

Pages: [1]   Go Up
Jump to: