user19482: I am working on a small project with Processing and Arduino, in which, depending upon the value of a photoresistor, it displays whether it is day/night/evening.
I read the value from the photoresistor and write the value to the serial port. Then in Processing I read the value from the serial port and display depending on the value.
But I get a very flickery output, and there is always a -1 imprinted on the screen as shown in the video.
Please help me out.
link to the video
Code for Arduino:
int ldr1 = A0;
int x,y;

void setup() {
  pinMode(ldr1,INPUT);
  Serial.begin(9600);
}

void loop() {
  x = analogRead(ldr1);
  Serial.write(x);
  delay(100);
}

Code for Processing:
import processing.serial.*;
Serial port;
int x,y;

void setup() {
  size(640, 320);
  port = new Serial(this, "COM3", 9600);
}

void draw() {
  background(0);
  x = port.read();
  text(x,100,100);
  if(x < 400 && x > 200) {
    textSize(32);
    fill(0,0,255);
    text("it is evening",200,200);
  }
  if(x < 200 && x > 20) {
    textSize(32);
    fill(0,255,0);
    text("it is night",200,200);
  }
  if(x > 400 && x < 900) {
    textSize(32);
    fill(255,0,0);
    text("it is morning",200,200);
  }
}


user4179: If you read the documentation for Serial::read() at https://processing.org/reference/libraries/serial/Serial_read_.html 
it says:
"Returns a number between 0 and 255 for the next byte that's waiting in the buffer. Returns -1 if there is no byte, although this should be avoided by first cheacking available() to see if data is available."
Either before calling "Draw" function, or inside "Draw", you should first check:
if(port.available()){
  //do your checking for X value and set text as you are doing before

}

IMPORTANT - you are only ever sending the lower byte (values 0-255) from Arduino when you do Serial.write(x). That is only 1 byte. The "int" return from an analogue read is 10 bits, and needs 2 bytes to store the information from 0-1023. 
You should use the Arduino function Map() to scale it down from 0-1023 to 0-255. It looks like your application should be okay with this, in terms of resolution.
  int val = analogRead(ldr1);
  val = map(val, 0, 1023, 0, 255);
  Serial.write(val); //now you will not lose information

On the Processing side, you would then look at values from 0-255 to determine daylight/times of day.

19482: <p>I am working on a small project with Processing and Arduino, in which, depending upon the value of a photoresistor, it displays whether it is day/night/evening.</p>

<p>I read the value from the photoresistor and write the value to the serial port. Then in Processing I read the value from the serial port and display depending on the value.</p>

<p>But I get a very flickery output, and there is always a -1 imprinted on the screen as shown in the video.</p>

<p>Please help me out.</p>

<p><a href="https://drive.google.com/open?id=0B5NnRFsbrHprNlJYYnBFcUNJQ3M" rel="nofollow noreferrer">link to the video</a></p>

<p><strong>Code for Arduino:</strong></p>

<pre><code>int ldr1 = A0;
int x,y;

void setup() {
  pinMode(ldr1,INPUT);
  Serial.begin(9600);
}

void loop() {
  x = analogRead(ldr1);
  Serial.write(x);
  delay(100);
}
</code></pre>

<p><strong>Code for Processing:</strong></p>

<pre><code>import processing.serial.*;
Serial port;
int x,y;

void setup() {
  size(640, 320);
  port = new Serial(this, "COM3", 9600);
}

void draw() {
  background(0);
  x = port.read();
  text(x,100,100);
  if(x &lt; 400 &amp;&amp; x &gt; 200) {
    textSize(32);
    fill(0,0,255);
    text("it is evening",200,200);
  }
  if(x &lt; 200 &amp;&amp; x &gt; 20) {
    textSize(32);
    fill(0,255,0);
    text("it is night",200,200);
  }
  if(x &gt; 400 &amp;&amp; x &lt; 900) {
    textSize(32);
    fill(255,0,0);
    text("it is morning",200,200);
  }
}
</code></pre>
 4179: <p>If you read the documentation for Serial::read() at <a href="https://processing.org/reference/libraries/serial/Serial_read_.html" rel="nofollow noreferrer">https://processing.org/reference/libraries/serial/Serial_read_.html</a> </p>

<p>it says:<br>
<em>"Returns a number <strong>between 0 and 255</strong> for the next byte that's waiting in the buffer. <strong>Returns -1 if there is no byte,</strong> although this should be avoided by first cheacking available() to see if data is available."</em></p>

<p>Either before calling "Draw" function, or inside "Draw", you should first check:</p>

<pre><code>if(port.available()){
  //do your checking for X value and set text as you are doing before

}
</code></pre>

<p>IMPORTANT - you are only ever sending the lower byte (values 0-255) from Arduino when you do Serial.write(x). That is only 1 byte. The "int" return from an analogue read is 10 bits, and needs 2 bytes to store the information from 0-1023. </p>

<p>You should use the Arduino function Map() to scale it down from 0-1023 to 0-255. It looks like your application should be okay with this, in terms of resolution.</p>

<pre><code>  int val = analogRead(ldr1);
  val = map(val, 0, 1023, 0, 255);
  Serial.write(val); //now you will not lose information
</code></pre>

<p>On the Processing side, you would then look at values from 0-255 to determine daylight/times of day.</p>
