Skip to content

Set the Mode Buttons

We need a way to monitor the Set/Mode, Increment and Decrement buttons to manually set the time. Here is a program that will monitor the three buttons and change the mode.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from utime import sleep, ticks_ms, ticks_diff
from machine import Pin

# Global Variable Default Settings
mode = 0
hour = 6
minute = 30
pm_indicator = True
mode_names = ['run', 'set hour', 'set minute', 'set AM/PM']
mode_count = len(mode_names)

# Button Pin GPIO Configuration
CHANGE_MODE_PIN = 13
INCREMENT_PIN = 14
DECREMENT_PIN = 15
DEBOUNCE_TIME_MS = 200

# Create 3 button objects as inputs with pullup resistors
change_mode_pin = Pin(CHANGE_MODE_PIN, Pin.IN, Pin.PULL_UP)
increment_pin = Pin(INCREMENT_PIN, Pin.IN, Pin.PULL_UP)
decrement_pin = Pin(DECREMENT_PIN, Pin.IN, Pin.PULL_UP)

# Variables to track last button press times for debouncing
last_mode_press = 0
last_increment_press = 0
last_decrement_press = 0

# These functions gets called every time a button is pressed
def mode_button_pressed(pin):
    global mode, last_mode_press
    current_time = ticks_ms()
    if ticks_diff(current_time, last_mode_press) > DEBOUNCE_TIME_MS:
        mode += 1
        mode = mode % mode_count
        last_mode_press = current_time

def increment_button_pressed(pin):
    global mode, hour, minute, pm_indicator, last_increment_press
    current_time = ticks_ms()
    if ticks_diff(current_time, last_increment_press) > DEBOUNCE_TIME_MS:
        if mode == 1:
            hour = (hour % 12) + 1  # Increment hour from 1-12 properly
        if mode == 2:
            minute += 1
            minute = minute % 60  # Fixed: minute should go from 0 to 59
        if mode == 3:
            pm_indicator = not pm_indicator
        last_increment_press = current_time

def decrement_button_pressed(pin):
    global mode, hour, minute, pm_indicator, last_decrement_press
    current_time = ticks_ms()
    if ticks_diff(current_time, last_decrement_press) > DEBOUNCE_TIME_MS:
        if mode == 1:
            hour -= 1
            if hour <= 0:  # Handle wrapping from 1 to 12
                hour = 12
        if mode == 2:
            minute -= 1
            if minute < 0:  # Handle wrapping from 0 to 59
                minute = 59
        if mode == 3:
            pm_indicator = not pm_indicator
        last_decrement_press = current_time

# Register the button handler functions using the irq setter method for each pin
change_mode_pin.irq(trigger=Pin.IRQ_FALLING, handler=mode_button_pressed)
increment_pin.irq(trigger=Pin.IRQ_FALLING, handler=increment_button_pressed)
decrement_pin.irq(trigger=Pin.IRQ_FALLING, handler=decrement_button_pressed)

# Only print on change - these variables store the old values
last_mode = mode
last_hour = hour
last_minute = minute
last_pm_indicator = pm_indicator
print("Default values: mode:", mode, "hour:", hour, "minute:", minute, "pm indicator:", pm_indicator)

while True:
    if mode != last_mode:
        print("new mode:", mode, mode_names[mode])
        last_mode = mode
    if hour != last_hour:
        print("new hour:", hour)
        last_hour = hour
    if minute != last_minute:
        print("new minute:", minute)
        last_minute = minute
    if pm_indicator != last_pm_indicator:
        print("new pm indicator:", pm_indicator)
        last_pm_indicator = pm_indicator
    # sleep(.01)