Use Python with Codey

Note: the content of this page will no longer be updated, and is not suitable for the latest version of mBlock 5; please visit the new page: Use Python with Codey

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" (Insert Pics)

Note: please make sure that Codey is currently selected. (Insert Pics)

Here's an example code

from codey import *
from rocky import *

color("red")

for i in range(5):
    rocky.forward(50, 1)
    rocky.right(50, 1)

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

Convert Blocks to Python

(Insert Pic)

Use the Face Panel

show(text)

Show a piece of text on the Face Panel

codey.show("hello world")
codey.show(3.14)
codey.show("14:02")

face(image, duration=0)

Show an image on the face panel.
If the duration is any other number than zero, the panel will turn off after the set duration

codey.face(
'0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'
'0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0'
'0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0'
'0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0'
'0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0'
'0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0'
'0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0'
'0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0')

face_at(image, x, y)

display an image on the face panel at a specific location

codey.face_at(
'0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'
'0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0'
'0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0'
'0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0'
'0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0'
'0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0'
'0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0'
'0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', 0, 0) # Show a smiley face at the top-left corner

pixel(x, y) / pixel_off(x, y) / pixel_toggle(x, y)

  • pixel: light up a pixel at (x, y)
  • pixel_off: turn off a pixel at (x, y)
  • pixel_toggle: toggle a pixel at (x, y): turn on if it is off, and off if it is on
codey.pixel(5, 5) # light up a pixel at (5, 5)
codey.pixel_off(5, 5) # turn off a pixel at (5, 5)
codey.pixel_toggle(5, 5) # toggle the pixel at (5, 5)

has_pixel(x, y)

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

print(codey.has_pixel(5, 5))  # print True if the pixel at (5, 5) is on

clear()

Clear all the content at the face panel

codey.clear() # turn off the face panel

Use the RGB LED on the Codey

color(color, duration=0)

Set the color of Codey's LED.
If the duration is a non-zero number, the LED will turn off at the specified seconds.

codey.color('#334455', 1) # set the color, turn off at 1 second
codey.color('#ff0000') # change the color to red

color_off()

turn off the led light

codey.color_off() # turn off the led light

red(value) / green(value) / blue(value)

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

codey.red(255)
codey.green(255)
codey.blue(255)

Make a Sound

play(name)

Play the a sound file.

codey.play("cat")
codey.play("cat.wav") # .wav is optional

sound parameters available to select:

  • 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
  • leepy.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

