Why isn't the LED responding to voice commands in my MicroPython code?

Hey guys still based on my project on developing a home automation voice-controlled system using MicroPython and TinyML on an ESP32 microcontroller to recognize specific voice commands ("turn on" and "turn off") for controlling an LED, am currently testing the voice command recognition and validating LED control using simulated commands ("on", "off") But am getting no response from LED despite commands being printed correctly. My aim is to ensure the LED responds appropriately to the recognized voice commands, How can I resolve this? this is my code 👇
import time
import random
from machine import Pin

led_pin = Pin(2, Pin.OUT)

class LED:
def on(self):
led_pin.value(1)
print("LED is now ON.")

def off(self):
led_pin.value(0)
print("LED is now OFF.")

def process_audio():
commands = ["turn on", "turn off", "unknown command"]
return random.choice(commands)

led = LED()

while True:
command = process_audio()
print(f"Received command: {command}")

if command == "turn on":
led.on()
print("LED turned on.")
elif command == "turn off":
led.off()
print("LED turned off.")
else:
print("Command not recognized.")

time.sleep(1)
import time
import random
from machine import Pin

led_pin = Pin(2, Pin.OUT)

class LED:
def on(self):
led_pin.value(1)
print("LED is now ON.")

def off(self):
led_pin.value(0)
print("LED is now OFF.")

def process_audio():
commands = ["turn on", "turn off", "unknown command"]
return random.choice(commands)

led = LED()

while True:
command = process_audio()
print(f"Received command: {command}")

if command == "turn on":
led.on()
print("LED turned on.")
elif command == "turn off":
led.off()
print("LED turned off.")
else:
print("Command not recognized.")

time.sleep(1)
Solution:
Hi @Enthernet Code modify your SPI_PeripheralControl function to use a read-modify-write operation: ```c void SPI_PeripheralControl(SPI_RegDef_t *pSPIX, uint8_t EnOrDi)...
Jump to solution
2 Replies
Solution
wafa_ath
wafa_ath•3mo ago
Hi @Enthernet Code modify your SPI_PeripheralControl function to use a read-modify-write operation:
void SPI_PeripheralControl(SPI_RegDef_t *pSPIX, uint8_t EnOrDi)
{
uint16_t temp = pSPIX->CR1;
if(EnOrDi == ENABLE)
{
temp |= (1 << SPI_CR1_SPE);
}
else
{
temp &= ~(1 << SPI_CR1_SPE);
}
pSPIX->CR1 = temp;
}
void SPI_PeripheralControl(SPI_RegDef_t *pSPIX, uint8_t EnOrDi)
{
uint16_t temp = pSPIX->CR1;
if(EnOrDi == ENABLE)
{
temp |= (1 << SPI_CR1_SPE);
}
else
{
temp &= ~(1 << SPI_CR1_SPE);
}
pSPIX->CR1 = temp;
}
This approach preserves other bits in the CR1 register while modifying only the SPE bit.
Enthernet Code
Enthernet Code•3mo ago
Thanks @wafa_ath currently trying it out, would appreciate it more if u can send the python version of your code, Thanks for the help 🙌
Want results from more Discord servers?
Add your server