Edit Assignment 1

This commit is contained in:
Siwat Sirichai 2023-10-12 05:29:07 +01:00
parent 138f84efb6
commit 95d6e2dfda
1 changed files with 29 additions and 22 deletions

51
Assignment 1/assignment_1.py Normal file → Executable file
View File

@ -1,37 +1,44 @@
#! /usr/bin/python
import RPi.GPIO as GPIO
import time
PIR_pin = 3
LED_pin = 4
global blinking, blink_count
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED_pin ,GPIO.OUT) # Set up pin to emit LED
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(PIR_pin, pull_up_down = GPIO.PUD_UP) # Set up pin 23 pull up
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 PIN_pin_ISR(pressed):
print("Falling Edge Detected")
'''
def Blink_3_times(pressed):
i = 0
while(i<=2):
GPIO.output(LED_pin, GPIO_HIGH)
time.sleep(1)
GPIO.output(LED_pin, GPIO_LOW)
time.sleep(1)
print("LED Blink")
GPIO.add_event_detect(PIR_PIN, GPIO.FALLING, callback = Blink_3_times, bouncetime = 300)
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("Nothing")
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()