So I thought I had the code working but Ive got a huge problem.
If I hold the button down the led still turns on and I really dont know what to do anymore.
The led should only turn on AFTER I release the button, and 3 seconds have passed, and everytime I oush the button within the span of less than 3 seconds continously, then release for 3 seconds the led should light up as many times as I clicked the button.
Also when I start the simulation in proteus the led lights up one time before any input, like it just does it one time without any action from the nutton
It is best if you wire up buttons between the input pin and ground. Then enable the internal pull up resistor in the pin mode statement in the setup function.
Your code should look for a LOW being returned from a digitalRead of the pin number when it is pressed.
const int LED_PIN = 2;
const int BTN_PIN = 3;
const unsigned long blinkDuration = 3000; // Duración de parpadeo predeterminada en milisegundos (5 segundos)
int oldState = HIGH; // El estado inicial del botón es alto (debido a INPUT_PULLUP)
int counter = 0;
unsigned long lastButtonPressTime = 0; // Variable para almacenar el tiempo del último botón presionado
bool counting = false; // Variable para indicar si se está contando clics
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
}
void checkButton() {
int state = digitalRead(BTN_PIN);
if (state != oldState) { // Si el estado cambió
oldState = state; // Recordar el estado anterior
if (state == LOW) { // Si el botón está presionado
counter++;
lastButtonPressTime = millis(); // Guardar el tiempo del último botón presionado
counting = true; // Empezar a contar
}
delay(20); // Debounce
}
}
void loop() {
checkButton();
// Si estamos contando y ha pasado el tiempo de duración del parpadeo
if (counting && millis() - lastButtonPressTime >= blinkDuration) {
counting = false; // Detener la cuenta
for (int i = 0; i < counter; i++) {
digitalWrite(LED_PIN, HIGH);
delay(200); // Encender el LED durante 200ms
digitalWrite(LED_PIN, LOW);
delay(200); // Apagar el LED durante 200ms
}
counter = 0; // Reiniciar el contador después de parpadear el LED
}
}
@monloca
I modified your code, added a button test. (lines 20 to 22).
If the button is kept pressed, the blinking time is always updated.
const int LED_PIN = 2;
const int BTN_PIN = 3;
const unsigned long blinkDuration = 5000; // Duración de parpadeo predeterminada en milisegundos (5 segundos)
int oldState = HIGH; // El estado inicial del botón es alto (debido a INPUT_PULLUP)
int counter = 0;
unsigned long lastButtonPressTime = 0; // Variable para almacenar el tiempo del último botón presionado
bool counting = false; // Variable para indicar si se está contando clics
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
Serial.println("Button hasn't been pressed yet.");
}
void checkButton() {
int state = digitalRead(BTN_PIN);
while (!digitalRead(BTN_PIN)) {
lastButtonPressTime = millis();
}
if (state != oldState) { // Si el estado cambió
oldState = state; // Recordar el estado anterior
if (state == LOW) { // Si el botón está presionado
counter++;
lastButtonPressTime = millis(); // Guardar el tiempo del último botón presionado
counting = true; // Empezar a contar
Serial.print("Button has been pressed ");
Serial.print(counter);
Serial.println(" times.");
}
delay(20); // Debounce
}
}
void loop() {
checkButton();
// Si estamos contando y ha pasado el tiempo de duración del parpadeo
if (counting && millis() - lastButtonPressTime >= blinkDuration) {
counting = false; // Detener la cuenta
for (int i = 0; i < counter; i++) {
digitalWrite(LED_PIN, HIGH);
delay(200); // Encender el LED durante 200ms
digitalWrite(LED_PIN, LOW);
delay(200); // Apagar el LED durante 200ms
}
counter = 0; // Reiniciar el contador después de parpadear el LED
}
}