PA 08 00 Control led pulsador. Toma contacto sensores.
const int buttonPin = 2;
const int ledPin = 9;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // Encender el LED cuando se presiona el botón
} else {
digitalWrite(ledPin, LOW); // Apagar el LED cuando se suelta el botón
}
}
PA 08 01 Control led pulsador. On/Off.
const int buttonPin = 2; // Definir Pin del pulsador
const int ledPin = 9; // Definir Pin del LED
bool ledState = false;
bool lastButtonState = LOW;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Configurar el botón con resistencia pull-up
pinMode(ledPin, OUTPUT); // Configurar el Pin de salida
}
void loop() {
bool buttonState = digitalRead(buttonPin); // Define una variable y le asigna el valor de pulsador
if (buttonState == LOW && lastButtonState == HIGH) { // Compara el valor del oulsador con su último estado
ledState = !ledState; // Alternar estado del LED
if (ledState) { // Este if se puede sustiyuir por: digitalWrite(ledPin, ledState ? HIGH : LOW);
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
} delay(200); // Pequeño retardo para evitar rebotes
}
lastButtonState = buttonState; // Asigna el último valor del estado del pulsador para el siguiente ciclo
}
const int buttonPin = 2;
const int ledPin = 9;
bool ledState = false;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
delay(200);
while (digitalRead(buttonPin) == LOW);
ledState = !ledState;
digitalWrite(ledPin, ledState);
}
}