Flashing LCD text Pico project

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Flashing LCD text Pico project

Post by soggycashew » Fri Oct 01, 2021 3:52 pm

Hello, I have a project I'm messing with involving a few buttons, active buzzer, 4X20 LCD and Pico. The below code is almost what I'm trying to do and what I have is working in a breadboard except I would like to add a few things.

1) First, Adding flashing text to the block of code until B2 is pressed?

Code: Select all

if button1_counter %2 == 0:  #Pushed it twice kick to buzzer
            lcd.clear() #Clear LCD screen
            lcd.move_to(2,0) #Moves text 2 characters from left on row 1
            lcd.putstr("INCOMPLETE CYCLE")
            lcd.move_to(2,1) #Moves text 2 characters from left on row 2
            lcd.putstr("PULL HANDLE DOWN")
            lcd.move_to(0,3) #Moves text 0 characters from left on row 4
            lcd.putstr("(CHECK ALL STATIONS)")
            utime.sleep(0.5)
            nasty_buzzer()  #Sound buzzer because you pressed the button twice
            button1_counter += 1  #Set counter to 1 incase pressed again
lastly, I'm trying to self learn how to basic program the Raspberry Pi Pico using MicroPython, can someone point me to a good book, videos or example code other than the hundred videos of a flashing LED with a button?

Below is my code so far... Thanks!

Code: Select all

import utime
import machine
from machine import I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd

I2C_ADDR     = 0x27
I2C_NUM_ROWS = 4 #LCDs number of rows
I2C_NUM_COLS = 20 #LCDs number of columns

i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)

B1 = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)  #C to 3.3v, NO to GPIO 14
B2 = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)  #C to 3.3v, NO to GPIO 15
buzzer = machine.Pin(12, machine.Pin.OUT)  #Red to GPIO 12 Black to Ground

button1_counter = 0
button2_counter = 0

def nasty_buzzer():
    for i in range(10):
        buzzer.value(1)  #Start active buzzer
        utime.sleep(0.5) #Pause 1/2 sec
        buzzer.value(0)  #Stop active buzzer
        utime.sleep(0.5) #Pause 1/2 sec

#LCDs 4 rows are 0,1,2,3
lcd.clear() #Clear LCD screen
lcd.move_to(3,1) #Moves text 3 characters from left on row 2
lcd.putstr("LEE LOADMASTER")
lcd.move_to(3,2) #Moves text 3 characters from left on row 3
lcd.putstr("CONTROL PANNEL")
utime.sleep(4) #Pause for 4 seconds

lcd.clear() #Clear screen
lcd.move_to(2,1) #Moves text 2 characters from left on row 2
lcd.putstr("PULL HANDLE DOWN")
button1_counter += 1  #Sets button1 to 1 to show it was pressed on startup
button2_counter = 0   #Sets button2 to 0
utime.sleep(3) #Pause for 3 seconds

while True:
    if B1.value(): #Was the B1 button pushed?
        button1_counter += 1
        button2_counter = 0   #Force the toggle (reset the other side)
        if button1_counter %2 == 1:  #Message head to button 2 on odd
            lcd.clear() #Clear screen
            lcd.move_to(2,1) #Moves text 2 characters from left on row 2
            lcd.putstr("PULL HANDLE DOWN")
            utime.sleep(0.5)
        if button1_counter %2 == 0:  #Pushed it twice kick to buzzer
            lcd.clear() #Clear LCD screen
            lcd.move_to(2,0) #Moves text 2 characters from left on row 1
            lcd.putstr("INCOMPLETE CYCLE")
            lcd.move_to(2,1) #Moves text 2 characters from left on row 2
            lcd.putstr("PULL HANDLE DOWN")
            lcd.move_to(0,3) #Moves text 0 characters from left on row 4
            lcd.putstr("(CHECK ALL STATIONS)")
            utime.sleep(0.5)
            nasty_buzzer()  #Sound buzzer because you pressed the button twice
            button1_counter += 1  #Set counter to 1 incase pressed again

    if B2.value(): #Was the B2 button pushed?
        button2_counter += 1
        button1_counter = 0   #Force the toggle (reset the other side)
        if button2_counter % 2 == 1:  #Message head to button 1 on odd pushes
            lcd.clear() #Clear screen
            lcd.move_to(3,1) #Moves text 3 characters from left on row 2
            lcd.putstr("PUSH HANDLE UP")
            utime.sleep(0.5)
        if button2_counter %2 == 0:  #Pushed it twice kick to buzzer 
            lcd.clear() #Clear LCD screen
            lcd.move_to(2,0) #Moves text 2 characters from left on row 1
            lcd.putstr("INCOMPLETE CYCLE")
            lcd.move_to(3,1) #Moves text 3 characters from left on row 2
            lcd.putstr("PUSH HANDLE UP")
            lcd.move_to(0,3) #Moves text 0 characters from left on row 4
            lcd.putstr("(CHECK ALL STATIONS)")
            nasty_buzzer()  #Sound buzzer because you pressed the button twice
            button2_counter += 1  #Set counter to 1 incase pressed again
 

soggycashew
Posts: 55
Joined: Sat Sep 18, 2021 10:21 pm

Re: Flashing LCD text Pico project

Post by soggycashew » Sat Oct 02, 2021 3:07 pm

I got it....

Code: Select all

def nasty_buzzer():
    for i in range(5): #buzz 5 times
        buzzer.value(1)  #Start active buzzer
        lcd.display_off()  #Turns off LCD
        utime.sleep(0.25) #Pause
        buzzer.value(0)  #Stop active buzzer
        lcd.display_on()  #Turns on LCD
        utime.sleep(0.25) #Pause

Post Reply