Use Python

Codey is a micropython-based robot. Python code need to be uploaded to Codey in order to run.

Start Using Python

Start using Python by switching the programming mode from "Blocks" to "Python"

Note: please make sure that Codey is currently selected.

Here's an example code:

After programming, click "Upload" to upload the program to Codey.

Convert Blocks to Python

In the Scripts area, click to covert blocks to Python. The following is an example:

Use the Face Panel

display.show(text, wait=False)

Show a piece of text on Codey's Face Panel.

display.show_image(image, time)

Show an image on Codey's face panel. The image displayed needs to be converted to hexadecimal data.

If the duration is any other number than zero, the panel will turn off after the set duration.

display.show_image(image, x, y)

Display an image on Codey's face panel at a specific location (x, y).

codey.display.set_pixel(x, y, status) / codey.display.toggle_pixel(x, y)

  • set_pixel: set the status of the pixel at (x, y); set True to light up the pixel, or False to turn it off

  • toggle_pixel: toggle a pixel at (x, y): turn on if it is off, and off if it is on

display.get_pixel(x, y)

Check if the certain pixel of Codey is on or not. If on, return True; otherwise return False

display.clear()

Clear all the contents on Codey's face panel.

Use the RGB LED on the Codey

led.show(r, g, b, time)

Set the color of Codey's LED.

If the duration is a non-zero number, the LED will turn off after specified seconds.

led_off()

Turn off Codey's LED.

led.set_red(value) / led.set_green(value) / led.set_blue(value)

Set the RGB value of the LED independently. The range of each color value is 0-255.

Make a Sound

speaker.play_melody(name)

Play the a sound file.

Sound file available:

  • hello.wav : hello

  • hi.wav :hi

  • bye.wav : bye

  • yeah.wav : yeah

  • wow.wav : wow

  • laugh.wav : laugh

  • hum.wav : hum

  • sad.wav : sad

  • sigh.wav : sigh

  • annoyed.wav : annoyed

  • angry.wav : angry

  • surprised.wav : scared

  • yummy.wav : pettish

  • curious.wav : curious

  • embarrassed.wav : embarrassed

  • ready.wav : ready

  • sprint.wav : sprint

  • sleepy.wav : snore

  • meow.wav : meow

  • start.wav : start

  • switch.wav : switch

  • beeps.wav : beeps

  • buzzing.wav : buzz

  • exhaust.wav : air-out

  • explosion.wav : explosion

  • gotcha.wav : gotcha

  • hurt.wav : painful

  • jump.wav : jump

  • laser.wav : laser

  • level up.wav : level-up

  • low energy.wav : low-energy

  • metal clash.wav : metal-clash

  • prompt tone.wav : prompt-tone

  • right.wav : right

  • wrong.wav : wrong

  • ring.wav : ringtone

  • score.wav : score

  • shot.wav : shot

  • step_1.wav : step_1

  • step_2.wav : step_2

  • wake.wav : activate

  • warning.wav : warning

speaker.play_note(note_number, beats)

Tell Codey to play a musical note for a duration.

The note number is the MIDI number. You may also use note names like "C3" or "D4". Incorrect note name will produce an error in the console.

speaker.play_tone(frequency, beats)

Play a certain frequency of sound for a duration of certain beats.

speaker.rest(beats)

Pause the sound for a certain number of beats.

speaker.stop_sounds()

Stop all sounds.

speaker.volume

Set or read the volume of Codey's speaker.

Use the Sensors of Codey

button_a.is_pressed()/button_b.is_pressed()/button_c.is_pressed()

If the specified button is pressed, return True; otherwise, return False.

potentiometer.get_value()

Get the position of the potentiometer knob on Codey's side. The value will be 0-100.

sound_sensor.get_loudness()

Get the loudness of the sound sensor. The range is 0-100.

light_sensor.get_value()

Get the light intensity detected by the light sensor. The range is 0-100.

motion_sensor.is_shaked()

Tell whether the Codey is being shaken. The result is True or False.

motion_sensor.is_tilted_left()/motion_sensor.is_tilted_right()/ motion_sensor.is_ears_up()/motion_sensor.is_ears_down()

