How would I get an LED to stay lit when a photocell is actuated- then when it is actuated again, the LED will turn off?
Here is the code I currently have- the LED turns off immediately after removing your finger off of the photocell.
int LDR = 0;
//analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0;
//that’s a variable to store LDR values
int light_sensitivity = 570;
//This is the approx value of light surrounding your LDR
void setup()
{
{
Serial.begin(9600); //start the serial monitor with 9600 buad
pinMode(13, OUTPUT); //we mostly use13 because there is already a built in yellow LED in arduino which shows output when 13 pin is enabled
delay(3000);
}
}
void loop()
{
LDRValue = analogRead(LDR);
Serial.println(LDRValue);
//prints the LDR values to serial monitor //This is the speed by which LDR sends value to arduino
if (LDRValue < light_sensitivity)
{
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}
}
Since this is an analog signal, rather than using a digital comparator it's probably a good idea to add some hysteresis to prevent it from switching erratically when transitioning through the comparison threshold.
Hysteresis basically means you have two switching levels: a lower level where you switch low when it goes below that level, and a higher level where you switch the output on when you go higher than it. Example code:
const uint16_t high_threshold = 575;
const uint16_t low_threshold = 565;
uint8_t light_state = HIGH;
uint8_t previous_light_state = HIGH;
uint8_t LED_state = LOW;
uint16_t light_level;
void loop()
{
// Read light level
light_level = analogRead(LDR);
// If the light level crosses the high_threshold,
// Turn the output on
if( light_state==LOW && light_level>high_threshold )
{
light_state==HIGH;
}
// If the light level crosses the low_threshold
// Turn the output off
if( light_state==HIGH && light_level<low_threshold )
{
light_state==LOW;
}
// If the LDR has been triggered
if( light_state!=previous_light_state && state==LOW )
{
// Invert the LED_state and write it out to the output.
LED_state = !LED_state;
digitalWrite( 13, LED_state );
}
previous_light_state = light_state;
}
Depending on the specifics of your light sensor and application, you might want to increase the different between the high and low thresholds.
Jiggy-Ninja:
Since this is an analog signal, rather than using a digital comparator it's probably a good idea to add some hysteresis to prevent it from switching erratically when transitioning through the comparison threshold.
Hysteresis basically means you have two switching levels: a lower level where you switch low when it goes below that level, and a higher level where you switch the output on when you go higher than it. Example code:
// If the light level crosses the high_threshold,
// Turn the output on
if( light_state==LOW && light_level>high_threshold )
{
light_state==HIGH;
}
// If the light level crosses the low_threshold
// Turn the output off
if( light_state==HIGH && light_level<low_threshold )
{
light_state==LOW;
}
// If the LDR has been triggered
if( light_state!=previous_light_state && state==LOW )
{
// Invert the LED_state and write it out to the output.
LED_state = !LED_state;
digitalWrite( 13, LED_state );
}
previous_light_state = light_state;
}
Depending on the specifics of your light sensor and application, you might want to increase the different between the high and low thresholds.
I uploaded the code and the light is just staying off the whole time.. If you could take a look and identify any issues that would be great! Thanks!
const uint16_t high_threshold = 675;
const uint16_t low_threshold = 250;
byte light_state = HIGH;
byte previous_light_state = HIGH;
int LDR = 0;
byte LED_state = LOW;
byte light_level;
void setup()
{
{
Serial.begin(9600);
pinMode(13, OUTPUT); //start the serial monitor with 9600 buad //we mostly use13 because there is already a built in yellow LED in arduino which shows output when 13 pin is enabled
}
}
void loop()
{
// Read light level
light_level = analogRead(LDR);
Serial.println(light_level);
// If the light level crosses the high_threshold,
// Turn the output on
if( light_state==LOW && light_level>high_threshold )
{
light_state==HIGH;
}
// If the light level crosses the low_threshold
// Turn the output off
if( light_state==HIGH && light_level<low_threshold )
{
light_state==LOW;
}
// If the LDR has been triggered
if( light_state!=previous_light_state && light_state==LOW )
{
// Invert the LED_state and write it out to the output.
LED_state = !LED_state;
digitalWrite( 13, LED_state );
}
previous_light_state = light_state;
}