How to force LEDs controlled by a light sensor, to take turns?

Hi,
I'm a beginner and have not much experience with programming.
So here is my current code:

const int ldr_pin = 7;
const int led_pin = 4;
const int led_pin2 = 13;

void setup() {
pinMode(ldr_pin,INPUT);
pinMode(led_pin,OUTPUT);
pinMode(led_pin2,OUTPUT);

Serial.begin(9600);
}

void loop() {
if ( digitalRead( ldr_pin ) == 1){
digitalWrite( led_pin,HIGH);
delay(5000);
digitalWrite( led_pin,LOW);

}
else{
digitalWrite( led_pin2 , HIGH);
delay(5000);
digitalWrite( led_pin2 , LOW);

}

Serial.println( digitalRead( ldr_pin ));
delay(100);
}

So how can I force the program to just turn on a LED, if it hasn't turned on before, even if its getting a "0" again from the sensor, so only if the other one has turned on, by receiving a "1", the first on should be activated.

Thanks for help!

Your "how" question makes no sense. When your program starts no LED has been turned on before. What are you trying to do?

Paul

Write HIGH to one or the other in setup().

not sure what you’re trying (or failing) to accomplish.

usually, it helps to simplify stuff down until it does work, and then build on it.

i suggest that you not write to the serial port, since your main loop could produce way more meaningless serial output than you need.

i would try something like:

void loop() {
if ( digitalRead( ldr_pin ) == 1){
digitalWrite( led_pin,HIGH);
}
else{
digitalWrite( led_pin, LOW);}
delay(1000);
}

once that works as expected, add back the dual led function, possibly by replacing the 2 digitalWrite statements with:

{digitalWrite(led_pin,HIGH);
digitalWrite(led_pin2,LOW);}

and

{digitalWrite(led_pin,LOW);
digitalWrite(led_pin2,HIGH);}

// excuse the lousy formatting. yammv.

It is had better to use analogRead() function. The below code is a part of Arduino Light Sensor controls LED tutorial

// constants won't change
const int LIGHT_SENSOR_PIN = A0; // Arduino pin connected to light sensor's  pin
const int LED_PIN          = 3;  // Arduino pin connected to LED's pin
const int ANALOG_THRESHOLD = 500;

// variables will change:
int analogValue;

void setup() {
  pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
}

void loop() {
  analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin

  if(analogValue < ANALOG_THRESHOLD)
    digitalWrite(LED_PIN, HIGH); // turn on LED
  else
    digitalWrite(LED_PIN, LOW);  // turn off LED
}

Thanks for your help, I solved the problem with "while-loops":

const int ldr_pin = 7;
const int led_pin = 4;
const int led_pin2 = 13;

void setup() {
// put your setup code here, to run once:
pinMode(ldr_pin,INPUT);
pinMode(led_pin,OUTPUT);
pinMode(led_pin2,OUTPUT);

Serial.begin(9600);
}

void loop() {
while( digitalRead( ldr_pin ) == 0)
{

}
digitalWrite( led_pin,HIGH);
delay(5000);
digitalWrite( led_pin,LOW);

while(digitalRead( ldr_pin ) == 1)
{

}
digitalWrite( led_pin2,HIGH);
delay(5000);
digitalWrite( led_pin2,LOW);

Serial.println( digitalRead( ldr_pin ));
delay(100);
}

I'm trying to build a controller for my curtains. My Arduino is connected to a motor and a light-sensor, the pins which are now declared as LEDS, will later be the pins for the motor. the idea was that the light sensor check every 30minutes if there is still sunshine or not, but by using if-conditions the motor would turn every time in the same dire, when there is still light, and its supposed to only turn once, only if it gets a signal that there is no light, it should turn in the other directions once to close the curtains, otherwise do nothing.

Thanks for your help

IoT_hobbyist:
It is had better to use analogRead() function. The below code is a part of Arduino Light Sensor controls LED tutorial

// constants won't change

const int LIGHT_SENSOR_PIN = A0; // Arduino pin connected to light sensor's  pin
const int LED_PIN          = 3;  // Arduino pin connected to LED's pin
const int ANALOG_THRESHOLD = 500;

// variables will change:
int analogValue;

void setup() {
 pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
}

void loop() {
 analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin

if(analogValue < ANALOG_THRESHOLD)
   digitalWrite(LED_PIN, HIGH); // turn on LED
 else
   digitalWrite(LED_PIN, LOW);  // turn off LED
}

for real world use, you might want to modify the example code slightly, such as pausing between reads, and maybe offsetting the threshold value between off and on, to avoid too many short cycles. also make sure the photosensor can't see the led or light that's being controlled.

static bool FlipLED = false;

digitalWrite(LED_BUILTIN, FlipLED);
FlipLED = !FlipLED;
digitalWrite(LED_2, FlipLED);

will make two leds alternate

Hi,
Please read the post at the start of any forum , entitled "How to use this Forum".
OR
https://forum.arduino.cc/index.php?topic=712198.0
Then look down to "code problems" about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What is your light sensor?
Can you post a link to data/specs please?
Is it an LDR, as in light dependent resistor?
If so you would be better to use an analog input and read its value.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
What model Arduino are you using?

Thanks.. Tom... :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.