7. Mission 2 Let It Speak

Share for us

Since the car can walk now, let’s check how to let it speak!

A speaker is integrated on PiSmart. We can let PiSmart speak to realize the speech message feedback. In this part, let’s upgrade the code so as to let the car tell us what it is doing. 

Use nano to change the previous code (only the modified part will be shown hereafter)

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

Add my_pismart.Say = “content” after each action and before sleep(). Then it will be like below:

GNU nano 2.2.6                                                File: my_pismart.py
… …
… …
def loop():
    # PiSmart Car move forward
    my_pismart.MotorA = 60 
    my_pismart.MotorB = 60 
    my_pismart.Say = "I go forward!"  # my pysmart say “I go forward”
    # PiSmart Car stop
    my_pismart.MotorA = 0 
    my_pismart.MotorB = 0 
    my_pismart.Say = "I stop!"        # my pysmart say “I stop”
    # PiSmart Car backward
    my_pismart.MotorA = -60 
    my_pismart.MotorB = -60 
    my_pismart.Say = "I go backward!" # my pysmart say “I go backward”
    # PiSmart Car turn left
    my_pismart.MotorA = 60 
    my_pismart.MotorB = 20 
    my_pismart.Say = "I turn left!"   # my pysmart say “I turn left”
    # PiSmart Car turn right
    my_pismart.MotorA = 20
    my_pismart.MotorB = 60
    my_pismart.Say = "I turn right!"  # my pysmart say “I turn right”
… …
… …
[ Read 25 lines ]
^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 run the code then.

pi@raspberrypi:~ /my_pismart $ python my_pismart.py

The car will tell you what it has done.

my_pismart.Say = “I go forward!”, is to make my PiSmart say “I go forward!“.

The robot will speak this sentence. If you want it to speak other else, just change I go forward! inside the quotation mark to others.