Arduino + processing, unsuccessful communication

Hello,

I am quite new at both of these programs, just as a precursor. I can use processing okay, and I have done a lot of Arduino experiments recently with sensors affecting LEDs etc, and this has all been fine.

However, now I am trying to get processing to speak to arduino just to draw a simple graphic based on readings from an LDR, but it will not read the values from the sensor correctly.

I have read a lot about this this morning, when i was first faced with this problem, and therefore I have tried closing the arduino script and only running the processing script so that there is not a conflict over port usage, I have tried importing firmata into the arduino script as someone said this might solve it, however what I am now stuck with, is a list of “values” from the sensor in the println of processing which are entirely random. It is giving me the repeated values of “10, 13, 46, 59 and 233” which are not responsive to the LDR in any way.

I have read a thread where someone had the same problem with these values being repeated but it was never resolved, and so now I turn to the forum in the hope someone knows what is going wrong. I know the arduino is wired up properly because the serial monitor in arduino is giving me responsive LDR values. It is the communication with processing that simply will not work.

Any advice appreciated, thanks in advance.

Arduino code:

#include <Boards.h>
#include <Firmata.h>

int rval=0;
int lightval;

//RECORD MIN AND MAX
int minval=20;
int maxval=900;

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

void loop()
{
  rval=analogRead(1);
  Serial.println(rval);
  
  rval=constrain(rval,minval,maxval);
  lightval=map(rval,minval,maxval,0,255);
  Serial.write(lightval);
  
  delay(50);
}

Processing Code:

import org.firmata.*;
import cc.arduino.*;

import processing.serial.*;

//VARIABLES
Serial port;
float value=0;

//SETUP
void setup()
{
  size(200, 200);

  //open the port you are using at the rate you want:
  port=new Serial(this, "COM3", 9600);
}

//DRAW
void draw()
{
  if (0<port.available())
  {
    value=port.read();
  }
  println(value);
}

However, now I am trying to get processing to speak to arduino just to draw a simple graphic based on readings from an LDR, but it will not read the values from the sensor correctly.

So, Processing is supposed to read the LDR, and the Arduino is supposed to draw something? Seems strange to me.

I think you’ll have better luck getting Processing to LISTEN to the Arduino, and having the Arduino read the LDR and having Processing do the drawing.

Try this in your Processing code:

  // list all the available serial ports:
  println("Serial ports:");
  println(Serial.list());
  println("===============");

What ports are listed?

  if (0<port.available())
  {
    value=port.read();
  }

Do NOT read serial data in the draw() function. Use a serialEvent() function to read serial data.

Unlike the loop() function, the draw() function is not called as often as possible. It is called at a specific frame rate.

#include <Firmata.h>

What the f**k for?

  Serial.write(lightval);

Why are you sending binary data to a process that is expecting to read ASCII data?