I would like to switch LCD backlight with a button. I tried to do it this way:
1. Setup IRQ handler that toggles value of global variable lcd_backlight between 0 and 1 everytime the button is pressed.
2. Start a separate thread, that checks the value of the lcd_backlight variable and switches the LCD backlight on or off.
Here is my ugly code (lcdbutton.py):
Code: Select all
# Example of display backlight control using a button
import utime
import _thread
from machine import I2C, UART, Pin
from i2c_lcd import I2cLcd
import bme280
import us100
i2c = I2C(0, pins=('GP13', 'GP12'))
lcd = I2cLcd(i2c, 0x27, 4, 20)
bme = bme280.BME280(i2c=i2c)
uart = UART(0, baudrate=9600, bits=8, parity=None, stop=1)
us = us100.US100(uart=uart, bottom_distance=600)
lcd_backlight = 1 # Used to set LCD backlight on (1) or off (1)
button_last_pressed = utime.ticks_ms() # Used for button debouncing
def button_pressed(line):
"""Toggles the value of lcd_backlight variable when the button is pressed"""
global lcd_backlight
global button_last_pressed
# Button debouncing
if utime.ticks_diff(button_last_pressed, utime.ticks_ms()) > 200:
# Toggle lcd_backlight value
if lcd_backlight == 1:
lcd_backlight = 0
else:
lcd_backlight = 1
button_last_pressed = utime.ticks_ms()
return
# Initialize button pin and IRQ handler
button = Pin('GP17', mode=Pin.IN, pull=Pin.PULL_UP)
button_irq = button.irq(trigger=Pin.IRQ_RISING, handler=button_pressed)
def backlight_control(arg):
"""Checks the lcd_backlight value and turns the backlight on or off"""
last_state = 1
while True:
if lcd_backlight != last_state:
if lcd_backlight == 1:
lcd.backlight_on()
last_state = 1
else:
lcd.backlight_off()
last_state = 0
utime.sleep_ms(500)
return
_thread.start_new_thread(backlight_control,(None,))
while True:
lcd.clear()
lcd.putstr('Temperature: {0} C\n'.format(bme.read_temperature()//100))
lcd.putstr('Humidity: {0} %\n'.format(bme.read_humidity()//1024))
lcd.putstr('Pressure: {0} hPa\n'.format(bme.read_pressure()//25600))
lcd.putstr('Water level: {0} mm'.format(us.read_liquid_level()))
utime.sleep(60)
>>> execfile('lcdbutton.py')
Unhandled exception in thread started by <function backlight_control at 0x20034dc0>
Traceback (most recent call last):
File "lcdbutton.py", line 40, in backlight_control
NameError: name not defined
Line 40 is this one:
Code: Select all
if lcd_backlight != last_state:
Michal