Project 2: Table Lamp
Description
The common table lamp uses LED lights and buttons, which can control the light on and off pressing the button.
Button Principle
The button module is a digital sensor, which can only read 0 or 1. When the module is not pressed, it is in a high level state, that is, 1, when pressed, it is a low level 0.

Pins of the Button
Button 1 |
16 |
|---|---|
Button 2 |
27 |
Project 2.2. Table Lamp
Description
For common simple table lamp, click the button it will be opened, click it again, the lamp will be closed.
Test Code
Calculate the clicked button times and take the remainder of 2, you can get 0 or 1 two state values.
from machine import Pin
import time
button1 = Pin(16, Pin.IN, Pin.PULL_UP)
led = Pin(12, Pin.OUT)
count = 0
while True:
btnVal1 = button1.value() # Reads the value of button 1
#print("button1 =",btnVal1) #Print it out in the shell
if(btnVal1 == 0):
time.sleep(0.01)
while(btnVal1 == 0):
btnVal1 = button1.value()
if(btnVal1 == 1):
count = count + 1
print(count)
val = count % 2
if(val == 1):
led.value(1)
else:
led.value(0)
time.sleep(0.1) #delay 0.1s
Test Result
The shell will print out the clicked button times, then click the button once, the LED will be on, click it again, it will be off.

