Hola,
Estoy haciendo un programa, que dependiendo del numero de veces que hemos pulsado un pulsador haga una determinada tarea.(tipo Menú)
EL problema es que en las tareas que hay DELAY, a la que pulso el pulsador, aveces no me hace caso,(tengo que dejarlo pulsado algún tiempo).
Como podría solucionarlo? Gran parte del programa lo he copiado de "Button State Change".
Puede ser que deba utilizar "Case" en vez de if?
Os adjunto el Sketch y os explico lo que hace:
Cuenta las veces que hemos pulsado el boton.
En la 5ª Pulsación se alternan 2 Leds (puedes escojer la frecuencia con un potenciometro) AQUI TENGO QUE MANTENER PULSADO EL PULSADOR PARA QUE CAMBIE
En la 6ª Pulsación parpadean 2 Leds conjuntamente (puedes escojer la frecuencia con un potenciometro) AQUÍ TENGO QUE MANTENER PULSADO EL PULSADOR PARA QUE CAMBIE
const int buttonPin = 4; // the pin that the pushbutton is attached to
const int LedPinL = 10; // the pin that the LED is attached to
const int LedPinR = 11;
const int potPin = 2; // Pin Entrada Analogico
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(LedPinL, OUTPUT);
pinMode(LedPinR, OUTPUT); // Led Right
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter, DEC);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
if (buttonPushCounter == 5) {
int val = analogRead(potPin); // Lee el valor del Potenciometro
val = map(val, 0, 1023, 1000, 33);
digitalWrite(LedPinL, HIGH); // Enciende el led segun el valor del potenciometro/4
digitalWrite(LedPinR, LOW);
delay(val);
digitalWrite(LedPinL, LOW);
digitalWrite(LedPinR, HIGH);
delay(val);
}
if (buttonPushCounter == 6) {
int val = analogRead(potPin); // Lee el valor del Potenciometro
val = map(val, 0, 1023, 1000, 33);
digitalWrite(LedPinL, HIGH); // Enciende el led segun el valor del potenciometro/4
digitalWrite(LedPinR, HIGH);
delay(val);
digitalWrite(LedPinL, LOW);
digitalWrite(LedPinR, LO);
delay(val);
}
if (buttonPushCounter == 10) {
digitalWrite(LedPinL, HIGH);
}
else {
digitalWrite(LedPinL, LOW);
}
if (buttonPushCounter > 10) {
buttonPushCounter = 0;
}
}
Muchas Gracias.
Víctor