I've been playing around with printing the status of a light sensor to the serial monitor. It displays two very basic things: 'sensor open', the sensor senses light, and 'sensor blocked', the sensor is blocked or is not sensing any light. I just need one small thing added to the code that I have not been able to figure out. When the program prints the status of the light sensor, it prints 'sensor open' continuously until the sensor is blocked, and then it prints 'sensor blocked' continuously. Like this:
sensor open
sensor open
sensor open
sensor open
sensor blocked
sensor blocked
sensor blocked
sensor blocked
sensor blocked
sensor open
sensor open
sensor open
I want it to be print 'sensor open' once, and the when the sensor is blocked, 'sensor blocked' only once until the sensor is open once again. Then the serial monitor would look more like this:
sensor open
sensor blocked
sensor open
sensor blocked
If you understand what I mean, help would be really appreciated. I'm really new to the programming aspect of the Arduino.
Here is my code:
int lightPin = 0; //PhotoResistor Pin
int ledPin = 13; // Led Pin
void setup()
{
pinMode(ledPin, OUTPUT); //sets the led pin to output
digitalWrite(ledPin, HIGH); //turns on led
Serial.begin(9600);
}
void loop()
{
int threshold = 300;
if (analogRead(lightPin) > threshold)
{
Serial.println("sensor blocked");
delay(50);
}else{
Serial.println("sensor open");
delay(50);
}}