-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMakeBlock.py
92 lines (75 loc) · 1.87 KB
/
MakeBlock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from megapi import *
import time
import pygame
import RPi.GPIO as GPIO
announce = time.time()
distance = 123
# -=Function=-
def Forward(port, speed):
sleep(0.4)
bot.encoderMotorRun(port,-speed)
def Backward(port, speed):
bot.encoderMotorRun(port, speed)
# -=UltrasonicSensor=-
def UltraSonic(port):
global distance
distance = port
print(distance)
# -=SoundPlay=-
def SoundPlay(SoundFile):
SoundList = ["Ms C.mp3", "Mr Williams.mp3", "beep.mp3"] # Ms. C = 0 | Mr. W = 1 | beep = 2
pygame.mixer.init()
pygame.mixer.music.set_volume(0.2) # Volume: (0 - 1)
pygame.mixer.music.load(SoundList[SoundFile])
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == False:
continue
# -=GPIO=-
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
# Speaker
speaker_buttonPin1 = 32
speaker_buttonPin2 = 22
GPIO.setup(speaker_buttonPin1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(speaker_buttonPin2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Sound Sensor
sound_sensorPin = 18
GPIO.setup(sound_sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# -=Main=-
if __name__ == '__main__':
bot = MegaPi()
bot.start()
output = 0
SoundPlay(2)
while True:
# -=SET UP=-
if GPIO.input(speaker_buttonPin1) == GPIO.HIGH:
output = 1
if GPIO.input(speaker_buttonPin2) == GPIO.HIGH:
output = 2
Forward(4, 0)
Backward(1, 0)
# -=GO OUT=-
if output == 1:
# Sound
if time.time() - announce > 30:
announce = time.time()
SoundPlay(0)
# Measure Distance (Store in "Distance" global variable)
bot.ultrasonicSensorRead(6,UltraSonic)
# Movement
if distance < 20:
Forward(4,50)
Backward(1, 0)
else:
Forward(4, 50) # Left Wheel
Backward(1, 50) # Right Wheel
# -=BE QUIET=-
elif output == 2:
Forward(4, 0)
Backward(1, 0)
if GPIO.input(sound_sensorPin) == GPIO.HIGH:
print("MAMA MIA")
SoundPlay(1)
else:
pass