Lights On¶
This primer will take you through the steps to learning how to control your CodeBot with Python. All you need is your CodeBot and a Chrome Web Browser.
Ready to get started? Open your browser to make.firialabs.com and let’s do this!
1. Turn on a User LED¶
Blinking an LED is often the first “Hello World” program for embedded devices like CodeBot. As a first step, let’s light one of the “User” LEDs in the center of the ‘bot.
Type the following into CodeSpace. Remember, you don’t have to type in the
#comments
.
# Very first CodeBot program - Lights ON!
from botcore import *
leds.user_num(0, True)
The code above uses the botcore
module, which provides functions to control and monitor all the hardware
on CodeBot. Take a look at the documentation for botcore.CB_LEDs.user_num
to see how this function works.
2. A sequence of LEDs¶
Next try lighting more of the User LEDs, as shown below.
Note: to slow things down a bit, this code imports the
time
Python module. The functiontime.sleep
adds a delay before each LED turns on.
from botcore import *
import time
leds.user_num(0, True)
time.sleep(1.0)
leds.user_num(1, True)
time.sleep(1.0)
leds.user_num(2, True)
time.sleep(1.0)
leds.user_num(3, True)
3. Blink an LED in an infinite loop¶
- Now let’s try an infinite
while
loop in Python. The code that’s indented beneathwhile True:
will repeat continuously. Do you see how to exit the loop?
from botcore import *
import time
while True:
leds.user_num(0, True)
time.sleep(1.0)
leds.user_num(0, False)
time.sleep(1.0)
if buttons.is_pressed(0):
break
The exit code above uses botcore.Buttons.is_pressed
to detect if BTN-0 is being pressed. If it is, The break statement
breaks the code out of the loop!
Experiment with other LEDs on CodeBot…
CodeBot has many more LEDs to try. Explore botcore.CB_LEDs
to find out how to use them, and do some experiments.
For example:
leds.ls_num(n, is_on) # Line Sensors 0-4
leds.prox_num(n, is_on) # Proximity Sensors 0-1
And can you guess what these do?:
leds.user([1,0,1,0,1,0,1,0]) # 8-bits of LED fun!
leds.ls([1,1,0,1,1])
leds.prox([1,1])