import pycom #setting pin10 to input mode pinAlert = Pin('P10', mode=Pin.IN, pull= Pin.PULL_DOWN) #turn of the led pycom.heartbeat(False) #turn on green led pycom.rgbled(0x00FF00) # Green #setting pin8 to output mode pinDrive = Pin('P8', mode=Pin.OUT) #pin8 doest hold the value when sleep pinAlert.hold(False) #set pin8 to high pinDrive.value(1) #creating thread lock a_lock = _thread.allocate_lock() #initial distance as 0 dist = 0 old_dist = 0 #function to read ultrasonic sensor value def reading(): global dist
import network import os import time import utime import gc from pytrack import Pytrack import pycom from machine import Pin gc.enable() py = Pytrack() pycom.heartbeat(False) # pycom.heartbeat(True) p_out = Pin('P20', mode=Pin.OUT) if not p_out.hold(): print("not on hold") p_out.value(1) else: print("on hold") p_out.hold(False) p_out.toggle() p_out.hold(True) print("performed hold") if p_out.value(): pycom.rgbled(0x007f7f) else: pycom.rgbled(0x007f00) py.setup_sleep(3) py.go_to_sleep()
class HX711: """ Baseline driver for the HX711 by David Gerber, with modifications. https://cdn.sparkfun.com/datasheets/Sensors/ForceFlex/hx711_english.pdf """ def __init__(self, dout, pd_sck, gain=128): # Define two pins for clock and data. self.pSCK = Pin(pd_sck, mode=Pin.OUT) self.pOUT = Pin(dout, mode=Pin.IN, pull=Pin.PULL_UP) self.initialized = False self.GAIN = 0 self.OFFSET = 0 self.SCALE = 1 self.time_constant = 0.1 self.filtered = None self.set_gain(gain) def set_gain(self, gain): if gain is 128: self.GAIN = 1 elif gain is 64: self.GAIN = 3 elif gain is 32: self.GAIN = 2 def is_ready(self): return self.pOUT() == 0 def initialize(self): """ Power and initialize the chip. Wait for becoming ready. """ if not self.initialized: # Wake up the HX711. self.power_up() # Wait for device to become ready. log.info('Initialization started') self.wait_ready() self.initialized = True log.info('Initialization succeeded') return True def wait_ready(self): retries = 10000 #log.info('Waiting for device to become ready') while not self.is_ready(): idle() retries -= 1 if retries == 0: raise DeviceNotFound('HX711 not ready') #log.info('Device ready') def read(self): """ This chip has a non-standard serial protocol. Serial Interface ---------------- Pin PD_SCK and DOUT are used for data retrieval, input selection, gain selection and power down controls. When output data is not ready for retrieval, digital output pin DOUT is high. Serial clock input PD_SCK should be low. When DOUT goes to low, it indicates data is ready for retrieval. By applying 25~27 positive clock pulses at the PD_SCK pin, data is shifted out from the DOUT output pin. Each PD_SCK pulse shifts out one bit, starting with the MSB bit first, until all 24 bits are shifted out. The 25th pulse at PD_SCK input will pull DOUT pin back to high. """ # Initialize the hardware once. # Otherwise, croak with ``DeviceNotFound('HX711 not available')``. if not self.initialize(): # Wait for the device becoming ready. self.wait_ready() # Shift in data, gain & channel info. result = 0 for j in range(24 + self.GAIN): state = disable_irq() self.pSCK(True) self.pSCK(False) result = (result << 1) | self.pOUT() enable_irq(state) # Shift back the extra bits. result >>= self.GAIN # Check sign. if result > 0x7fffff: result -= 0x1000000 return result def read_average(self, times=3): sum = 0 for i in range(times): sum += self.read() return sum / times def read_lowpass(self): if self.filtered is None: self.filtered = self.read() self.filtered += self.time_constant * (self.read() - self.filtered) return self.filtered def get_value(self, times=3): return self.read_average(times) - self.OFFSET def get_units(self, times=3): return self.get_value(times) / self.SCALE def tare(self, times=15): sum = self.read_average(times) self.set_offset(sum) def set_scale(self, scale): self.SCALE = scale def set_offset(self, offset): self.OFFSET = offset def set_time_constant(self, time_constant=None): if time_constant is None: return self.time_constant elif 0 < time_constant < 1.0: self.time_constant = time_constant def power_up(self): """ When PD_SCK Input is low, chip is in normal working mode. """ # Unfreeze pin hold when coming from deep sleep. # https://community.hiveeyes.org/t/strom-sparen-beim-einsatz-der-micropython-firmware-im-batteriebetrieb/2055/72 self.pSCK.hold(False) log.info('HX711 power up') self.pSCK.value(False) #utime.sleep_us(80) #self.initialize() def power_down(self): """ When PD_SCK pin changes from low to high and stays at high for longer than 60µs, HX711 enters power down mode. """ log.info('HX711 power down') state = disable_irq() self.pSCK.value(False) self.pSCK.value(True) utime.sleep_us(80) enable_irq(state) # Hold level to HIGH, even during deep sleep. # https://community.hiveeyes.org/t/strom-sparen-beim-einsatz-der-micropython-firmware-im-batteriebetrieb/2055/72 self.pSCK.hold(True)
def release_pins(self): pins = ('P22', 'P21', 'P3', 'P4', 'P10', 'P9') for p in self.leaky_pins: _logger.info("Releasing pin %s", p) pin = Pin(p, mode=Pin.OUT) pin.hold(False)
def hold_pins(self): for p in self.leaky_pins: _logger.info("Holding pin %s", p) pin = Pin(p, mode=Pin.OUT) pin(1) pin.hold(True)
import pycom #setting pin19 to input mode pinAlert = Pin('P19', mode=Pin.IN, pull=Pin.PULL_DOWN) #turn of the led pycom.heartbeat(False) #turn on green led pycom.rgbled(0x00FF00) # Green #setting pin8 to output mode pinDrive = Pin('P8', mode=Pin.OUT) #pin8 doest hold the value when sleep pinAlert.hold(False) pinDrive.hold(False) #set pin8 to high pinDrive.value(1) #creating thread lock a_lock = _thread.allocate_lock() #initial I2C lidarReady = bytearray([0xff]) # holds the returned data for ready check readyBuf = bytearray([0x01]) # step 2 address for readiness check distance = 0 # variable for distance reading dist = 0 #function to read ultrasonic sensor value