import machine
from machine import Pin, Timer
import time
from time import sleep
# Configuración del motor paso a paso
QB0 = Pin(13, Pin.OUT)
QB1 = Pin(9, Pin.OUT)
QA0 = Pin(10, Pin.OUT)
QA1 = Pin(11, Pin.OUT)
WB0 = Pin(26, Pin.OUT)
WB1 = Pin(27, Pin.OUT)
WA0 = Pin(14, Pin.OUT)
WA1 = Pin(12, Pin.OUT)
EB0 = Pin(21, Pin.OUT)
EB1 = Pin(22, Pin.OUT)
EA0 = Pin(23, Pin.OUT)
EA1 = Pin(32, Pin.OUT)
RB0 = Pin(17, Pin.OUT)
RB1 = Pin(5, Pin.OUT)
RA0 = Pin(18, Pin.OUT)
RA1 = Pin(19, Pin.OUT)
step_sequence = [
[1, 0, 1, 0],
[0, 1, 1, 0],
[0, 1, 0, 1],
[1, 0, 0, 1]
]
def set_stepQ(pins):
QB0.value(pins[0])
QB1.value(pins[1])
QA0.value(pins[2])
QA1.value(pins[3])
def set_stepW(pins):
WB0.value(pins[0])
WB1.value(pins[1])
WA0.value(pins[2])
WA1.value(pins[3])
def set_stepE(pins):
EB0.value(pins[0])
EB1.value(pins[1])
EA0.value(pins[2])
EA1.value(pins[3])
def set_stepR(pins):
RB0.value(pins[0])
RB1.value(pins[1])
RA0.value(pins[2])
RA1.value(pins[3])
def step_motor(steps, delay=0.01):
step_count = len(step_sequence)
step_dir = 1 if steps > 0 else -1
steps = abs(steps)
for _ in range(steps):
for step in step_sequence[::step_dir]:
set_stepQ(step)
set_stepW(step)
set_stepE(step)
set_stepR(step)
time.sleep(delay)
def step_motorQ(steps, delay=0.01):
step_count = len(step_sequence)
step_dir = 1 if steps > 0 else -1
steps = abs(steps)
for _ in range(steps):
for step in step_sequence[::step_dir]:
set_stepQ(step)
time.sleep(delay)
def step_motorW(steps, delay=0.01):
step_count = len(step_sequence)
step_dir = 1 if steps > 0 else -1
steps = abs(steps)
for _ in range(steps):
for step in step_sequence[::step_dir]:
set_stepW(step)
time.sleep(delay)
def step_motorE(steps, delay=0.01):
step_count = len(step_sequence)
step_dir = 1 if steps > 0 else -1
steps = abs(steps)
for _ in range(steps):
for step in step_sequence[::step_dir]:
set_stepE(step)
time.sleep(delay)
def step_motorR(steps, delay=0.01):
step_count = len(step_sequence)
step_dir = 1 if steps > 0 else -1
steps = abs(steps)
for _ in range(steps):
for step in step_sequence[::step_dir]:
set_stepR(step)
time.sleep(delay)
Key_Up = const(0)
Key_Down = const(1)
Numpad = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['*', '0', '#']]
Rows = [16, 4, 0, 2]
Cols = [15, 8, 7]
pines_Rows = [Pin(pin_name, mode = Pin.OUT) for pin_name in Rows]
pines_Cols = [Pin(pin_name, mode = Pin.IN, pull = Pin.PULL_DOWN) for pin_name in Cols]
def Start():
for Row in range(0, 4):
for Col in range(0, 3):
pines_Rows[Row].value(0)
def Scan(Row, Col):
#Escaneo de teclado
pines_Rows[Row].value(1)
key = None
if pines_Cols[Col].value() == Key_Down:
key = Key_Down
if pines_Cols[Col].value() == Key_Up:
key = Key_Up
pines_Rows[Row].value(0)
return key
State = 0
Imput = 0
def FSM(State, Imput):
if State == 0 and Imput == 1:
return 1
if State == 1 and Imput == 1:
return 2
if State == 2 and Imput == 1:
return 3
if State == 3 and Imput == 1:
return 4
if State == 4 and Imput == 1:
return 5
if State == 5 and Imput == 1:
return 6
return 0
def Data(State, last_key_press):
if State == 0 and last_key_press == '*':
return 1
if State == 1 and last_key_press == '1':
return 1
if State == 2 and last_key_press == '2':
return 1
if State == 3 and last_key_press == '1':
return 1
if State == 4 and last_key_press == '2':
return 1
if State == 5 and last_key_press == '#':
return 1
return 0
Motion = Pin(25, Pin.IN, Pin.PULL_UP)
Buzzer = Pin(33, Pin.OUT)
cont = 0
print("iniciando .............Presione una tecla:")
Start()
while True:
Sensor = Motion.value() #or 1
if Sensor:
for Row in range(4):
for Col in range(3):
Key = Scan(Row, Col)
if Key == Key_Down:
last_key_press = Numpad[Row][Col]
if last_key_press == '#':
cont = cont + 1
Imput = Data(State, last_key_press)
State = FSM(State, Imput)
if State == 6:
cont = 0
print("LA PUERTA SE ESTA ABRIENDO...")
step_motorQ(150)
print("25%")
step_motorW(150)
print("50%")
step_motorE(150)
print("75%")
step_motorR(150)
print("100%")
sleep(0.2)
print("PUERTA ABIERTA")
sleep(15)
print("LA PUERTA COMENZARA A CERRARSE EN:")
sleep(0.2)
print("5")
sleep(1)
print("4")
sleep(1)
print("3")
sleep(1)
print("2")
sleep(1)
print("1")
sleep(1)
step_motor(-150)
print("PUERTA CERRADA")
State = 0
sleep(0.2)
while cont > 2:
Buzzer.value(1)
Buzzer.value(0)