Skip to main content

Case 05: Dazzling Lights

Introduction

Make Cutebot drive forward and display random light colors.

Programming Preparation

Please refer to: Preparing the Programming Environment

Sample code

from cutebot import *
from random import *
from time import *

# Create a sample for Cutebot category
cutebot = Cutebot()

# Set the cutebot smart car to drive forward at 50% speed
cutebot.set_speed(50,50)
# Initialize the rainbow light
cutebot.init_rainbow_leds()

# While true, change the color of the headlights and the Rainbow LED at random.
while True:
while True:
R = randint(0, 255)
G = randint(0, 255)
B = randint(0, 255)
cutebot.set_light(RGB.left,R,G,B)
cutebot.set_light(RGB.right,R,G,B)
cutebot.rainbow_leds[0] = (R,G,B)
cutebot.rainbow_leds[1] = (R,G,B)
sleep(1)

Code details

  1. Import the modules that we need for the program: cutebot module contains classes and functions that operate on Cutebot smart cars, time module contains functions that operate on time, random module contains functions that generate random numbers.
from cutebot import *
from random import *
from time import *
  1. Create a sample for Cutebot category
cutebot = Cutebot()
  1. Set the cutebot smart car to drive forward at 50% speed and initialize the rainbow lights.
cutebot.set_speed(50,50)
cutebot.init_rainbow_leds()
  1. While true, change the color of the headlights and the Rainbow LED at random.
while True:
R = randint(0, 255)
G = randint(0, 255)
B = randint(0, 255)
cutebot.set_light(RGB.left,R,G,B)
cutebot.set_light(RGB.right,R,G,B)
cutebot.rainbow_leds[0] = (R,G,B)
cutebot.rainbow_leds[1] = (R,G,B)
sleep(1)

Results

After powering on, the cutebot smart car moves forward and the lights change the colours at random.

Exploration

How do you make the lights switch colors automatically according to the speed of the cutebot smart car?