-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
73 lines (49 loc) · 1.88 KB
/
main.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
import os, time, brickpi3
# Initialize the EV3 Brick
BP = brickpi3.BrickPi3()
# Initialize EV3 touch sensor and motors
BP.set_sensor_type(BP.PORT_1, BP.SENSOR_TYPE.TOUCH)
BP.set_sensor_type(BP.PORT_2,BP.SENSOR_TYPE.EV3_ULTRASONIC_CM)
touchButton = 0
ultraSound = 0
# Without a delay reading sensor data can cause an error
time.sleep(0.5)
# Quite loop when CTRL+C is pressed
try:
# Create a loop to react to buttons
while True:
# Get touch sensor status
'''
touchButton = BP.get_sensor(BP.PORT_1)
# If the touch sensor is pressed
if touchButton:
BP.set_motor_power(BP.PORT_A, 100)
# Turn off BrickPi on board LED
BP.set_led(1)
# Turn on Raspberry Pi on board LED
os.system('echo 1 | sudo dd status=none of=/sys/class/leds/led0/brightness')
# If the touch sensor is released
else:
BP.set_motor_power(BP.PORT_A, 0)
# Turn off BrickPi on board LED
BP.set_led(0)
# Turn off Raspberry Pi on board LED
os.system('echo 0 | sudo dd status=none of=/sys/class/leds/led0/brightness')'''
ultraSound = BP.get_sensor(BP.PORT_2)
print(ultraSound)
if ultraSound < 200:
BP.set_motor_power(BP.PORT_A,100)
BP.set_led(1)
os.system('echo 1 | sudo dd status=none of=/sys/class/leds/led0/brightness')
else:
BP.set_motor_power(BP.PORT_A,0)
BP.set_led(0)
os.system('echo 0 | sudo dd status=none of=/sys/class/leds/led0/brightness')
# Loop delay
time.sleep(0.1)
# Result of CTRL+C
except KeyboardInterrupt:
# Unconfigure the sensors
BP.reset_all()
# Turn off Raspberry Pi on board LED
os.system('echo 0 | sudo dd status=none of=/sys/class/leds/led0/brightness')