Playing with multiple LEDs using a light sensor and a push button!

I created a project where you can play with both analog input and digital input to activate different actions on the LED lights. To begin with, I have installed a photoresistor so that different number of lights can be turned on depending on the light intensity. With the least bright light switching on the red LED, it can go all the way up until the brightest light switching on the green LED. Another aspect I have added to this is the usage of a push button to initiate a collective blinking reaction. All four lights will blink four times and will not be interfered with the light during this process. The LEDs will go back to reacting to light after this. I initially wanted to use the potentiometer but I wanted to show something others have not already done so I chose to make use of the light sensor.

Here is my code:

int photoresistor = A0;
int yellow = 2;
int blue = 3;
int red = 4;
int green = 5;
int button = 6;


void setup() {
  pinMode(yellow, OUTPUT);
  pinMode(blue,OUTPUT);
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(button, INPUT);
}


void loop() {
  
  int button1 = digitalRead(button);
  if (button1 == HIGH) {
    digitalWrite(yellow, HIGH);
    digitalWrite(blue, HIGH);
    digitalWrite(red, HIGH);
    digitalWrite(green, HIGH);
    delay(2000);
    
    int count = 0;
    while (count < 5){
      digitalWrite(yellow, HIGH);
      digitalWrite(blue, HIGH);
      digitalWrite(red, HIGH);
      digitalWrite(green, HIGH);
      delay(20);                  
      digitalWrite(yellow, LOW);
      digitalWrite(blue, LOW);
      digitalWrite(red, LOW);
      digitalWrite(green, LOW);
      delay(1000); 
      count += 1;
      }
  }
  
  else{
    
    int analogValue = analogRead(photoresistor);
    if(analogValue < 50){            
      digitalWrite(red, HIGH);
    }
    else if(analogValue >= 50 && analogValue < 100){
      digitalWrite(blue, HIGH);
    }
    else if(analogValue >= 100 && analogValue <= 150){
      digitalWrite(yellow, HIGH);
    }
    else{
      digitalWrite(green, HIGH);  
    }
    
    delay(100);
    
    digitalWrite(yellow, LOW);
    digitalWrite(blue, LOW);
    digitalWrite(red, LOW);
    digitalWrite(green, LOW);
  }
}

 

Here is the video of how it works:

 

Leave a Reply