In this lesson we bring together the things we have learned in the earlier 13 lessons to create a project on the Raspberry Pi 5 that we interact with through voice commands, instead of the keyboard and screen. The project is to audibly prompt us for our favorite color. It will then audibly acknowledge our color choice, and then turn the RGB LED that color. This is my solution to the homework assignment I gave in LESSON 13.
This is the schematic we are using for the project:

This is the code we developed in the video:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | from fusion_hat.pwm import PWM from fusion_hat.stt import STT import threading from queue import Queue from time import sleep from fusion_hat.tts import Piper tts = Piper() tts.set_model('en_US-kristin-medium') msg = 'Speak your favorite color, and your wish is my command' tts.say(msg,stream=False) redPin = 5 greenPin = 6 bluePin = 7 redLED= PWM(redPin) greenLED=PWM(greenPin) blueLED=PWM(bluePin) rVal=0 gVal=0 bVal=0 stt = STT('en-us') running=True colorQ = Queue() def getColor(): print("Input Thread is Running") global running while running: print('What color: red, green, blue, cyan, magenta, yellow, off, quit') myColor = stt.listen(stream=False) myColor=myColor.strip() if myColor == 'quit': running = False msg = 'So Sorry to See you go, Please Call Me Again Soon' tts.say(msg, stream=False) break colorQ.put(myColor) print("Thread is Terminated") colorThread= threading.Thread(target=getColor,daemon=True) colorThread.start() print("Main Program is Started") try: while running: if colorQ.empty() == False: myColor = colorQ.get() print('Color: ',myColor) if myColor == 'off': rVal = 0 gVal = 0 bVal = 0 msg = 'Please bring back your beautiful colors!' tts.say(msg, stream=False) if myColor == 'red' or myColor=='read': rVal = 100 gVal = 0 bVal = 0 msg = 'I can not get you out of my head, so I will turn it red' tts.say(msg, stream=False) if myColor == 'green': rVal = 0 gVal = 100 bVal = 0 msg = 'Because I want you to be seen, I will turn it green' tts.say(msg, stream=False) if myColor == 'blue': rVal = 0 gVal = 0 bVal = 100 msg = 'Because I love you, I will turn it blue' tts.say(msg, stream=False) if myColor == 'cyan': rVal = 0 gVal = 100 bVal = 25 msg = 'You turn my world cyan and bright, what a beautiful sight!' tts.say(msg, stream=False) if myColor == 'magenta': rVal = 100 gVal = 0 bVal = 100 msg = 'You Light my Magenta Fire, You are my burning Desire' tts.say(msg, stream=False) if myColor == 'yellow': rVal = 100 gVal = 25 bVal = 0 msg = 'You are such a handsome fellow, I will turn it yellow' tts.say(msg, stream=False) myColor = 'null' redLED.pulse_width_percent(rVal) greenLED.pulse_width_percent(gVal) blueLED.pulse_width_percent(bVal) redLED.pulse_width_percent(0) greenLED.pulse_width_percent(0) blueLED.pulse_width_percent(0) redLED.enable(False) greenLED.enable(False) blueLED.enable(False) print("LEDs are Released") print("Program is Terminated") except KeyboardInterrupt: running = False redLED.pulse_width_percent(0) greenLED.pulse_width_percent(0) blueLED.pulse_width_percent(0) redLED.enable(False) greenLED.enable(False) blueLED.enable(False) print("LEDs are Released") print("Program is Terminated") |