Week 9- Sunset Effect


The concept involves creating an Arduino-based project that combines analog and digital sensors to control two LEDs in a creative way.  The goal is to simulate a sunset effect using the LDR to detect ambient light levels. The system uses the button to sequentially turn on two LEDs and the potentiometer to adjust the brightness, providing a visually appealing sunset simulation.

Sunset Simulation Sequence:

    1. Initialization:
      • Upon activation, both yellow and red LEDs light up simultaneously, representing the starting point of the sunset effect.
    2. First Button Press:
      • When the button is pressed for the first time, the yellow LED dims or turns off, symbolizing the initial phase of the sunset where daylight begins to fade.
    3. Second Button Press:
      • Upon another press of the button, the red LED dims or turns off, indicating the advanced stage of the sunset where the sky adopts warmer hues.
    4. Potentiometer Control:
      • Throughout the simulation, the potentiometer allows users to adjust the overall brightness, offering a real-time customization of the sunset effect.



const int buttonPin = 2;     // Pin number for the push button (connected to ground with pull-up resistor)
const int led1Pin = 3;       // Pin number for the first LED
const int led2Pin = 5;       // Pin number for the second LED
const int potPin = A0;       // Pin number for the potentiometer

int buttonState = HIGH;      // Variable to store the current state of the button
int lastButtonState = HIGH;  // Variable to store the previous state of the button
int led1State = LOW;         // Variable to store the state of the first LED
int led2State = LOW;         // Variable to store the state of the second LED
int potValue = 0;            // Variable to store the potentiometer value
int brightness = 0;          // Variable to store the LED brightness
int flag = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);

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

  // Check if the button is pressed (LOW) and was not pressed before
  if (buttonState == LOW && lastButtonState == HIGH) {
    // Toggle the state of the first LED
    led1State = !led1State;
    digitalWrite(led1Pin, led1State);

    // If the first LED is turned on, wait for the second button press to turn on the second LED
    if (led1State == HIGH) {
      delay(50); // Debounce delay
      buttonState = digitalRead(buttonPin);
      if (buttonState == LOW) {
        // Toggle the state of the second LED
        led2State = !led2State;
        digitalWrite(led2Pin, led2State);
        flag = 1;

  if (flag == 1) {
    // Read the potentiometer value and map it to the LED brightness range (0-255)
    potValue = analogRead(potPin);
    brightness = map(potValue, 0, 1023, 0, 255);

    // Apply the brightness to both LEDs
    analogWrite(led1Pin, led1State ? brightness : 0);
    analogWrite(led2Pin, led2State ? brightness : 0);

  // Save the current button state for the next iteration
  lastButtonState = buttonState;



For future upgrades, I want to make the lights in my project more interesting by creating complex patterns. I’m thinking of making the sunset simulation more dynamic and captivating with intricate lighting sequences inspired by nature. Also, I’d like to get better at drawing schematics.

Leave a Reply