Project 3: The Green Lamp

For my third project, I decided to play around with the photo-resistor. The goal was to use analog inputs to manipulate LEDs, so what I did was set my LED to first turn on if there was a certain amount of light.

After, I built a small lamp-like device that covered or shadowed my photo-resistor. That way, the LED would remain turned off. Inside my lamp box, there is a small green LED. I programmed so that the switch I had next to it would turn on the green LED, which would provide artificial light to my photo-resistor. Once it detected light, the photo-resistor would provoke my original red LED to start blinking, indicating that my green LED inside was working since the box is so small and it is arduous to look inside.

const int PR = A0;
const int checklight = 3;
const int rlight = 2;
int value;
const int button = 4;
int PBS = LOW;
int checklightstate = LOW;
unsigned long  pm = 0;
const long interval = 1000;
int rlightstate = LOW;
void setup() {
  // put your setup code here, to run once:
  pinMode(checklight,OUTPUT);
  pinMode(rlight,OUTPUT);
  pinMode(PR,INPUT);
  pinMode(button,INPUT);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  value = analogRead(PR);
  int CBS = digitalRead(button);
  unsigned long cm = millis();
  if(value>500){
    if(cm - pm >= interval){
      pm = cm;
      if(rlightstate == LOW){
        rlightstate = HIGH;
      } else{
        rlightstate = LOW;
      }
      digitalWrite(rlight,rlightstate);
    }
  }
  else{
    rlightstate = LOW;
    digitalWrite(rlight,rlightstate);
  }
  
  if(CBS == HIGH && PBS == LOW){
    if(checklightstate ==HIGH){
      checklightstate = LOW;
    }
    else if (checklightstate ==LOW){
      checklightstate = HIGH;
    }
  }
  digitalWrite(checklight,checklightstate);
  PBS = CBS;
}

Leave a Reply