volatile int state = 1;
const int buttonPin = A1;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 100;
void setup() {
Serial.begin(115200);
GPIOB->MODER = 0x5555;
GPIOA->MODER = 0x0000;
analogReadResolution(12);
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterruptHandler, RISING);
}
void updateLEDMeter(int value) {
int ledsOn = (value + 31) / 32;
int ledPattern = (1 << ledsOn) - 1;
GPIOB->ODR = (GPIOB->ODR & ~0xFF) | ledPattern;
}
int led_toggle = 0;
void loop() {
int value = analogRead(A0) >> 4;
Serial.println(value);
if (state == 1) {
updateLEDMeter(value);
} else {
GPIOB->ODR &= ~0xFF;
}
delay(10);
}
void buttonInterruptHandler() {
unsigned long currentTime = millis();
if ((currentTime - lastDebounceTime) > debounceDelay) {
state = (state == 1) ? 0 : 1;
lastDebounceTime = currentTime;
}
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6