play_note(note_number, beats) / (not implemented)play_note(note_name, 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.

codey.play_note(36, 1)
codey.play_note('c3', 0.25)

play_freq(frequency, beats)

Play a note of a certain frequency, for a duration of certain beats

codey.play_freq(700, 1)

pause(beats)

Pause the sound playing for the certain beats.

codey.pause(0.25)

mute()

Stop playing all sound.

codey.mute()

set_volume(volume)

Set the volume of Codey

codey.set_volume(100) # Set the volume of Codey to 100

volume()

Get the current volume of Codey

print(codey.volume()) # Output the current volume of Codey

change_volume_by(value)

Change the volume by a certain value

codey.change_volume_by(-10) # turn down the volume by 10

Use the Sensor of Codey

button_pressed(button_name)

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

The button_name could be (capitalized) "A", "B", or "C"

print(codey.button_pressed("A")) # print True if the button A is pressed.

dail()

Get the position of the (gear potential meter) dail on the side of Codey.
The value will be 0-100

print(codey.dail()) # Get the position of the dail

loudness()

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

print(codey.loudness()) # output the loudness of the sound sensor

lightness()

Get the lightness of the light sensor. The range is 0-100

print(codey.lightness()) # output the light sensor's value

is_shaked()

Tell whether the Codey is shaked. The result is True or False

print(codey.is_shaked()) # if Codey is shaked, return True

is_tilted(direction)

Tell whether Codey is tilted to a certain direction. The result is True or False.
The direction could be one of "forward", "backward", "left", "right"

print(codey.is_tilted("forward")) # If Codey is tilted forward, return True

is_placed(position)

Tell if Codey is placed in a certain position. Return True or False. The position could be "screen_up", "screen_down", or "up_right".

print(codey.is_placed("up_right")) # if codey is placed up_right, return True.

gyro

Get the pitch or yaw value of the Gyro

  • gyro.pitch
  • gyro.yaw

Use gyro.rotation to get the gyro's rotation value (in degrees) in a certain axis.

  • gyro.rotation.x
  • gyro.rotation.y
  • gyro.rotation.z
  • gyro.rotation.reset(): reset the rotation angle of the gyro

You can use gyro.shake_strength to get the shake strength of the gyro (0-100)

print("The yaw and pitch is:", gyro.pitch, gyro.yaw) # output the yaw and pitch value of the gyro
print("The rotation value is:", gyro.rotation.x, gyro.rotation.y, gyro.rotation.z)
gyro.rotation.reset() # reset the rotation value. Now gyro.rotation.x, gyro.rotation.y, gyro.rotation.z are zeros.
print("Shake strength:", gyro.shake_strength)

time()

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

print(codey.time()) # print the timer value since startup or last reset

reset_time()

reset the timer

codey.reset_time() 
print(codey.time()) # prints 0

Codey's Event and Flow Control

Codey support 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 button_a_pressed(): # define a function
    print("Button A is pressed!")

codey.on_button('A', button_a_pressed) # register it to "when button A is pressed" event

on_button(button_name, function_name)

When the button is pressed, run the function.

def button_a_pressed():
    print("The A button is pressed")

codey.on_button('A', button_a_pressed)

on_shake(function_name)

When Codey is shaked, call the function

def when_shaked():
    print("I'm shaked!")

codey.on_shake('A', when_shaked)

on_tilt(direction, function_name)

Call the function when Codey is shaked direction could be one of "forward", "backward", "left", "right"

def when_tilted_forward():
    print("I'm tilted forward!")

codey.on_tilt('forward', when_tilted_forward)

on_sound_over(loudness, function_name)

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

def when_sound_over_50():
    print("The volume is over 50! Too loud!")

codey.on_sound_over(50, when_sound_over_50)

on_light_under(lightness, function_name)

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

def when_light_under_30():
    print("The light is under 30! too dark!")

codey.on_light_under(30, when_light_under_30)

on_message(message_name, function_name)

When received a Scratch broadcast, trigger the function.

def when_game_start():
    print("game start!")

codey.on_message('game_start', when_game_start)

message(message_name)

Broadcast a certain message to the Scratch system.

codey.message('game_start')

stop_all()

Stop all processes on Codey

codey.stop_all() # stop all processes on Codey

Let the Rocky move

forward(speed, duration=0)

Tell the Rocky to move forward for a certain duration. If the number is 0, 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

back(speed, duration=0)

Tell the Rocky to move backward for a certain duration. If the number is 0, it will keep moving.
The speed range is 0-100

rocky.back(50)
rocky.back(100) # run backward at the max speed of 100

right(speed, duration=0)

Tell the Rocky to turn right for a certain duration. If the number is 0, it will keep turning.
The speed range is 0-100

rocky.right(50, 1) # turn right at speed 50 for 1 second
rocky.right(100) # turn right at the max speed of 100

left(speed, duration=0)

Tell the Rocky to turn left for a certain duration. If the number is 0, it will keep turning.
The speed range is 0-100

rocky.left(50, 1) # turn left at speed 50 for 1 second
rocky.left(100) # turn left at the max speed of 100

right_angle(angle)

turn right at a certain angle until done

rocky.right_angle(90) # turn right for 90 degrees until done

left_angle(angle)

turn left at a certain angle until done

rocky.left_angle(90) # turn left for 90 degrees until done

drive(left_speed, right_speed)

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

Stop the movement of Codey

rocky.stop() # tell Codey to stop moving

Use Rokey's Sensors and Light

base_light(color, duration=0)

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

rocky.base_light("red")

base_light_off()

Turn off Rocky's LED.

rocky.base_light_off()

is_obstacle_ahead()

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

print(rocky.is_obstacle_ahead()) #

base_sensor

Get the value of the base sensor array.

  • base_sensor.lightness: get the lightness of the environment, returns 0-100
  • base_sensor.reflection: get the reflection light value of the surface, returns 0-100
  • base_sensor.ir_reflection: get the IR reflection value of the surface, returns 0-100
  • base_sensor.grey: get the greyness of the surface by light sensor, returns 0-100
  • base_sensor.red: get the red value of the surface, returns 0-255
  • base_sensor.green: get the green value of the surface, returns 0-255
  • base_sensor.blue: get the blue value of the surface, returns 0-255
  • base_sensor.is_color(color_name): Tell whether the color sensor senses the specific color, returns True or False the color_name could be one of "red", "green", "blue", "yellow", "purple", "cyan", "white", "black".
    print("Lightness and Reflection: ", base_sensor.lightness, base_sensor.reflection)
    print("IR light value: ", base_sensor.ir_reflection)
    print("Greyness: ", base_sensor.grey)
    print("RGB value: ", base_sensor.red, base_sensor.green, base_sensor.blue )
    print("On a red surface? ", base_sensor.is_color("red") )
    

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

codey.ir_send("A")

ir_receive()

Recevies a message sent with infrared signals. Returns a string value.

codey.show(codey.ir_receive())

results matching ""

    No results matching ""