rpi_assignments/assignment_1/assignment_1.py

44 lines
951 B
Python
Raw Normal View History

2023-10-12 04:32:41 +00:00
#! /usr/bin/python
import RPi.GPIO as GPIO
import time
global blinking, blink_count
blinking: bool = False
led_state: bool = False
blink_count: int = 0
LED_PIN = 2
BTN_PIN = 3
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(LED_PIN ,GPIO.OUT)
GPIO.setup(BTN_PIN,GPIO.IN, pull_up_down = GPIO.PUD_UP)
blinking = not GPIO.input(BTN_PIN)
def ISR_DECT_BOTH(channel: int):
global blinking, blink_count
time.sleep(0.05)
blinking = not GPIO.input(BTN_PIN)
if(blinking):
blink_count = 0
GPIO.add_event_detect(BTN_PIN, GPIO.BOTH, callback = ISR_DECT_BOTH, bouncetime = 50)
try:
while(1):
print(blinking)
if(blinking):
if led_state == 0:
blink_count += 1
if blink_count <= 3:
led_state = not led_state
else:
led_state = False
GPIO.output(LED_PIN, led_state)
time.sleep(1)
except:
GPIO.cleanup()