Tell whether Codey is tilted to the right/left, or placed ears-up/ears-down. The result is True or False.

Gyroscope

Get the roll, pitch or yaw value of Codey's gyroscope

  • motion_sensor.get_roll()

  • motion_sensor.get_pitch()

  • motion_sensor.get_yaw()

Get the gyroscope's rotation value (in degrees) around a certain axis.

  • motion_sensor.get_rotation(axis): x, y, or z axis

  • motion_sensor.reset_rotation(axis="all"): reset the rotation angles of the gyro

You can use motion_sensor.get_shake_strength() to get the intensity of the shaking.

get_timer()

Get the timer value in seconds (since startup or last reset)

reset_timer()

Reset the timer.

Codey's Events and Flow Control

Codey supports events (like when the button is pressed), and it also supports multi-threading.

If you wish to use event, declare a function and register it to the event. A program can only register no more than 6 event functions.

Example:

on_button_a_pressed()/on_button_b_pressed()/on_button_c_pressed()

When the button is pressed, run the function.

on_shaked()

When Codey is shaken, call the function.

on_tilted_left()

Call the function when Codey tilts to the left.

on_greater_than(volume, 'sound_sensor')

When the loudness is over a certain value, call the function.

on_less_than(lightness, 'light_sensor')

When the lightness is under a certain value, call the function.

on_received(message_name)

When a Scratch broadcast is received, trigger the function.

broadcast(message_name)

Broadcast a certain message to the Scratch system.

stop_all_scripts()/stop_this_script()/stop_other_scripts()

Stop all processes, this process, or other processes.

Let the Rocky move

forward(speed, time)

Tell the Rocky to move forward for a certain period of time. If the time is not set, it will keep moving. The speed range is 0-100.

backward(speed, time)

Tell the Rocky to move backward for a certain period of time. If the time is not set, it will keep moving. The speed range is 0-100.

turn_right(speed, time)

Tell the Rocky to turn right for a certain period of time. If the time is not set, it will keep turning. The speed range is 0-100.

turn_left(speed, time)

Tell the Rocky to turn left for a certain period of time. If the time is not set, it will keep turning. The speed range is 0-100.

turn_right_by_degree(angle)

Make Rocky turn right at a certain angle until done.

turn_left_by_degree(angle)

Make Rocky turn left at a certain angle until done.

drive(left_speed, right_speed)

Set the speed of Codey's two wheels at the same time.

stop()

Stop the movement of Codey

Use Rocky's Sensors and Light

color_ir_sensor.set_led_color(color)

Set the color of Rocky's LED. Color can be one of: "red", "green", "blue", "yellow", "purple", "cyan", "white", "black". Set the color to "black" will turn off Rocky's LED.

color_ir_sensor.is_obstacle_ahead()

Tell if there is obstacle ahead. Return True or False. (Rocky's sensor array must face forward)

color_ir_sensor

Get the value of the base sensor array.

  • color_ir_sensor.get_light_strength(): get the ambient light intensity of the environment; return 0-100

  • color_ir_sensor.get_reflected_light(): get the reflection light value of the surface; return 0-100

  • color_ir_sensor.get_reflected_infrared(): get the IR reflection value of the surface; return 0-100

  • color_ir_sensor.get_greyness(): get the greyness of the surface by light sensor; return 0-100

  • color_ir_sensor.get_red(): get the red value of the surface; return 0-255

  • color_ir_sensor.get_green(): get the green value of the surface; return 0-255

  • color_ir_sensor.get_blue(): get the blue value of the surface; return 0-255

  • color_ir_sensor.is_color(color_str): Tell whether the color sensor senses the specific color; return True or False. The color_name could be one of "red", "green", "blue", "yellow", "purple", "cyan", "white", "black".

Communicate with Infrared Signals

Codey can send and receive infrared signals.

ir_send(message)

send a piece of string message with Codey's Infrared Emitter. Another Codey can receive this message with its Infrared Receiver.

ir_receive()

When a piece of message sent with infrared signals is received, return a string value.

Last updated

Was this helpful?