def convertRGB(): low = 10 high = 50 r = convertInput(pin1.read_analog(), low, high) g = convertInput(pin2.read_analog(), low, high) b = convertInput(pin0.read_analog(), low, high) return g, r, b
def readAnalogSensor(self, pin): if pin == "P0": value = pin0.read_analog() elif pin == "P1": value = pin1.read_analog() elif pin == "P2": value = pin2.read_analog() return value
def sensors(self): '''Returns a tuple with 3 booleans for the state of the 3 analog infrared sensors over a black line. Returns: left (bool): True if left sensor is above a black line. center (bool): True if center sensor is above a black line. right (bool): True if right sensor is above a black line. ''' left = pin0.read_analog() >= self.threshold center = pin1.read_analog() >= self.threshold right = pin2.read_analog() >= self.threshold return (left, center, right)
def count_claps(): t = running_time() lastr = 900 claps = 0 while running_time() - t < 1000: r = pin0.read_analog() if lastr > 500 and r < 100: t = running_time() sleep(100) elif lastr < 100 and r > 500: t = running_time() sleep(100) claps += 1 lastr = r return claps
def encoders(threshold=150): left = pin0.read_analog() right = pin1.read_analog() l, r = 1, 1 count_l, count_r = 0, 0 if left < threshold: l = 0 count_l += 1 elif left > threshold: l = 1 if right < threshold: r = 0 count_r += 1 elif right > threshold: r = 1 return (l, r, count_l, count_r)
def readDigitalSensor(self, pin, lightLevel): if pin == "P0": value = pin0.read_analog() ref = self.sensorLeftRef elif pin == "P1": value = pin1.read_analog() ref = self.sensorCentreRef elif pin == "P2": value = pin2.read_analog() ref = self.sensorRightRef if lightLevel == "Light": if (value >= (ref + self.detectionLevel)): result = True else: result = False elif lightLevel == "Dark": if (value <= (ref - self.detectionLevel)): result = True else: result = False return result
def calibrate(): t = running_time() lastrt = t prev = 0 calibrated = False while not calibrated: r = pin0.read_analog() - 11 r = abs(r) / 900 * 5 r = int(r) if r < 6 else 5 if (r == 5 and prev != 5) or (r == 0 and prev != 0): t = running_time() prev = r if running_time() - t > 3000: lastrt = running_time() if r == 0: # No sound for more than 3 seconds # Turn right display.show(Image.ARROW_E) else: # Saturated for more than 3 seconds # Turn left display.show(Image.ARROW_W) else: for y in range(0, 5): for x in range(0, 5): if 4 - y < r: display.set_pixel(x, y, 5) else: display.set_pixel(x, y, 0) if r > 0: # If there is sound, leave the leds on for .1 seconds sleep(100) if running_time() - lastrt > 5000: display.show(Image.YES) sleep(1000) calibrated = True display.clear() return calibrated
def __init__(self): self.sensorLeftRef = pin0.read_analog() self.sensorCentreRef = pin1.read_analog() self.sensorRightRef = pin2.read_analog() self.detectionLevel = 45
from microbit import pin0, sleep from time import ticks_ms while True: value = ( ticks_ms(), pin0.read_analog(), ) print(value) sleep(100)
from microbit import pin0, sleep while True: value = (pin0.read_analog(), ) print(value) sleep(100)
return round( (oneminuteinmilliseconds / ((currenteventtime - lasteventtime) * 3))) minlightvalue, maxlightvalue = 2, 4 lasttriggertime, lastreportime = 0, 0 hashitminlightlevel = False oneminuteinmilliseconds = 60000 print("ready to start") while True: # get current reading then devide value by a number # that brings it into single digits, in our case % # 100 this makes it easier to detect high and low # light values as there are fewer steps in light # level available currentlightreading = round(pin0.read_analog() / 100) # uncomment line below to calibrate min and max light # values but be sure to comment out again before # trying to calculate RPM # print("light reading:", currentlightreading) # check if light has # dimmed to lowest value BUT has been to the highest # value first this reduces / elimenates false # positive readings if (currentlightreading <= minlightvalue and not hashitminlightlevel): hashitminlightlevel = True currenttime = running_time() if (currenttime - lastreportime > 1000): # write our result at most once per second otherwise # serial.write slows down the reading of the light # sensor too much
def read_light_sensor(): light = pin0.read_analog() return light