Skip to content

Commit 6e5dcf9

Browse files
committed
First commit of get_angle() API
1 parent 4887dea commit 6e5dcf9

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

neopia/neosoco.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,19 @@ def get_value(self, port='in1'):
328328
else:
329329
raise TypeError
330330

331+
def get_angle(self, port='in1'):
332+
if isinstance(port, str):
333+
if port.lower() =='in1':
334+
return self.read(Neosoco.INPUT_1)
335+
elif port.lower() =='in2':
336+
return self.read(Neosoco.INPUT_2)
337+
elif port.lower() =='in3':
338+
return self.read(Neosoco.INPUT_3)
339+
else:
340+
raise ValueError('Wrong value of port')
341+
else:
342+
raise TypeError
343+
331344
def convert_scale(self, port='in1', omin=0, omax=255, tmin=0, tmax=100):
332345
if isinstance(port, str):
333346
if port.lower() =='in1':

test.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
n = Neosoco()
44

5+
## LED
56
# case1) Turn on LED with 100% brightness during 1s
67
# n.led_on('out1', '100')
78
# wait(1000)
@@ -10,13 +11,15 @@
1011
# n.set_value('out1', 255)
1112
# wait(1000)
1213

14+
## LED, distance sensor
1315
# case3) Turn on LED when the distance is under 10cm
1416
# while True:
1517
# if n.get_value('in1') < 10:
1618
# n.led_on('out1', '100')
1719
# else:
1820
# n.led_off('out1')
1921

22+
## Motors
2023
# case4-1) Move forth and back during 1s and stop
2124
# n.motor_move('forward')
2225
# wait(500)
@@ -40,12 +43,13 @@
4043
# n.motor_move('stop')
4144

4245
# case5) Move forth and back with speed 30% during 1s and stop
43-
n.motor_rotate('both', 'forward', '30')
44-
wait(500)
45-
n.motor_rotate('both', 'backward', '30')
46-
wait(500)
47-
n.motor_move('stop')
46+
# n.motor_rotate('both', 'forward', '30')
47+
# wait(500)
48+
# n.motor_rotate('both', 'backward', '30')
49+
# wait(500)
50+
# n.motor_move('stop')
4851

52+
## Buzzer
4953
# case6) Play same note by pitch, sharp and flat, and a length of note
5054
# n.buzzer('3', n.NOTE_NAME_C)
5155
# n.buzzer('3', 'c')
@@ -60,6 +64,15 @@
6064
# while True:
6165
# n.buzzer_by_port('in1')
6266

67+
## Color LED, distance sensor
6368
# case8) Color LED on with variable color by input port
6469
# r = g = b = n.convert_scale('in1', 0, 255, 85, 170) # Limit to middle brightness
65-
# color_led_on('out1', r, g, b)
70+
# color_led_on('out1', r, g, b)
71+
72+
## LED, Angle sensor
73+
# case9) # Turn on LED when a degree of the angle sensor is under 90 degrees
74+
while True:
75+
if n.get_angle('in1') < 90:
76+
n.led_on('out1', '100')
77+
else:
78+
n.led_off('out1')

0 commit comments

Comments
 (0)