Descripción.
Este sketch se encuentra completamente basado en el anterior al cual se le modifica su funcionalidad permitiendo manipular un estado para el LED, es decir, cuando se presiona el botón el LED se enciende, aún sin necesidad de que el usuario final continúe presionandolo; este comportamiento se mantendrá hasta que el usuario presione nuevamente el botón para apagar el LED.
Ya que los cambios introducidos desde el sketch anterior corresponden a su funcionalidad, el hardware permanece sin variaciones y estas son introducidas en el software.
Implementación.
Software.
#define LED 13 // Digital PIN for the LED #define BUTTON 7 // Digital PIN for the Button int state = 0; // The current state of the LED int lastState = 0; // The past state of the button void setup () { pinMode(LED, OUTPUT); // LED pin is output: turn it on/off pinMode(BUTTON, INPUT); // Button pin is input: read it } void loop() { int currentState = digitalRead(BUTTON); // Consider a button's change of state only when is pressed if (lastState == LOW && currentState == HIGH) { state = !state; delay(10); } lastState = currentState; digitalWrite(LED, state); }
One Reply to “Modificación del segundo sketch: The LED with button and state”