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 lineFollowCal(self): self.rightLineSensor = pin1.read_analog() self.leftLineSensor = pin2.read_analog() #calculate the middle value between the two sensor readings offset = abs(self.rightLineSensor - self.leftLineSensor) / 2 #apply the offset to each reading so that it neutralises any difference if self.leftLineSensor > self.rightLineSensor: self.leftLfOffset = -offset self.rightLfOffset = offset else: self.leftLfOffset = offset self.rightLfOffset = -offset
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 play(self): p1 = pin1.read_analog() p2 = pin2.read_analog() if p1 <= 300: if p2 < 300: display.show(Image.ARROW_SW) elif p2 >= 900: display.show(Image.ARROW_NW) else: display.show(Image.ARROW_W) elif p1 >= 900: if p2 < 300: display.show(Image.ARROW_SE) elif p2 >= 900: display.show(Image.ARROW_NE) else: display.show(Image.ARROW_E) elif 300 < p1 < 900: if p2 < 300: display.show(Image.ARROW_S) elif p2 >= 900: display.show(Image.ARROW_N) else: if not pin15.read_digital(): display.show(Image.HAPPY) music.play(music.JUMP_UP) elif not pin14.read_digital(): display.show(Image.SURPRISED) music.play(music.POWER_UP) elif not pin13.read_digital(): display.show(Image.SAD) music.play(music.JUMP_DOWN) elif not pin12.read_digital(): display.show(Image.ASLEEP) music.play(music.POWER_DOWN) elif not pin8.read_digital(): display.show(Image.HEART) music.play(["C1:1", "C#1:1"]) display.clear()
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 logic(self): display.clear() p1 = pin1.read_analog() p2 = pin2.read_analog() if p1 <= 204: x = 0 elif 204 < p1 <= 408: x = 1 elif 408 < p1 <= 612: x = 2 elif 612 < p1 <= 816: x = 3 else: x = 4 if p2 <= 204: y = 4 elif 204 < p2 <= 408: y = 3 elif 408 < p2 <= 612: y = 2 elif 612 < p2 <= 816: y = 1 else: y = 0 display.set_pixel(*self.pix) display.set_pixel(x, y, 9) if [x, y, 9] == self.pix: self.pix = None display.show(Image.HAPPY) music.play(music.JUMP_UP) sleep(500) if self.btn_pressed(): sleep(150) self.set_random_pix()
def __init__(self): self.sensorLeftRef = pin0.read_analog() self.sensorCentreRef = pin1.read_analog() self.sensorRightRef = pin2.read_analog() self.detectionLevel = 45
def readLineFollow(self, sensor): if sensor == "left": return pin2.read_analog() + self.leftLfOffset elif sensor == "right": return pin1.read_analog() + self.rightLfOffset
def listen(): return pin1.read_analog()
def lightDetect(): return pin1.read_analog()