Created
September 24, 2018 20:57
-
-
Save deejaygraham/0f0163bf04eb5de61dc85841017b134e to your computer and use it in GitHub Desktop.
Microbit detects vibration in case there are T-Rexs or other dinosaurs nearby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from microbit import * | |
sleep_time = 200 | |
warning_time = 5000 | |
class Pinger: | |
def __init__(self): | |
self.direction = 1 | |
self.x = 2 | |
self.y = 2 | |
def update(self): | |
display.set_pixel(self.x, self.y, 0) | |
if self.x >= 4: | |
self.x = 4 | |
self.direction = -self.direction | |
elif self.x <= 0: | |
self.x = 0 | |
self.direction = -self.direction | |
self.x += self.direction | |
display.set_pixel(self.x, self.y, 9) | |
class DinosaurDetector: | |
def __init__(self): | |
self.horizontal_start = 0 | |
self.vertical_start = 0 | |
self.reset() | |
def dinosaur_detected(self): | |
horizontal_now = accelerometer.get_x() | |
vertical_now = accelerometer.get_y() | |
return self.horizontal_start != horizontal_now or self.vertical_start != vertical_now | |
def reset(self): | |
self.horizontal_start = accelerometer.get_x() | |
self.vertical_start = accelerometer.get_y() | |
display.scroll('Dino Detector', delay=100) | |
detector = DinosaurDetector() | |
pinger = Pinger() | |
glass1 = Image("00000:00000:00900:00000:00000") | |
glass2 = Image("00000:00900:09090:00900:00000") | |
glass3 = Image("09990:90009:90009:90009:09990") | |
glass4 = Image("09090:90009:00000:90009:09090") | |
ripple_animation = [glass1, glass2, glass3, glass4, glass3, glass2, glass1] | |
while True: | |
pinger.update() | |
sleep(sleep_time) | |
if detector.dinosaur_detected(): | |
detector.reset() | |
display.show(ripple_animation, delay=100) | |
display.show(Image.GIRAFFE) | |
sleep(warning_time) | |
display.clear() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment