const int led1 = 13;
const int led2 = 12;
const int led3 = 11;
const int btn1 = 10;
const unsigned long THRESH1 = 1000;
const unsigned long THRESH2 = 3000;
const unsigned long DEBOUNCE_MS = 40;
unsigned long pressTime = 0;
unsigned long lastBounceTime = 0;
bool buttonPressed = false;
int lastButtonState = HIGH;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(btn1, INPUT_PULLUP);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}
void loop() {
int reading = digitalRead(btn1);
unsigned long now = millis();
if (reading != lastButtonState) {
lastBounceTime = now;
lastButtonState = reading;
}
if (now - lastBounceTime < DEBOUNCE_MS) {
return;
}
if (reading == LOW && !buttonPressed) {
pressTime = now;
buttonPressed = true;
}
if (reading == LOW && buttonPressed) {
unsigned long duration = now - pressTime;
if (duration < THRESH1) {
digitalWrite(led1, HIGH); digitalWrite(led2, LOW); digitalWrite(led3, LOW);
} else if (duration <= THRESH2) {
digitalWrite(led1, LOW); digitalWrite(led2, HIGH); digitalWrite(led3, LOW);
} else {
digitalWrite(led1, LOW); digitalWrite(led2, LOW); digitalWrite(led3, HIGH);
}
}
if (reading == HIGH && buttonPressed) {
buttonPressed = false;
digitalWrite(led1, LOW); digitalWrite(led2, LOW); digitalWrite(led3, LOW);
}
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6