9. Mission 4 Use LED for Indicator

Share for us

In order to check whether the car is listening to commands and when it stops listening, we add LED to help.

In the code, add my_pismart.led = 80 before the listening, meaning to set the luminance of the LEDs as 80 when PiSmart’s waiting for commands. Then add my_pismart.led = 0 after my_pismart.heard to dim the LED after listening stops.

And the code will be like this:

GNU nano 2.2.6                            File: my_pismart.py
...
 
def loop():
    my_pismart.LED = 80              # turn on led
    my_pismart.listen
    if my_pismart.heard:
        my_pismart.LED = 0           # turn off led
        if my_pismart.result == "forward":
            # PiSmart Car move forward
            my_pismart.MotorA = 60
            my_pismart.MotorB = 60
            my_pismart.Say = "I go forward!"
            sleep(3)
        if my_pismart.result == "backward":
            # PiSmart Car backward
            my_pismart.MotorA = -60
            my_pismart.MotorB = -60
            my_pismart.Say = "I go backward!"
            sleep(3)
 
        if my_pismart.result == "left":
            # PiSmart Car turn left
            my_pismart.MotorA = 60
            my_pismart.MotorB = 20
            my_pismart.Say = "I turn left!"
            sleep(3)
 
        if my_pismart.result == "right":
            # PiSmart Car turn right
            my_pismart.MotorA = 20
            my_pismart.MotorB = 60
            my_pismart.Say = "I turn right!"
            sleep(3)
        my_pismart.MotorA = 0
        my_pismart.MotorB = 0
        sleep(0.5)
...
 
^G Get Help   ^O WriteOut    ^R Read File   ^Y Prev Page    ^K Cut Text       ^C Cur Pos
^X Exit       ^J Justify     ^W Where Is    ^V Next Page    ^U UnCut Text     ^T To Spell


Press Ctrl + O to save and Ctrl + X to exit, and rerun my_pismart.py:

pi@raspberrypi:~/SunFounder_PiSmart_Car/ $ nano my_pismart.py

The program will initialize the speech recognition engine after running this part. When the LED ring lights up (steadily), it is ready to listen to your commands. After one is received, the LED ring will go out and the car will take corresponding actions. Then the LED ring will light up again, meaning the car gets ready for the next command.

You can make various patterns by the LEDs such as using blinking LED to simulate Morse Code or make a “breathing” LED by luminance. Just go to play with your creation to make cooler things!

With the 4 missions above, the basic design of this interactive smart car is finished. You can refer to our code for more details.

Download the code on our github page:

https://github.com/sunfounder/SunFounder_PiSmart_Car.git

Download by git clone:

pi@raspberrypi:~ $ git clone https://github.com/sunfounder/SunFounder_PiSmart_Car.git

Then cd (change directory) to the examples directory, and view the file:

pi@raspberrypi:~ $ cd SunFounder_PiSmart_Car/examples/

pi@raspberrypi:~/SunFounder_PiSmart_Car/examples $ ls

avoidance.py  command.sps  line_follower_module.py  line_track.py  my_pismart  remote_control

The directory my_pismart is the example code of above.

More: For further control of the car, you need to know more about basic Python programming by reading relevant books.

As for the subsequent For Advanced Users, if you’re a beginner, you can try experiencing it by connecting cables, installing modules, and running the code according to the instruction. You can also try to read the code and check whether it’s comprehensible to you.