arduino+processing=proplem

Hello!
i tried to take a value from light sensor that connect to arduino uno and i succed to do it and turn on a LED.
after i tried to take the value to processing sketch that show if it is "light" or "dark"
but i always get a wrong value
here is the codes
i would like if someone halp me to find my mistake

arduino

int ledpin=9;
void setup() {
pinMode(ledpin,OUTPUT);
Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);

if(sensorValue>600)analogWrite(ledpin,0);
if((sensorValue<490)&&(sensorValue>350))analogWrite(ledpin,50);
if(sensorValue<350)analogWrite(ledpin,255);
Serial.println(sensorValue);
}

processing

import processing.serial.*;
Serial port;
String bool="";
int index=0;
PFont font;
void setup()

{
  size(500,500);
port=new Serial(this,"COM4",9600);
font = loadFont("AgencyFB-Bold-200.vlw");
textFont(font,200);
}
  void draw()
  {
  background(0,0,index);
  fill(46,209,2);
  text(bool,80,175);
  fill(200, 100,0);
  //to check if index have the corent value
  text(index,80,400);
}
void serialEvent (Serial port)
{index=port.read();
  if(index<=450)
{
    
  bool="dark";
}
if(index>450)
{
  bool="light";
}}

tnks :slight_smile:

Serial.println(sensorValue);

Send some text, like "238".

void serialEvent (Serial port)
{index=port.read();
  if(index<=450)
{
    
  bool="dark";
}
if(index>450)
{
  bool="light";
}}

Read one character, like '2'. If that is less than or equal 450 (it always will be) set a poorly named variable to "dark".

Is that consistent with what you see?

i change the sensor value to 3 and send it to the serial
the procesing give me a unstable number
afther i add a delay of 500ms to arduino and the processing give me a 10 but in a few second is move to a very fast 3 or 50 and back to 10
thanks for repaly :slight_smile:

I'd need to see your code, to be sure, but it looks like you are now sending '3',, and . Those are 51, 10, and 13 in the ASCII table.

Your Processing code needs to read the whole string of data being sent, not one character at a time. Use the bufferUntil() method the the Serial class, in setup() to define when the serialEvent() callback is to be called.

Then, use readStringUntil() in serialEvent(). Then, use String::trim() to get rid of the and . Then, convert the String instance to an int.

Is there a reason you're not simply using Firmata on the Arduino?

Then all the code would be handled through Processing, and you wouldn't need to directly control the serial communications part.

Is there a reason you're not simply using Firmata on the Arduino?

Aside from the fact that it sucks? And that it is unsupported? And that developing a protocol for communication is a great learning exercise?

Not everyone needs crutches.

today before I tried your advice I did something that I thought that can solve the promblem and it Succeeded this is the new and working codes
arduino

int ledpin=9;
void setup() {
pinMode(ledpin,OUTPUT);
Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);
if(sensorValue>600){
  analogWrite(ledpin,0);
  Serial.println('0');
}
if((sensorValue<490)&&(sensorValue>350)){
  analogWrite(ledpin,50);
  Serial.println("1");
}
if(sensorValue<350)
{analogWrite(ledpin,255);
Serial.println('1');
}

}

processing

import processing.serial.*;
Serial port;
int brightness;
String bool="";
int index=0;
PFont font;
void setup()

{
  size(500,220);
port=new Serial(this,"COM4",9600);
font = loadFont("AgencyFB-Bold-200.vlw");
textFont(font,200);
}
  void draw()
  {
  background(brightness,brightness,brightness);
  fill(75,150,196);
  text(bool,80,175);
}
void serialEvent (Serial port)
{index=port.read();
  if(index=='1')
{
    
  bool="dark";
brightness=0;
}
if(index=='0')
{
  bool="light";
  brightness=255;
}}
  Serial.println('0');
  Serial.println("1");

I like consistency. Too bad there isn't any here.

if(sensorValue>600){
  analogWrite(ledpin,0);
  Serial.println('0');
}
if((sensorValue<490)&&(sensorValue>350)){
  analogWrite(ledpin,50);
  Serial.println("1");
}
if(sensorValue<350)
{analogWrite(ledpin,255);
Serial.println('1');
}

So, if sensorValue is between 350 and 490 (that if statement is backwards) send a 1. If it is less than 350, send a 1. If it is greater than 600, send a 0. If it is between 490 and 600, forget it. Do nothing.

What you are setting the LED pin to and what you are sending to the serial port are not consistent.

void serialEvent (Serial port)
{index=port.read();
  if(index=='1')
{
    
  bool="dark";
brightness=0;
}
if(index=='0')
{
  bool="light";
  brightness=255;
}}

What if index is 10 or 13? You are sending those values, too.