Afra binjerais – Week 10 assignment

In order to demonstrate both digital and analog input processing, my project uses an Arduino Uno to control two LEDs depending on input from an analog photo resistor and a digital pushbutton. The circuit consists of a pushbutton that activates the system when pressed and a photoresistor that controls the state of a red and a green LED by measuring the amount of ambient light. Pressing the button causes the system to come alive, activating a flag that maintains the LEDs’ responsiveness to light variations. The green LED shines in brighter lighting, while the red LED flashes in low light to signify darkness.

This is my code:

// Define pin numbers
const int buttonPin = 2;     // Digital pin connected to the button
const int redLEDPin = 3;     // Digital pin connected to the red LED
const int greenLEDPin = 4;   // Digital pin connected to the green LED
const int photoResistorPin = A0; // Analog pin connected to the photoresistor

bool ledsActive = false;  // State variable to keep track of LEDs' activity

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(buttonPin);
  int lightLevel;

  // Check if the button is pressed
  if (buttonState == HIGH) {
    ledsActive = true;  // Remember that LEDs should be active
  }

  // Control LEDs based on the 'ledsActive' state
  if (ledsActive) {
    // Read light level from the photoresistor
    lightLevel = analogRead(photoResistorPin);
    
    // Determine which LED to keep on based on light level
    if (lightLevel < 512) {  // Threshold for light level can be adjusted
      digitalWrite(redLEDPin, HIGH);  // Turn on red LED if dark
      digitalWrite(greenLEDPin, LOW); // Turn off green LED
    } else {
      digitalWrite(redLEDPin, LOW);   // Turn off red LED if bright
      digitalWrite(greenLEDPin, HIGH); // Turn on green LED
    }
  } else {
    // Turn off both LEDs if the LEDs should not be active
    digitalWrite(redLEDPin, LOW);
    digitalWrite(greenLEDPin, LOW);
  }
}

And this is the setup of my Arduino:

I really enjoyed playing around with the photoresistor in this project and seeing how variations in the surrounding light dynamically affected the LEDs’ behavior, providing a concrete example of how electronic components interact with their surroundings. In addition to adding another level of interest, this investigation into light sensitivity helped me gain a deeper comprehension of analog sensors. Nevertheless, I did struggle with a couple of things. At first, I had trouble wiring the circuit correctly and dealing with connections that were wrongly placed in incorrect places on the breadboard. Even though they were annoying, these errors taught me a great deal about circuit design and debugging and made me realize how important it is to pay close attention to detail when working with electronics.

Please view the video to visualize how my project works:

Leave a Reply