Coding Practice
Coding Practice
1. Flashing LED
import time
LED_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
try:
while True:
GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(1)
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
PART B
import time
RED = 18
YELLOW = 23
GREEN = 24
BUZZER = 12
BUTTON = 25
GPIO.setmode(GPIO.BCM)
GPIO.setup(RED, GPIO.OUT)
GPIO.setup(YELLOW, GPIO.OUT)
GPIO.setup(GREEN, GPIO.OUT)
GPIO.setup(BUZZER, GPIO.OUT)
def reset_leds():
GPIO.output(RED, GPIO.LOW)
GPIO.output(YELLOW, GPIO.LOW)
GPIO.output(GREEN, GPIO.LOW)
try:
while True:
if GPIO.input(BUTTON) == GPIO.HIGH:
GPIO.output(RED, GPIO.HIGH)
GPIO.output(BUZZER, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(BUZZER, GPIO.LOW)
time.sleep(1)
GPIO.output(YELLOW, GPIO.HIGH)
time.sleep(1)
GPIO.output(GREEN, GPIO.HIGH)
time.sleep(1)
reset_leds()
time.sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()