"""
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Raspberry Pi Pico I2C LCD Display (MicroPython) ┃
┃ ┃
┃ An example of using an I2C LCD display with the ┃
┃ Raspberry Pi Pico. ┃
┃ ┃
┃ Copyright (c) 2023 Anderson Costa ┃
┃ GitHub: github.com/arcostasi ┃
┃ License: MIT ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
"""
from machine import I2C, Pin
from time import sleep
from pico_i2c_lcd import I2cLcd
btn_green = Pin(8, Pin.IN, Pin.PULL_UP)
btn_blue = Pin(9, Pin.IN, Pin.PULL_UP)
btn_yellow = Pin(10, Pin.IN, Pin.PULL_UP)
btn_red = Pin(15, Pin.IN, Pin.PULL_UP)
i2c_bus = I2C(0, sda=Pin(0), scl=Pin(1), freq=40000)
i2c_address = i2c_bus.scan()[0]
lcd_display = I2cLcd(i2c_bus, i2c_address, 2, 16)
def lcd_display_list(strings):
if strings:
for line in strings:
lcd_display.clear()
lcd_display.move_to(0, 0)
lcd_display.putstr(line)
sleep(1)
else:
lcd_display.move_to(0, 0)
lcd_display.putstr("no colours")
lcd_display.move_to(0, 1)
lcd_display.putstr("pressed")
# add to this list in the while loop below
colours = []
# Part 1: Receiving input and adding to a list
while True:
if btn_green.value() == 0:
print("green pressed")
# add "green" to the colours list
elif btn_blue.value() == 0:
print("blue pressed")
# add "blue" to the colours list
elif btn_yellow.value() == 0:
print("yellow pressed")
# add "yellow" to the colours list
elif btn_red.value() == 0:
print("red pressed")
# pressing the red button will stop receiving input from the buttons
break
sleep(0.3)
print("colours pressed were: \n")
print(colours)
lcd_display_list(colours)
# Part 2: Go through the colours list and convert all elements to upper case (must use a for loop).
# print capitalised elements to the console using print()
# Part 3: Open and read the colours.txt file into a list.
# add the contents of the colour list from above to the colours.txt file.
# reread the modified colours.txt file and place the each line in a list called lines
lines = []
# code here
lcd_display_list(lines)
with open("colours.txt", "r") as f:
contents = f.read()
sleep(0.1)
print(contents)
sleep(1)