Skip to content

List of HW control APIs

BCKM edited this page Oct 12, 2024 · 3 revisions

General

Create an instance of a Neosoco object


from neopia import *

n = Neosoco()

Get a value from input port

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 a value to output port

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 a range of values from input port to another

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)

Detect color from input port

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

Turn on LED with brightness

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)

Control LED's brightness by value from input port

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')

Turn off LED

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

Turn on color LED with color

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)

DC Motor

Move forward by motor

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)

Rotate DC motor by a count of motor, direction and speed

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)

Stop motor

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

Play a sound by pitch, note with sharp and flat, and a length of note

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')

Play a sound by value from input port

buzzer_by_port('in1')

  • param - port: 'in1' ~ 'in3'

example


# Play a sound by value from input port
while True:
  n.buzzer_by_port('in1')

Stop buzzer

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')

Angle

Get a degree of an angle sensor

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 Motor

Rotate servo motor by direction and speed

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)

Make 0 degree where servo motor is currently

servo_reset_degree('out1')

  • param - output port: 'out1' ~ 'out3', 'all'

example


# Refer to the example of server_rotate_by_degree function

Rotate servo motor by direction, speed and degree of angle

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)

Stop servo motor

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 controller

Check if the button of remote controller is pressed

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()