Leds and photoresistor

When i make darkness only pin 7 works

int lightPin = 0;  //define a pin for Photo resistor

int ledPin1 = 5;
int ledPin2 = 6;
int ledPin3 = 7;


void setup(){
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);    
}

void loop(){

    int lectura = analogRead(lightPin);
  
    if(lectura < 500)
    {
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);      
    }
    else if(lectura >= 500 && lectura < 300)
    {
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);
    }
    else if(lectura >= 500 && lectura < 500)
    {
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);
    }
    else if(lectura >= 700)
    {
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, HIGH);      
    }

    delay(100);
}

When i make darkness only pin 7 works

God will be most upset with you.

if(lectura >= 500 && lectura < 300)

so how can this variable be above 500 and below 300 at the same time?

if(lectura >= 500 && lectura < 500)

and how can this variable be above 500 and below 500 at the same time?

1 Like

Also, this is a bad idea:

int lightPin = 0;  //define a pin for Photo resistor

Pins 0 and 1 are generally used on Arduinos by the serial port for debugging and for code downloading. I am not saying that Pins 0 and 1 can't be used if handled carefully, but otherwise I would suggest that you choose a different pin.

vaj4088:
int lightPin = 0; //define a pin for Photo resistor

Pins 0 and 1 are generally used on Arduinos by the serial port for debugging and for code downloading.

... analogRead(lightPin); // the compiler switches here to A0

int lightPin = 0; // bad practice if you mean the analogue pin
int lightPin = A0; // better

Leo..

I didn't know that about analogRead(). Thanks!