-
Notifications
You must be signed in to change notification settings - Fork 2
List of HW control APIs
from neopia import *
n = Neosoco()
get_value('in1')
- param1 - port: 'in1' ~ 'in3', 'remo', 'bat'
example
# Turn on LED when the distance by the sensor is under 10cm
while True:
if n.get_value('in1') < 10:
n.led_on('out1','100')
else:
n.led_off('out1')
set_value('out1', 255)
- param1 - port: 'out1' ~ 'out3', 'all'
- param2 - value to set: 0 ~ 255
example
# Turn on LED with 100% brightness during 1s
n.set_value("out1", 255)
wait(1000)
convert_scale('in1', 0, 255, 0, 100)
- param1 - port: 'in1' ~ 'in3'
- param2, param3 - min and max value of original scale: 0 ~ 255
- param3, param4 - min and max value of scale to be converted
example
# Color LED on with variable color by input port
r = g = b = n.convert_scale('in1', 0, 255, 85, 170) # Limit to middle brightness
color_led_on('out1', r, g, b)
color_check('in1', 'white')
- param1 - port: 'in1' ~ 'in3'
- param2 - color to find: 'white or COLOR_NAME_WHITE', 'red or COLOR_NAME_RED', 'yellow or COLOR_NAME_YELLOW', 'green or COLOR_NAME_GREEN', 'blue or COLOR_NAME_BLUE'
example
# Turn on LED when sensor detects green color
while True:
print(n.get_value('in1'))
wait(500)
if n.check_color('in1', 'Green'): # Сapital letters can be used to indicate colors
n.led_on('out1', '100')
else:
n.led_off('out1')
led_on('out1', '100')
- param1 - port: 'out1' ~ 'out3', 'all'
- param2 - percentage of brightness: '0', '10' ~ '100'
example
# Turn on LED with 100% brightness during 1s
n.led_on('out1','100')
wait(1000)
led_by_port('in1', 'out1')
- param1 - port: 'in1' ~ 'in3'
- param2 - port: 'out1' ~ 'out3', 'all'
example
# Control LED's brightness with a sensor
while True:
n.led_by_port('in1', 'out1')
led_off('out1')
- param - port: 'out1' ~ 'out3', 'all'
example
# LED blinking
while True:
n.led_on('out1', '100')
wait(1000)
n.led_off('out1')
wait(1000)
color_led_on('out1', 255, 0, 0)
- param1 - port: 'out1' ~ 'out3', 'all'
- param2 - Red: 0 ~ 255
- param3 - Green: 0 ~ 255
- param4 - Blue: 0 ~ 255
example
# Turn on color LED with red color during 3s
n.color_led_on('out1', 255, 0, 0)
wait(3000)
motor_move('forward')
- param1 - direction: 'forward', 'backward', 'left', 'right', 'stop'
example 1
# Move forth and back during 1s and stop
n.motor_move('forward')
wait(500)
n.motor_move('backward')
wait(500)
n.motor_move('stop')
example 2 (Refer to more details of keyboard monitoring from this link)
# Moving control by direction keys on the keyboard
def on_press(key):
if key == Keyboard.UP:
n.motor_move('forward')
elif key == Keyboard.DOWN:
n.motor_move('backward')
elif key == Keyboard.LEFT:
n.motor_move('left')
elif key == Keyboard.RIGHT:
n.motor_move('right')
elif key == Keyboard.SPACE:
n.motor_move('stop')
elif key == Keyboard.ESC:
return False
Keyboard.read(on_press)
example 3 (Keep in mind that you may be asked to grant your IDE access to read keyboard input)
# Moving control by regular keys (0-9, a-z) on the keyboard
def on_press(key):
if Keyboard.key_to_str(key) == 'w':
n.motor_move('forward')
elif Keyboard.key_to_str(key) == 's':
n.motor_move('backward')
elif Keyboard.key_to_str(key) == 'a':
n.motor_move('left')
elif Keyboard.key_to_str(key) == 'd':
n.motor_move('right')
elif Keyboard.key_to_str(key) == 'b':
n.motor_move('stop')
elif key == Keyboard.ESC:
return False
Keyboard.read(on_press)
motor_rotate('both', 'forward', '100')
- param1 - motor: 'both', 'left', 'right'
- param2 - direction: 'forward', 'backward', 'left', 'right', 'stop'
- param3 - speed percentage or input port: '0', '10' ~ '100', 'in1' ~ 'in3'
example
# Move forth and back with speed 30% during 1s and stop
n.motor_rotate('both', 'forward', '30')
wait(500)
n.motor_rotate('both', 'backward', '30')
wait(500)
motor_stop('right')
- param - position of motor: 'both', 'left', 'right'
example
# Move if pressed 'w' and stop if 's'
def on_press(key):
if Keyboard.key_to_str(key) == 'w':
n.motor_move('forward')
elif Keyboard.key_to_str(key) == 's':
n.motor_stop('both')
buzzer('3', 'c', '8')
- param1 - pitch: '1' ~ '6'
- param2 - note: 'c or NOTE_NAME_C', 'c# or NOTE_NAME_C_SHARP', 'db or NOTE_NAME_D_FLAT', 'd' ~ 'b'
- param3 - length of note: '2', '4', '8', '16'
example
# Play same note by pitch, sharp and flat, and a length of note
n.buzzer('3', n.NOTE_NAME_C)
n.buzzer('3', 'c')
n.buzzer('4', n.NOTE_NAME_C_SHARP, '8')
n.buzzer('4', 'c#', '8')
n.buzzer('5', n.NOTE_NAME_D_FLAT, '16')
n.buzzer('5', 'db', '16')
buzzer_by_port('in1')
- param - port: 'in1' ~ 'in3'
example
# Play a sound by value from input port
while True:
n.buzzer_by_port('in1')
buzzer_stop()
example
# Stop buzzer when the distance by the sensor is under 10cm
while True:
if n.get_value('in1') < 10:
n.buzzer_stop()
else:
n.buzzer('2', 'c', '4')
get_angle('in1')
- param1 - port: 'in1' ~ 'in3'
example
# Turn on LED when a degree of the angle sensor is under 90 degrees
while True:
if n.get_angle('in1') < 90:
n.led_on('out1','100')
else:
n.led_off('out1')
servo_rotate('out1', 'forward', '100')
- param1 - output port: 'out1', 'out2', 'out3'
- param2 - direction: 'forward', 'backward'
- param3 - speed percentage or input port: '0', '10' ~ '100', 'in1' ~ 'in3'
example
# Rotate servo motor forth and back with speed 50% during 5s and stop
n.servo_rotate('out2', 'forward', '50')
wait(2000)
n.servo_rotate('out2', 'forward', '0')
wait(1000)
n.servo_rotate('out2', 'backward', '50')
wait(2000)
n.servo_rotate('out2', 'forward', '0')
wait(1000)
servo_reset_degree('out1')
- param - output port: 'out1' ~ 'out3', 'all'
example
# Refer to the example of server_rotate_by_degree function
servo_rotate_by_degree('out1', 'forward', '100', '90')
- param1 - output port: 'out1', 'out2', 'out3'
- param2 - direction: 'forward', 'backward'
- param3 - speed percentage or input port: '0', '10' ~ '100', 'in1' ~ 'in3'
- param4 - degree of angle: '0', '5' ~ '180', 'in1' ~ 'in3'
example
# Rotate servo motor forward by 120 degrees at 50% speed within 3 seconds from its current position
n.servo_reset_degree('out1')
n.servo_rotate_by_degree('out1', 'forward', '50', '120')
wait(3000)
servo_stop('out1')
- param - output port: 'out1' ~ 'out3', 'all'
example
# Stop servo motor when the distance by the sensor is under 10cm
while True:
if n.get_value('in1') < 10:
n.servo_stop('out1')
else:
n.servo_rotate('out2', 'forward', '50')
wait(2000)
n.servo_rotate('out2', 'backward', '50')
wait(2000)
remote_button('1')
- param - button of RC: '1', '2', '3', 'up', 'left', 'right', 'down'
example
# When the 1 button of remote controller is pressed, turn on the LED on 1 output port
while True:
if n.remote_button('up'):
n.led_on() # By default value
else:
n.led_off()