Use Python
Last updated
Was this helpful?
Last updated
Was this helpful?
Codey is a micropython-based robot. Python code need to be uploaded to Codey in order to run.
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:
import codey
import rocky
import time
import event
@event.start
def on_start():
codey.led.show(255,0,0)
rocky.forward(50,1)
After programming, click "Upload" to upload the program to Codey.
Show a piece of text on Codey's Face Panel.
codey.display.show("hello world", wait=False)
codey.display.show(3.14, wait=False)
codey.display.show("14:02", wait=False)
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.
codey.display.show_image("00003c7e7e3c000000003c7e7e3c0000", time_s=1)
Display an image on Codey's face panel at a specific location (x, y).
codey.display.show_image("000c18181c0c000000000c1c18180c00", 0, 0)
# Show a smiley face at the upper-left corner
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
codey.display.set_pixel(5, 5, True) # light up the pixel at (5, 5)
codey.display.set_pixel(5, 5, False) # turn off the pixel at (5, 5)
codey.display.toggle_pixel(5, 5) # toggle the pixel at (5, 5)
Check if the certain pixel of Codey is on or not. If on, return True; otherwise return False
print(codey.display.get_pixel(5, 5)) # print True if the pixel at (5, 5) is on
Clear all the contents on Codey's face panel.
codey.display.clear() # turn off the face panel
Set the color of Codey's LED.
If the duration is a non-zero number, the LED will turn off after specified seconds.
codey.led.show(255, 255, 0, 1) # set the color, turn off after 1 second
codey.led.show(255, 0, 0) # change the color to red
Turn off Codey's LED.
codey.led_off() # turn off the led light
Set the RGB value of the LED independently. The range of each color value is 0-255.
codey.led.set_red(255)
codey.led.set_green(255)
codey.led.set_blue(255)
Play the a sound file.
codey.speaker.play_melody("meow")
codey.speaker.play_melody("meow.wav") # .wav is optional
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
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.
codey.speaker.play_note(36, 1)
codey.speaker.play_note('c3', 0.25)
Play a certain frequency of sound for a duration of certain beats.
codey.speaker.play_tone(700, 1)
Pause the sound for a certain number of beats.
codey.speaker.rest(0.25)
Stop all sounds.
codey.speaker.stop_sounds()
Set or read the volume of Codey's speaker.
codey.speaker.volume = (70) # Set the volume of Codey's speaker to 70%
print(codey.speaker.volume) # Get the current volume of Codey's speaker
If the specified button is pressed, return True; otherwise, return False.
print(codey.button_a.is_pressed()) # print True if the button A is pressed.
Get the position of the potentiometer knob on Codey's side. The value will be 0-100.
print(codey.potentiometer.get_value()) # Get the position of potentiometer
Get the loudness of the sound sensor. The range is 0-100.
print(codey.sound_sensor.get_loudness()) # output the loudness of the sound sensor
Get the light intensity detected by the light sensor. The range is 0-100.
print(codey.light_sensor.get_value()) # output the light sensor's value
Tell whether the Codey is being shaken. The result is True or False.
print(codey.motion_sensor.is_shaked()) # if Codey is being shaken, return True
Tell whether Codey is tilted to the right/left, or placed ears-up/ears-down. The result is True or False.
print(codey.motion_sensor.is_tilted_left()) # If Codey is left-tilted, return True
print(codey.motion_sensor.is_ears_up()) # If Codey is placed ears-up, return True
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.
print("The yaw and pitch is:", codey.motion_sensor.get_yaw(), codey.motion_sensor.get_pitch()) # output the yaw and pitch value of the gyro
print("The rotation value is:", codey.motion_sensor.get_rotation(x), codey.motion_sensor.get_rotation(y), codey.motion_sensor.get_rotation(z))
codey.motion_sensor.reset_rotation() # reset the rotation value.
print("Shake strength:", codey.motion_sensor.get_shake_strength())
Get the timer value in seconds (since startup or last reset)
print(codey.get_timer()) # print the timer value since startup or last reset
Reset the timer.
codey.reset_timer()
print(codey.get_timer()) # prints 0
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:
def on_button_a_pressed(): # define a function
print("Button A is pressed!")
codey.on_button_a_pressed() # register it to "when button A is pressed" event
When the button is pressed, run the function.
def on_button_b_pressed():
print("The B button is pressed")
codey.on_button_b_pressed()
When Codey is shaken, call the function.
def on_shaked():
print("I'm shaken!")
codey.on_shaked()
Call the function when Codey tilts to the left.
def on_tilted_left():
print("I'm tilted-left!")
codey.on_tilted_left()
When the loudness is over a certain value, call the function.
def on_greater_than():
print("The volume is over 50! Too loud!")
codey.on_greater_than(50, 'sound_sensor')
When the lightness is under a certain value, call the function.
def on_less_than():
print("The light is under 30! too dark!")
codey.on_less_than(30, 'light_sensor')
When a Scratch broadcast is received, trigger the function.
def on_received():
print("Game start!")
codey.on_received('game_start')
Broadcast a certain message to the Scratch system.
codey.broadcast('game_start')
Stop all processes, this process, or other processes.
codey.stop_all_scripts() # stop all processes on Codey
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.
rocky.forward(50, 1) # run forward for 1 seconds at the speed of 50
rocky.forward(100) # run forward forever at the speed of 100
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.
rocky.backward(50)
rocky.backward(100) # run backward at the max speed of 100
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.
rocky.turn_right(50, 1) # turn right at speed 50 for 1 second
rocky.turn_right(100) # turn right at the max speed of 100
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.
rocky.turn_left(50, 1) # turn left at speed 50 for 1 second
rocky.turn_left(100) # turn left at the max speed of 100
Make Rocky turn right at a certain angle until done.
rocky.turn_right_by_degree(90) # turn right for 90 degrees until done
Make Rocky turn left at a certain angle until done.
rocky.turn_left_by_degree(90) # turn left for 90 degrees until done
Set the speed of Codey's two wheels at the same time.
rocky.drive(50, 50) # set the speed of both wheels to 50
Stop the movement of Codey
rocky.stop() # tell Codey to stop moving
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.
rocky.color_ir_sensor.set_led("red")
rocky.color_ir_sensor.set_led("black")
Tell if there is obstacle ahead. Return True or False. (Rocky's sensor array must face forward)
print(rocky.color_ir_sensor.is_obstacle_ahead())
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".
print("Lightness and Reflection: ", color_ir_sensor.get_light_strength(), color_ir_sensor.get_reflected_light())
print("IR light value: ", color_ir_sensor.get_reflected_infrared())
print("Greyness: ", color_ir_sensor.get_greyness())
print("RGB value: ", color_ir_sensor.get_red(), color_ir_sensor.get_green(), color_ir_sensor.get_blue())
print("On a red surface? ", color_ir_sensor.is_color("red"))
Codey can send and receive infrared signals.
send a piece of string message with Codey's Infrared Emitter. Another Codey can receive this message with its Infrared Receiver.
codey.ir_send("A")
When a piece of message sent with infrared signals is received, return a string value.
codey.show(codey.ir_receive())
In the Scripts area, click to covert blocks to Python. The following is an example: