Step increments of LEDs using photoresistor

Good morning, I am currently working on a project in which I am trying to set up 2 LEDs (more if I can get it to work) and a photoresistor. Essentially, I would like that when the photoresistor goes over a threshold it lights up an LED. Once the light source comes back and the photoresistor goes over the threshold again it turns off the first light and lights the second. I am very new to coding and keep having issues with my ‘and’ command and the verification tells me too many arguments. Any help would be very appreciated, thank you

const int led1=3; // variable which stores pin number
const int led2=4;
int sensor_value = analogRead(A0);

void setup()
{
pinMode(led1, OUTPUT); //configures pin 3 as OUTPUT
pinMode(led2, OUTPUT);
}

void loop()
{

if (sensor_value < 150)// the point at which the state of LEDs change
{
digitalWrite(led1, HIGH); //sets LEDs ON
delay(5000);
}
if ((sensor_value < 150) && (digitalRead(led1,HIGH)))
{
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(5000);
}

}

led_test.ino (406 Bytes)

digitalRead(led1,HIGH)The problem is not "and", but digitalRead.

Please remember to use code tags when posting code

Let’s start with an obvious problem as pointed out by the compiler

if ((sensor_value < 150) && (digitalRead(led1,HIGH)))

Did you mean

if ((sensor_value < 150) && (digitalRead(led1) == HIGH))

and are you ever going to turn led2 off ?

my mistake on the code tag, I though that the attachment would generate it. I will be sure to look in the forum how to do in properly in the future

Ok this is the new code, does it seem like it will work. I am building the circuit now

const int led1=3; // variable which stores pin number
const int led2=4;
const int led3=5;
int sensor_value = analogRead(A0);

void setup() 
{
  pinMode(led1, OUTPUT);  //configures pin 3 as OUTPUT
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

void loop() 
{

  if (sensor_value < 150)// the point at which the state of LEDs change 
    { 
      digitalWrite(led1, LOW);  //sets LEDs ON
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      delay(1000);
    }
  if (sensor_value > 150) 
    { 
      digitalWrite(led1, HIGH);  //sets LEDs ON
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      delay(5000);
    }
 if ((sensor_value > 150) && (digitalRead(led1) == HIGH))
{
  digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, LOW);
  delay(5000);
}
 if ((sensor_value > 150) && (digitalRead(led2) == HIGH))
{
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  delay(5000);
}
}

It may be that the Arduino compiler will let you do a meaningful digitalRead() on a pin declared to be OUTPUT, but in my view that is not good programming practice. It makes a program hard to understand and probably, not portable to other devices.

Instead, I prefer to use flags to keep the state of the LEDs.

e.g.

// in setup()
byte led1_on = FALSE;  //flag for state of LED1, now off
...

//in loop()

digitalWrite(led1,1); //turn on LED1
led1_on = TRUE;  //set flag indicating LED1 is on

...

if (led1_on  && (sensor_value > 150)) { //do something

Ok, thank you for the insight