/*
Arduino | coding-help
code
paraquedista2_ — 10/28/24 at 3:28 PM
guys what do i need to do for this code to work this way:
when i press the button for more than 2 seconds, the leds blink 3 times
*/
// Definir pinos aos leds e botao
const int BTN_PIN = 5;
const int LED_PINS[] = {10, 8, 6, 7, 11, 12, 9};
const unsigned long LONG_PRESS = 2000;
unsigned long tempoPressionado = 0;
int oldBtnState = HIGH; // INPUT_PULLUP
bool wasPressed = false;
void checkButton() {
int btnState = digitalRead(BTN_PIN);
if (btnState != oldBtnState) {
oldBtnState = btnState;
if (btnState == LOW) {
Serial.println("Button pressed");
tempoPressionado = millis();
wasPressed = true;
} else {
Serial.println("Button released");
wasPressed = false;
}
delay(20); // debounce
}
}
void doStartBlinks() {
int piscadas = 0;
while (piscadas < 3) {
for (int pin = 0; pin < 7; pin++) {
digitalWrite(LED_PINS[pin], HIGH);
}
delay(500);
for (int pin = 0; pin < 7; pin++) {
digitalWrite(LED_PINS[pin], LOW);
}
delay(500);
piscadas = piscadas + 1;
}
}
void setup() {
Serial.begin(9600);
for (int pin = 0; pin < 7; pin++) {
pinMode(LED_PINS[pin], OUTPUT);
}
pinMode(BTN_PIN, INPUT_PULLUP);
}
void loop() {
checkButton();
if (wasPressed == true) {
if (millis() - tempoPressionado >= LONG_PRESS) {
doStartBlinks();
wasPressed = false;
}
else {
for (int pin = 0; pin < 7; pin++) {
digitalWrite(LED_PINS[pin], HIGH);
delay(100);
digitalWrite(LED_PINS[pin], LOW);
}
}
}
}