Pi Pico - 6 Buttons with a LCD
Fritzing File |
from machine import Pin from lcd_api import LcdApi from pico_i2c_lcd import I2cLcd # GP10 - Orange Wire # GP11 - Red Wire # GP12 - Yellow Wire # GP13 - Blue Wire # GP14 - Black Wire # GP15 - Green Wire orangeButton = machine.Pin( 10, machine.Pin.IN, machine.Pin.PULL_DOWN ) redButton = machine.Pin( 11, machine.Pin.IN, machine.Pin.PULL_DOWN ) yellowButton = machine.Pin( 12, machine.Pin.IN, machine.Pin.PULL_DOWN ) blueButton = machine.Pin( 13, machine.Pin.IN, machine.Pin.PULL_DOWN ) blackButton = machine.Pin( 14, machine.Pin.IN, machine.Pin.PULL_DOWN ) greenButton = machine.Pin( 15, machine.Pin.IN, machine.Pin.PULL_DOWN ) I2C_ADDR = 0x27 I2C_NUM_ROWS = 2 I2C_NUM_COLS = 16 SDA = machine.Pin(0) SCL = machine.Pin(1) i2c = machine.I2C( 0, sda = SDA, scl = SCL, freq = 400000 ) lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS) currOrangeButtonState = 0 prevOrangeButtonState = 0 counterOrange = 0 currRedButtonState = 0 prevRedButtonState = 0 counterRed = 0 currYellowButtonState = 0 prevYellowButtonState = 0 counterYellow = 0 currBlueButtonState = 0 prevBlueButtonState = 0 counterBlue = 0 currBlackButtonState = 0 prevBlackButtonState = 0 counterBlack = 0 currGreenButtonState = 0 prevGreenButtonState = 0 counterGreen = 0 while True: currOrangeButtonState = orangeButton.value() if ( prevOrangeButtonState == 0 ) and ( currOrangeButtonState == 1 ): counterOrange += 1 print("Orange Button Pressed!", counterOrange) lcd.clear() lcd.putstr("Orange Button Pressed!") prevOrangeButtonState = currOrangeButtonState currRedButtonState = redButton.value() if ( prevRedButtonState == 0 ) and ( currRedButtonState == 1 ): counterRed += 1 print("Red Button Pressed!", counterRed) lcd.clear() lcd.putstr("Red Button Pressed!") prevRedButtonState = currRedButtonState currYellowButtonState = yellowButton.value() if ( prevYellowButtonState == 0 ) and ( currYellowButtonState == 1 ): counterYellow += 1 print("Yellow Button Pressed!", counterYellow) lcd.clear() lcd.putstr("Yellow Button Pressed!") prevYellowButtonState = currYellowButtonState currBlueButtonState = blueButton.value() if ( prevBlueButtonState == 0 ) and ( currBlueButtonState == 1 ): counterBlue += 1 print("Blue Button Pressed!", counterBlue) lcd.clear() lcd.putstr("Blue Button Pressed!") prevBlueButtonState = currBlueButtonState currBlackButtonState = blackButton.value() if ( prevBlackButtonState == 0 ) and ( currBlackButtonState == 1 ): counterBlack += 1 print("Black Button Pressed!", counterBlack) lcd.clear() lcd.putstr("Black Button Pressed!") prevBlackButtonState = currBlackButtonState currGreenButtonState = greenButton.value() if ( prevGreenButtonState == 0 ) and ( currGreenButtonState == 1 ): counterGreen += 1 print("Green Button Pressed!", counterGreen) lcd.clear() lcd.putstr("Green Button Pressed!") prevGreenButtonState = currGreenButtonState