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 fly():
    eft = ticks_diff(ticks_ms(), st)
    if eft > 60000:
        return 4
    f.write("{},{},{}\n".format(eft, b.altitude(), pin2.read_analog()))
    sleep(200)
    return 3
Example #3
0
 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
Example #4
0
 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
Example #6
0
    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()
Example #7
0
    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
Example #8
0
    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()
Example #9
0
 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
Example #11
0
from microbit import pin2, running_time, display, Image, sleep
import os
from utime import ticks_ms

n = len(os.listdir())
f = open("sound{}.csv".format(n + 1), "w")
display.show(Image.NO)

while True:
    s = pin2.read_analog()
    t = ticks_ms()
    f.write("{},{}\n".format(t, s))
    sleep(200)

    if t > 60000:
        break

display.show(Image.YES)
f.close()