added code for timer pushbutton
This commit is contained in:
140
SW/RPi_Pico_Button/main.py
Normal file
140
SW/RPi_Pico_Button/main.py
Normal file
@@ -0,0 +1,140 @@
|
||||
import neopixel
|
||||
import time
|
||||
import sys
|
||||
import select
|
||||
from machine import Pin, SPI
|
||||
|
||||
#neopixel
|
||||
ws_pin = 2
|
||||
led_num = 10
|
||||
BRIGHTNESS = 0.2 # Adjust the brightness (0.0 - 1.0)
|
||||
|
||||
led_strip = neopixel.NeoPixel(Pin(ws_pin), led_num)
|
||||
|
||||
push_button = Pin(24,Pin.IN,Pin.PULL_UP)
|
||||
|
||||
pushbutton_pressed = 0
|
||||
blink_interrupted = 0
|
||||
|
||||
def set_neopixel_brightness(color):
|
||||
r, g, b = color
|
||||
r = int(r * BRIGHTNESS)
|
||||
g = int(g * BRIGHTNESS)
|
||||
b = int(b * BRIGHTNESS)
|
||||
return (r, g, b)
|
||||
|
||||
def set_neopixel_color(r,g,b):
|
||||
color = (r,g,b)
|
||||
color = set_neopixel_brightness(color)
|
||||
led_strip.fill(color)
|
||||
led_strip.write()
|
||||
|
||||
set_neopixel_color(0,0,0)
|
||||
|
||||
|
||||
|
||||
def loop_neopixel():
|
||||
# Display red
|
||||
color = (255, 0, 0) # Red color
|
||||
color = set_neopixel_brightness(color)
|
||||
led_strip.fill(color)
|
||||
led_strip.write()
|
||||
time.sleep(1)
|
||||
|
||||
# Display green
|
||||
color = (0, 255, 0) # Green color
|
||||
color = set_neopixel_brightness(color)
|
||||
led_strip.fill(color)
|
||||
led_strip.write()
|
||||
time.sleep(1)
|
||||
|
||||
# Display blue
|
||||
color = (0, 0, 255) # Blue color
|
||||
color = set_neopixel_brightness(color)
|
||||
led_strip.fill(color)
|
||||
led_strip.write()
|
||||
time.sleep(1)
|
||||
|
||||
neopix_loop_dot_pos = 0
|
||||
def neopixel_loop_dot():
|
||||
global neopix_loop_dot_pos
|
||||
color = (255, 0, 0)
|
||||
black = (0, 0, 0)
|
||||
|
||||
color = set_neopixel_brightness(color)
|
||||
led_strip[neopix_loop_dot_pos] = black
|
||||
neopix_loop_dot_pos += 1
|
||||
if neopix_loop_dot_pos > 9:
|
||||
neopix_loop_dot_pos = 0
|
||||
led_strip[neopix_loop_dot_pos] = color
|
||||
led_strip.write()
|
||||
time.sleep(0.2)
|
||||
|
||||
def lightup_led(index, color):
|
||||
led_strip[index] = color
|
||||
led_strip.write()
|
||||
|
||||
def blink_and_beep(times):
|
||||
global pushbutton_pressed, blink_interrupted
|
||||
|
||||
blink_delay = 0.2
|
||||
|
||||
#insert beep
|
||||
for i in range(0, times):
|
||||
set_neopixel_color(255, 0, 0)
|
||||
time.sleep(blink_delay)
|
||||
set_neopixel_color(0, 0, 0)
|
||||
time.sleep(blink_delay)
|
||||
if pushbutton_pressed == 1:
|
||||
pushbutton_pressed = 0
|
||||
blink_interrupted = 1
|
||||
i = times
|
||||
return
|
||||
|
||||
ten_min_timer = 0
|
||||
ten_min_timer_led_index = 0
|
||||
def count_10_min():
|
||||
global ten_min_timer, ten_min_timer_led_index, pushbutton_pressed, blink_interrupted
|
||||
|
||||
red = (255, 0, 0)
|
||||
red = set_neopixel_brightness(red)
|
||||
green = (0, 255, 0)
|
||||
green = set_neopixel_brightness(green)
|
||||
|
||||
if ten_min_timer_led_index == 11:
|
||||
return
|
||||
|
||||
if (ten_min_timer % 60) == 0:
|
||||
if ten_min_timer_led_index == 10:
|
||||
pushbutton_pressed = 0
|
||||
blink_and_beep(60)
|
||||
if blink_interrupted == 1:
|
||||
blink_interrupted = 0
|
||||
return
|
||||
ten_min_timer_led_index = 11
|
||||
led_strip[0] = green
|
||||
led_strip[2] = green
|
||||
led_strip[4] = green
|
||||
led_strip[6] = green
|
||||
led_strip[8] = green
|
||||
led_strip.write()
|
||||
return
|
||||
lightup_led(ten_min_timer_led_index, red)
|
||||
ten_min_timer_led_index += 1
|
||||
|
||||
ten_min_timer += 1
|
||||
|
||||
def button_callback(push_button):
|
||||
global ten_min_timer, ten_min_timer_led_index, pushbutton_pressed
|
||||
pushbutton_pressed = 1
|
||||
ten_min_timer = 0
|
||||
ten_min_timer_led_index = 0
|
||||
set_neopixel_color(0,0,0)
|
||||
count_10_min()
|
||||
|
||||
set_neopixel_color(0,0,0)
|
||||
push_button.irq(trigger=Pin.IRQ_FALLING, handler=button_callback)
|
||||
|
||||
while True:
|
||||
count_10_min()
|
||||
time.sleep(1)
|
||||
Reference in New Issue
Block a user