Skip to content

Instantly share code, notes, and snippets.

@Heinrich-XIAO
Created May 31, 2022 14:30
Show Gist options
  • Save Heinrich-XIAO/25ba11316709572a15705161c1a31ff2 to your computer and use it in GitHub Desktop.
Save Heinrich-XIAO/25ba11316709572a15705161c1a31ff2 to your computer and use it in GitHub Desktop.
Some micropython code for an elevator
from machine import PWM, Pin
from utime import sleep, sleep_us, ticks_ms
class StepperMotor:
def __init__(self, ins):
self.ins = [Pin(ins[0], Pin.OUT), Pin(ins[1], Pin.OUT), Pin(ins[2], Pin.OUT), Pin(ins[3], Pin.OUT)]
def step(self, direction):
r = [3, 2, 1, 0] if (not direction) else [0, 1, 2, 3]
for i in r:
for j in range(4):
self.ins[j].value(0)
self.ins[i].value(1)
sleep_us(2500)
class PosSen:
def __init__(self, p):
self.s = Pin(p, Pin.IN, Pin.PULL_UP)
def get(self):
return self.s.value() ^ 1
class Button:
def __init__(self, p):
self.p = Pin(p, Pin.IN, Pin.PULL_UP)
def pushed(self):
return self.p.value() == 0
ps1s = [PosSen(8),PosSen(7)]
sm = StepperMotor([0, 1, 4, 3])
floors = [Button(11), Button(12), Button(14), Button(13)]
floorheight = 26.5/len(floors)
circum = 6.283185307*2.26
steps360 = 8188
people = [False, False, False, False]
direction = 1
while ps1s[0].get()==0:
sm.step(True)
floorNum = len(floors)
inp = input("Mode: ")
if inp == "1":
while True:
pushed = None
for i in range(len(floors)+1):
if i == len(floors):
break
if floors[i].pushed():
print(i+1)
pushed = i
break
if pushed != None:
people[pushed] = True
if any(people):
goto = direction + floorNum
if goto > len(floors):
goto = len(floors)-1
direction = -1
if goto < 1:
goto = 2
direction = 1
if goto == 1:
while ps1s[1].get()==0:
pushed = None
for i in range(len(floors)+1):
if i == len(floors):
break
if floors[i].pushed():
print(i+1)
pushed = i+1
break
if pushed != None:
people[pushed-1] = True
sm.step(False)
floorNum = 1
elif goto == len(floors):
while ps1s[0].get()==0:
pushed = None
for i in range(len(floors)+1):
if i == len(floors):
break
if floors[i].pushed():
print(i+1)
pushed = i+1
break
if pushed != None:
people[pushed-1] = True
sm.step(True)
floorNum = goto
else:
print(floorNum, goto)
steps = steps360/circum*(floorNum-goto)
for i in range(abs(steps)):
p = None
for i in range(len(floors)+1):
if i == len(floors):
break
if floors[i].pushed():
print(i+1)
p = i+1
break
if p != None:
people[p-1] = True
if steps < 0:
sm.step(True)
else:
sm.step(False)
print(people, goto,floorNum)
floorNum = goto
if people[goto-1]==True:
sleep(1)
people[goto-1] = False
print(people, goto,floorNum)
if inp == "2":
while True:
pushed = None
for i in range(len(floors)+1):
if i == len(floors):
break
if floors[i].pushed():
print(i+1)
pushed = i+1
break
if pushed!=None:
if pushed == 1:
while ps1s[1].get()==0:
sm.step(False)
floorNum = 1
elif pushed == len(floors):
while ps1s[0].get()==0:
sm.step(True)
floorNum = pushed
else:
print(floorNum, pushed)
steps = steps360/circum*(floorNum-pushed)
for i in range(abs(steps)):
if steps < 0:
sm.step(True)
else:
sm.step(False)
floorNum = pushed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment