import RPi.GPIO as GPIO
import time
import Adafruit_SSD1306
from PIL import Image, ImageDraw, ImageFont
# Set up GPIO pins
GPIO.setmode(GPIO.BCM)
red_led = 17
yellow_led = 23
green_led = 24
GPIO.setup(red_led, GPIO.OUT)
GPIO.setup(yellow_led, GPIO.OUT)
GPIO.setup(green_led, GPIO.OUT)
# Set up OLED display
RST = None
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
disp.begin()
disp.clear()
disp.display()
# Create image and drawing objects
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
draw = ImageDraw.Draw(image)
font = ImageFont.load_default()
def display_text(text):
draw.rectangle((0, 0, width, height), outline=0, fill=0)
draw.text((0, 0), text, font=font, fill=255)
disp.image(image)
disp.display()
try:
while True:
# Red light
GPIO.output(red_led, GPIO.HIGH)
GPIO.output(yellow_led, GPIO.LOW)
GPIO.output(green_led, GPIO.LOW)
display_text("RED LIGHT")
time.sleep(5)
# Green light
GPIO.output(red_led, GPIO.LOW)
GPIO.output(yellow_led, GPIO.LOW)
GPIO.output(green_led, GPIO.HIGH)
display_text("GREEN LIGHT")
time.sleep(5)
# Yellow light
GPIO.output(red_led, GPIO.LOW)
GPIO.output(yellow_led, GPIO.HIGH)
GPIO.output(green_led, GPIO.LOW)
display_text("YELLOW LIGHT")
time.sleep(2)
except KeyboardInterrupt:
GPIO.cleanup()
disp.clear()
disp.display()Loading
pi-pico-w
pi-pico-w