5.4.4 Project 2.2. Table Lamp

1. Description

For common simple table lamp, click the button it will be opened, click it again, the lamp will be closed.

2. Test Code

Calculate the clicked button times and take the remainder of 2, you can get 0 or 1 two state values.

#define btn1 16
#define led_y 12
int btn_count = 0; // Counter for button presses

void setup()
{
  Serial.begin(9600);
  pinMode(btn1, INPUT);
  pinMode(led_y, OUTPUT);
}

void loop()
{
  boolean btn1_val = digitalRead(btn1);
  if(btn1_val == 0) // If button is pressed
  {
    delay(10);  // 10ms delay for debouncing
    if(btn1_val == 0) // Confirm button is still pressed
    {
      boolean btn_state = 1;
      while(btn_state == 1) // Loop until button is released
      {
        boolean btn_val = digitalRead(btn1);
        if(btn_val == 1)  // If button is released
        {
          btn_count++;    // Increment press counter
          Serial.println(btn_count);
          btn_state = 0;  // Exit loop
        }
      }
    }
    boolean value = btn_count % 2; // Modulo operation (0 or 1)
    if(value == 1)
    {
      digitalWrite(led_y, HIGH); // Turn LED on
    }
    else
    {
      digitalWrite(led_y, LOW); // Turn LED off
    }
  }
}

3. Test Result

Open the serial monitor and print out the clicked button times, then click the button once, the LED will be on, click it again, it will be off.

image40