0% found this document useful (0 votes)
16 views2 pages

Entradas Digitales

The document shares a code block to avoid using delay and still achieve proper button behavior. It defines constants for the switch and LED ports, a boolean for the LED state, and previous input state. In setup, it initializes serial, the ports as input/output. The loop reads the current input, and if it differs from previous, toggles the LED state and writes it, before updating the previous state.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Entradas Digitales

The document shares a code block to avoid using delay and still achieve proper button behavior. It defines constants for the switch and LED ports, a boolean for the LED state, and previous input state. In setup, it initializes serial, the ports as input/output. The loop reads the current input, and if it differs from previous, toggles the LED state and writes it, before updating the previous state.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

//CODIGO

int led = 5;

int boton = 3;

void setup() {

  Serial.begin(9600);

  pinMode(boton, INPUT);

  pinMode(led, OUTPUT);

void loop() {

  bool estado_pulsador = digitalRead(boton);

  if(estado_pulsador == false){

    if (digitalRead(led) == true ){

      digitalWrite(led,LOW);

      delay(500);

    }else{

      digitalWrite(led,HIGH);

      delay(500);

  }

 }

}
Comparto un blóque de código para evitar el uso de delay y, a la vez, lograr un comportamiento
adecuado del pulsador.

1. const int SWITH_PORT = 3;


2. const int LED_PORT = 5;
3. bool led_port_state = false;
4. bool previus_input_state = false;
5. void setup() {
6. Serial.begin(9600);
7. pinMode(SWITH_PORT, INPUT);
8. pinMode(LED_PORT, OUTPUT);
9. }
10.  
11. void loop() {
12. bool current_input_state = !digitalRead(SWITH_PORT);
13. if (current_input_state &&
14. (previus_input_state != current_input_state)){
15.
16. led_port_state = !led_port_state;
17. digitalWrite(LED_PORT, led_port_state);
18. }
19. previus_input_state = current_input_state;
20. }

You might also like