Implement function to move the table up or down for a distance of centimeters

master
Elis Axelsson 5 years ago
parent 2fb13b33cb
commit 278c97637f
  1. 40
      code/table.py

@ -1,6 +1,7 @@
from pinouts import NodeMCU as pins
from hcsr04 import HCSR04
import machine
import time
class Table:
@ -57,3 +58,42 @@ class Table:
else:
self.relay_up.value(1)
self.relay_down.value(1)
def move(self, distance):
"""
Move distance centimeters:
- Positive distances is up
- Negative distances is down
- Zero distances is stop
"""
# Calculate target height
targetHeight = self.currentHeight() + distance
# Default to not move in any direction
direction = 0
while True:
# Calculate how far to move
currentHeightDiff = targetHeight - self.currentHeight()
# Calculate which direction to move in
if currentHeightDiff > 1:
direction = 1
elif currentHeightDiff < -1:
direction = -1
else:
# Reset the relays
self.triggerRelay(0)
# Break the loop
break
# Move up or down
self.triggerRelay(direction)
# Sleep for a bit
time.sleep(0.1)
print("my work is done")

Loading…
Cancel
Save