from machine import Pin
import time
class LiquidCrystal:
def __init__(self, rs, e, d4, d5, d6, d7):
self.rs = Pin(rs, Pin.OUT)
self.e = Pin(e, Pin.OUT)
self.d4 = Pin(d4, Pin.OUT)
self.d5 = Pin(d5, Pin.OUT)
self.d6 = Pin(d6, Pin.OUT)
self.d7 = Pin(d7, Pin.OUT)
self.init_lcd()
def pulse_e(self):
self.e.value(0)
time.sleep_us(1) # enable pulse must be >450ns
self.e.value(1)
time.sleep_us(1) # enable cycle must be >500ns
self.e.value(0)
time.sleep_us(100) # commands need >37us to settle
def send(self, data, mode=False):
self.rs.value(mode) # False for command, True for data
# High bits
self.d4.value(bool(data & 0x10))
self.d5.value(bool(data & 0x20))
self.d6.value(bool(data & 0x40))
self.d7.value(bool(data & 0x80))
self.pulse_e()
# Low bits
self.d4.value(bool(data & 0x01))
self.d5.value(bool(data & 0x02))
self.d6.value(bool(data & 0x04))
self.d7.value(bool(data & 0x08))
self.pulse_e()
def init_lcd(self):
self.send(0x33, False) # Initialize
self.send(0x32, False) # Set to 4-bit mode
self.send(0x28, False) # 2 line, 5x7 matrix
self.send(0x0C, False) # Turn cursor off
self.send(0x06, False) # Shift cursor right
self.clear()
def clear(self):
self.send(0x01, False) # Clear display
time.sleep_ms(2)
def move_to(self, row, col):
# LCD address
# 0x00 for the first line
# 0x40 for the second line
addr = col + (0x40 if row else 0x00)
self.send(0x80 | addr, False)
def printstr(self, string):
for char in string:
self.send(ord(char), True)
#Pines LCD
rs = 13
e = 12
d4 = 18
d5 = 5
d6 = 2
d7 = 15
# Initialize LCD
lcd = LiquidCrystal(rs, e, d4, d5, d6, d7)
# Pines
verdeIzq = Pin(14, Pin.OUT)
amarilloIzq = Pin(27, Pin.OUT)
rojoIzq = Pin(25, Pin.OUT)
peatonalVerdeIzq = Pin(25, Pin.OUT)
peatonalRojoIzq = Pin(26, Pin.OUT)
verdeDer = Pin(19, Pin.OUT)
amarilloDer = Pin(21, Pin.OUT)
rojoDer = Pin(23, Pin.OUT)
peatonalVerdeDer = Pin(23, Pin.OUT)
peatonalRojoDer = Pin(22, 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
lcd.clear()
lcd.move_to(0, 0)
lcd.printstr("Der auto verde")
lcd.move_to(1, 0)
lcd.printstr("Izq auto rojo")
amarilloIzq.off()
peatonalRojoIzq.off()
rojoIzq.on()
peatonalVerdeIzq.on()
peatonalRojoDer.on()
verdeDer.on()
peatonalVerdeDer.off()
rojoDer.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
lcd.clear()
lcd.move_to(0, 0)
lcd.printstr("Der auto amarill")
lcd.move_to(1, 0)
lcd.printstr("Izq auto rojo")
amarilloIzq.off()
peatonalRojoIzq.off()
rojoIzq.on()
peatonalVerdeIzq.on()
peatonalRojoDer.on()
amarilloDer.on()
verdeDer.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
lcd.clear()
lcd.move_to(0, 0)
lcd.printstr("Der auto amarill")
lcd.move_to(1, 0)
lcd.printstr("Izq auto rojo")
amarilloIzq.off()
peatonalRojoIzq.off()
rojoIzq.on()
peatonalVerdeIzq.on()
peatonalRojoDer.on()
amarilloDer.on()
verdeDer.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
lcd.clear()
lcd.move_to(0, 0)
lcd.printstr("Der auto rojo")
lcd.move_to(1, 0)
lcd.printstr("Izq auto verde")
rojoIzq.off()
peatonalVerdeIzq.off()
peatonalRojoIzq.on()
verdeIzq.on()
peatonalRojoDer.off()
amarilloDer.off()
peatonalVerdeDer.on()
rojoDer.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
lcd.clear()
lcd.move_to(0, 0)
lcd.printstr("Der auto rojo")
lcd.move_to(1, 0)
lcd.printstr("Izq auto amarill")
peatonalRojoIzq.on()
verdeIzq.off()
amarilloIzq.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
lcd.clear()
lcd.move_to(0, 0)
lcd.printstr("Der auto rojo")
lcd.move_to(1, 0)
lcd.printstr("Izq auto amarill")
peatonalRojoIzq.on()
verdeIzq.off()
amarilloIzq.on()
tiempo_inicial = time.time()
while (time.time() - tiempo_inicial) < 1: # Bucle durante 4 segundos
pass # En este ejemplo, el bucle no hace nada