from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
from time import sleep
from machine import Pin
import time
# Define the I2C pins (you can adjust these pins based on your wiring)
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000) # Use your own pin numbers
# You may need to adjust the frequency (freq) based on your display's specifications.
# Initialize the OLED display with its dimensions (128x64) and the I2C object
oled = SSD1306_I2C(128, 64, i2c)
# Clear the display
oled.fill(0)
# Delay to allow time to see the text (optional)
#sleep(5)
# Turn off the OLED display
#oled.poweroff()
# Pines
GLeft = Pin(14, Pin.OUT)
YLeft = Pin(27, Pin.OUT)
RLeft = Pin(25, Pin.OUT)
peatonalGLeft = Pin(25, Pin.OUT)
peatonalRLeft = Pin(26, Pin.OUT)
GRight = Pin(19, Pin.OUT)
YRight = Pin(2, Pin.OUT) #not 2
RRight = Pin(18, Pin.OUT) # not 18
peatonalGRight = Pin(23, Pin.OUT)
peatonalRRight = Pin(5, Pin.OUT)
botonIzq = Pin(4, Pin.IN)
botonDer = Pin(32, Pin.IN)
tiempo_inicial = time.time() # Tiempo inicial
CambioRojoVerde = 10 # Tiempo que cambia el semaforo entre rojo y verde
CambioPeatonal = 2 # Tiempo a esperar para el cambio por peatones
x=1
while True:
if x==1:
x=10
# Set the text position and font size
oled.fill(0)
oled.text("Right Green", 10, 0)
oled.text("Left Red", 10, 20)
# Update the display to show the text
oled.show()
YLeft.off()
peatonalRLeft.off()
RLeft.on()
peatonalGLeft.on()
peatonalRRight.on()
GRight.on()
peatonalGRight.off()
RRight.off()
tiempo_inicial = time.time()
while (time.time() - tiempo_inicial) < 4: # Bucle durante 4 segundos
if botonIzq() == 1: # Si el botón está presionado (valor bajo)
x=9
break
pass # En este ejemplo, el bucle no hace nada
if x==9:
x=2
# Set the text position and font size
oled.fill(0)
oled.text("Right Yellow", 10, 0)
oled.text("Left Red", 10, 20)
# Update the display to show the text
oled.show()
YLeft.off()
peatonalRLeft.off()
RLeft.on()
peatonalGLeft.on()
peatonalRRight.on()
YRight.on()
GRight.off()
tiempo_inicial = time.time()
while (time.time() - tiempo_inicial) < 2: # Bucle durante 4 segundos
pass # En este ejemplo, el bucle no hace nada
if x==10:
x=2
# Set the text position and font size
oled.fill(0)
oled.text("Right Yellow", 10, 0)
oled.text("Left Red", 10, 20)
# Update the display to show the text
oled.show()
YLeft.off()
peatonalRLeft.off()
RLeft.on()
peatonalGLeft.on()
peatonalRRight.on()
YRight.on()
GRight.off()
tiempo_inicial = time.time()
while (time.time() - tiempo_inicial) < 4: # Bucle durante 4 segundos
pass # En este ejemplo, el bucle no hace nada
if x==2:
x=3
# Set the text position and font size
oled.fill(0)
oled.text("Right Red", 10, 0)
oled.text("Left Green", 10, 20)
# Update the display to show the text
oled.show()
RLeft.off()
peatonalGLeft.off()
peatonalRLeft.on()
GLeft.on()
peatonalRRight.off()
YRight.off()
peatonalGRight.on()
RRight.on()
tiempo_inicial = time.time()
while (time.time() - tiempo_inicial) < 4: # Bucle durante 4 segundos
if botonDer() == 1: # Si el botón está presionado (valor bajo)
x=4
break
pass # En este ejemplo, el bucle no hace nada
if x==3:
x=1
# Set the text position and font size
oled.fill(0)
oled.text("Right Red", 10, 0)
oled.text("Left Yellow.", 10, 20)
# Update the display to show the text
oled.show()
peatonalRLeft.on()
GLeft.off()
YLeft.on()
tiempo_inicial = time.time()
while (time.time() - tiempo_inicial) < 4: # Bucle durante 4 segundos
pass # En este ejemplo, el bucle no hace nada
if x==4:
x=1
# Set the text position and font size
oled.fill(0)
oled.text("Right Red", 10, 0)
oled.text("Left Yellow", 10, 20)
# Update the display to show the text
oled.show()
peatonalRLeft.on()
GLeft.off()
YLeft.on()
tiempo_inicial = time.time()
while (time.time() - tiempo_inicial) < 1: # Bucle durante 4 segundos
pass # En este ejemplo, el bucle no hace nada