import time
from machine import Pin, I2C
from esp8266_i2c_lcd import I2cLcd
# --- Initialize LCD ---
# Uses Hardware I2C(0) on pins 8 and 9 (Wokwi default address is 0x27)
i2c = I2C(0, scl=Pin(8), sda=Pin(9), freq=100000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
p4 = Pin(4, Pin.IN) # Button 1 (Increment)
p5 = Pin(5, Pin.IN) # Button 2 (Decrement)
p6 = Pin(6, Pin.IN) # ◄─── New Reset Button
l1 = Pin(1, Pin.OUT)
l2 = Pin(2, Pin.OUT)
count = 0
prev_val4 = 1
prev_val5 = 1
prev_val6 = 1 # ◄─── Track previous state for the Reset Button
print("Monitoring Buttons")
lcd.clear()
lcd.putstr("Monitoring\nButtons") # Boot-up text on LCD
while True:
val4 = p4.value()
val5 = p5.value()
val6 = p6.value() # ◄─── Read current state of the Reset Button
# --- Button 1: Increment ---
if val4 == 0 and prev_val4 == 1:
count += 1
message = f"Total Count: {count}"
print(message)
lcd.clear()
lcd.putstr(message)
# --- Button 2: Decrement ---
if val5 == 0 and prev_val5 == 1:
count -= 1
message = f"Total Count: {count}"
print(message)
lcd.clear()
lcd.putstr(message)
# --- New Reset Button Logic ---
if val6 == 0 and prev_val6 == 1:
count = 0 # Reset count back to zero
message = f"Total Count: {count}"
print("[SYSTEM RESET]")
print(message)
# Clear the display and update to show the reset value
lcd.clear()
lcd.putstr("[SYSTEM RESET]\n" + message)
# Update edge histories for all buttons
prev_val4 = val4
prev_val5 = val5
prev_val6 = val6 # ◄─── Save history for Reset Button
# --- LED Indicators ---
if val4 == 0:
l1.value(1)
else:
l1.value(0)
if val5 == 0:
l2.value(1)
else:
l2.value(0)
time.sleep(0.03)