const int ledPin = 8;
const int buttonPin = 9;
const int switchPin = 10;
int buttonState;
int lastButtonState = HIGH;
bool ledState = LOW;
bool switchState;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // setup led
pinMode(buttonPin, INPUT_PULLUP); // setup button
pinMode(switchPin, INPUT_PULLUP); // setup switch
}
void loop() {
int reading = digitalRead(buttonPin); // Lee el estado del botón
switchState = digitalRead(switchPin); // Lee el estado del switch
// debounce
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (switchState == LOW) { // Si el switch está activado
version2(buttonState);
} else { // Si el switch no está activado
version1(buttonState);
}
}
}
lastButtonState = reading;
}
void version1(int buttonState) {
if (buttonState == LOW) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
Serial.println(ledState == HIGH ? "Led Encendido" : "Led Apagado");
}
}
void version2(int buttonState) {
Serial.print("Estado del botón: ");
Serial.println(buttonState == HIGH ? "Botón No Presionado" : "Botón Presionado");
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}