7. Case 04: Turn Signals Automatically#

7.1. Introduction#

Make Cutebot follow a random route and turn on the turn signal automatically.

7.2. Programming Preparation#

Please refer to: Preparing Programming Environment

7.3. Sample code#

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

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

# Assign the random value among (0,100) to the speed for the two wheels, turn on the sigals light automatically in accordance with the driving directions of the Cutebot. 
while True:
    left_speed = randint(0, 100)
    right_speed = randint(0, 100)
    if left_speed > right_speed:
        cutebot.set_light(RGB.left,0,0,0)
        cutebot.set_light(RGB.right,255,255,0)
    if left_speed < right_speed:
        cutebot.set_light(RGB.left,255,255,0)
        cutebot.set_light(RGB.right,0,0,0)
    cutebot.set_speed(left_speed,right_speed)
    sleep(2)

Code details#

  1. Import the modules that we need for the program:cutebot module contains the classes and functions that operate on Cutebot smart cars, time module contains the functions that operate on time, random module contains the functions that generate random numbers.

from cutebot import *
from random import *
from time import *
  1. Create a sample for Cutebot category

cutebot = Cutebot()
  1. Assign the random value among (0,100) to the speed for the two wheels, turn on the sigals light automatically in accordance with the driving directions of the Cutebot.

while True:
    left_speed = randint(0, 100)
    right_speed = randint(0, 100)
    if left_speed > right_speed:
        cutebot.set_light(RGB.left,0,0,0)
        cutebot.set_light(RGB.right,255,255,0)
        if left_speed < right_speed:
            cutebot.set_light(RGB.left,255,255,0)
            cutebot.set_light(RGB.right,0,0,0)
            cutebot.set_speed(left_speed,right_speed)
    sleep(2)

7.4. Results#

After turning on the power, the cutebot smart car travels on a random trajectory and automatically lights up the turn signal according to the direction the smart car is traveling.

7.5. Exploration#

When the speed of the left and right wheels of the car is the same, how can we program the two headlights of the cutebot smart car to light up at the same time?