You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.4 KiB

4 years ago
from pinouts import NodeMCU as pins
from hcsr04 import HCSR04
import machine
class Table:
def __init__(self):
# Distance sensor
self.hcsr04 = HCSR04(trigger_pin=pins.D2, echo_pin=pins.D3)
# Movement sensor
self.movement_sensor = machine.Pin(pins.D4, mode=machine.Pin.IN)
# Relay to move up and down
self.relay_up = machine.Pin(pins.D5, mode=machine.Pin.OUT)
self.relay_down = machine.Pin(pins.D6, mode=machine.Pin.OUT)
# Reset both relays on set up
self.triggerRelay(0)
def currentHeight(self):
"""
Get current height of the table in CM.
Might return False if measuring error.
"""
distance = self.hcsr04.distance_cm()
if distance > 0:
return distance
return False
def getMovement(self):
"""
Get movement from sensor.
"""
return self.movement_sensor.value()
def triggerRelay(self, direction):
"""
Direction should be an integer:
- 1 to move up
- 0 to stop movement
- -1 to move down
"""
if direction == 1:
self.relay_down.value(0)
self.relay_up.value(1)
elif direction == -1:
self.relay_down.value(1)
self.relay_up.value(0)
else:
self.relay_up.value(1)
self.relay_down.value(1)