I am receiving data from an LDR which I am inputting into processing. The data seems to be received to Arduino correctly and I have looked at the data being sent via the serial monitor and it all seems correct. However, the data that is being received in processing doesn't reflect the data received by the LDR so I think I have done something wrong here but not sure what as I am fairly new to this all. (Please see image for what is printing in the processing serial monitor).
Any help identifying where I have gone wrong would be so so helpful!
ARDUINO CODE
int rval=0;
int lightval;
//Recorded minimum and maximum values
int minval=0;
int maxval=950;
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.println(lightval);
Serial.write(lightval);
}
PROCESSING CODE
import processing.serial.*;
Splash paintsplash;
Serial port;
float value=0;
void setup()
{
size(600,600);
smooth();
background(255);
paintsplash= new Splash();
port=new Serial(this, Serial.list()[6],9600);
//printArray(Serial.list());
}
void draw()
{
paintsplash.display();
paintsplash.splash();
delay(200);
if(0<port.available())
{
value=port.read();
}
println(value);
if(value<=75)
{
fill(255,137,132);
}
else if(value>75 && value<=125)
{
fill(32,78,95);
}
else
{
fill(183,215,216);
}
}
PROCESSING CLASS - PAINT_SPLASH
class Splash
{
float x;
float y;
float size;
Splash()
{
x=0;
y=0;
size=int(random(35));
}
void display()
{
//fill(255,137,132);
noStroke();
ellipse(x,y,size,size);
}
void splash()
{
float xcord=int(random(600));
float ycord=int(random(600));
pushMatrix();
translate(xcord,ycord);
for ( float i =3; i < 29; i ++)
{
float splashX = x + random(100);
float splashY = y + random(100);
ellipse(splashX,splashY,size-i,(size-i+random(3)));
}
popMatrix();
}
}
1 Answer 1
You're sending your lightval
twice - once as a textual representation of the number, and once as a character representing the lower 8 bits:
Serial.println(lightval);
Serial.write(lightval);
For example:
Q308
R304
Q306
If you take the 308 and AND it with 255 you get 52. 52 as an ASCII character is R
.
So you print 308, including a newline, then you print "R". Then you print 304, which would be 48 after ANDing with 255 - which of course is a Q.
-
Thank you so much for this! You were correct, I am now getting the correct values in the Arduino serial monitor! I am now having trouble with the processing side of the code. If you can lend any more of your genius I would be very grateful!user62091– user620912020年01月05日 15:41:30 +00:00Commented Jan 5, 2020 at 15:41
Explore related questions
See similar questions with these tags.
Serial.println(lightval);
in the version whose output we see.