In this video lesson we continue to expand our skills in using AI and Speech to Text (SST) capability to control our project via voice commands. In this lesson, we explore using voice commands to control the brightness of an LED. We use threading so it does not have a blocking issue. We are using the red channel of the RGB LED in order to demonstrate this capability.
This is the schematic we are using, from LESSON #5.

In the video, this is the code we developed:
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 | from fusion_hat.pwm import PWM from fusion_hat.stt import STT import threading from queue import Queue from time import sleep redPin = 5 redLED = PWM(redPin) redLED.freq(1000) stt = STT(language ="en-us") brightness = "off" bNumb = 0 running = True brightQ = Queue() def blinkBright(): print("Input Thread is Running") global running while running: print("What Brightness: (off, low, medium, high, on or quit: )") cmd = stt.listen(stream = False) cmd = cmd.strip() print(cmd) if cmd == "quit": running = False break brightQ.put(cmd) print("Thread is Terminated") brightThread = threading.Thread(target = blinkBright, daemon = True) brightThread.start() print("Main Program Has Started") try: while running: if brightQ.empty() == False: brightness = brightQ.get() if brightness == "quit": break if brightness == "off": bNumb = 0 if brightness == "low" or brightness == "whoa": bNumb = 8 if brightness == "medium": bNumb = 25 if brightness == "high" or brightness == "hi": bNumb = 60 if brightness == "on": bNumb = 100 redLED.pulse_width_percent(bNumb) redLED.pulse_width_percent(0) redLED.enable(False) print("LED Released") print("Program is Terminated") except KeyboardInterrupt: running = False redLED.pulse_width_percent(0) redLED.enable(False) print("LED Released") print("Program Terminated") |