Use Python
Last updated
Last updated
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:
After programming, click "Upload" to upload the program to Codey.
Show a piece of text on Codey's Face Panel.
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 an image on Codey's face panel at a specific location (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
Check if the certain pixel of Codey is on or not. If on, return True; otherwise return False
Clear all the contents on Codey's 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.
Turn off Codey's LED.
Set the RGB value of the LED independently. The range of each color value is 0-255.
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
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.
Play a certain frequency of sound for a duration of certain beats.
Pause the sound for a certain number of beats.
Stop all sounds.
Set or read the volume of Codey's speaker.
If the specified button is pressed, return True; otherwise, return False.
Get the position of the potentiometer knob on Codey's side. The value will be 0-100.
Get the loudness of the sound sensor. The range is 0-100.
Get the light intensity detected by the light sensor. The range is 0-100.
Tell whether the Codey is being shaken. The result is True or False.
Tell whether Codey is tilted to the right/left, or placed ears-up/ears-down. The result is True or False.
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 the timer value in seconds (since startup or last reset)
Reset the timer.
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:
When the button is pressed, run the function.
When Codey is shaken, call the function.
Call the function when Codey tilts to the left.
When the loudness is over a certain value, call the function.
When the lightness is under a certain value, call the function.
When a Scratch broadcast is received, trigger the function.
Broadcast a certain message to the Scratch system.
Stop all processes, this process, or other processes.
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.
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.
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.
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.
Make Rocky turn right at a certain angle until done.
Make Rocky turn left at a certain angle until done.
Set the speed of Codey's two wheels at the same time.
Stop the movement of Codey
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.
Tell if there is obstacle ahead. Return True or False. (Rocky's sensor array must face forward)
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".
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.
When a piece of message sent with infrared signals is received, return a string value.
In the Scripts area, click to covert blocks to Python. The following is an example: