This error correcting code.

Hello:

I made a code that reads a digital input on the "Arduino Uno R3". While Lee and trouble tickets antirrebote.

The code also makes the Arduino reset or turn it back on, set the PC sends the current status of the switch port can be HIGH or LOW.

If Arduino reset switch and digital input is LOW, it is shown in the Monitor Series LOW word. If I leave the switch in HIGH, the Monitor series shown twice HIGH word this way.

HIGH HIGH

HIGH must only show once.

This is the code.

byte estadoBoton = 0; // It will keep the state of the HIGH or LOW button.
byte estadoBotonAnt = 0;
char caracter;
String comando;
int boot = 0;

void setup()
{
  pinMode(13,OUTPUT); // Where the LED 13.
  pinMode(8,INPUT);   
  Serial.begin(115200);
}

void loop() {
    
    estadoBoton = digitalRead(8); // Read digital input No. 8.

        // prints the state to turn Arduino
    if(boot == 0 && estadoBoton == 0){
      Serial.println("LOW");
      boot++;
    }else if(boot == 0 && estadoBoton == 1){
      Serial.println("HIGH");
      boot++;
    }

  // If the button is pressed, the LED lights 13 HIGH 
  //command and sends through the serial port.
    if (estadoBoton == HIGH && estadoBotonAnt == LOW)
    {
        digitalWrite(13,HIGH);
        Serial.println("HIGH");
        delay(50);
    }
    
    // Otherwise, Led LOW 13 Epagado and sends the serial port.
    if (estadoBoton == LOW && estadoBotonAnt == HIGH) {
        digitalWrite(13,LOW);
        Serial.println("LOW");
        delay(50);
    }
     estadoBotonAnt = estadoBoton;
}

Regards.

this block is printing "HIGH" the first time... do you need this?

    // prints the state to turn Arduino
    if(boot == 0 && estadoBoton == 0){
      Serial.println("LOW");
      boot++;
    }else if(boot == 0 && estadoBoton == 1){
      Serial.println("HIGH");
      boot++;
    }

FYI this both evaluate as true:

HIGH == 1
LOW == 0

Lose you boot-time printing completely and do this:

  estadoBoton = digitalRead(8); // Read digital input No. 8.
  if (boot == 0)
  {
    estadoBotonAnt = estadoBoton ;
    boot++ ;
  }

Then the first read of the button will not trigger action as the previous value variable is set to be the same. This a cheating way to represent there being no previous reading.

Hi:

What is the best way to know when the switch is in 1 = 0 = HIGH or LOW and displayed in the Monitor Series when reset or turn Arduino UNO?

Regards.

To get the state of a pin (HIGH or LOW) use digitalRead.

Hello:

I've done it again the code. If I leave the switch in HIGH or 1 shown in the Mensagem Monitor Series HIGH, if I leave the interruprot or 0 to LOW, LOW infinitely many shows.

byte estadoBoton = 0; // Guardará el estado del botón HIGH o LOW.
byte estadoBotonAnt = 0;
char caracter;
String comando;

void setup()
{
  pinMode(13,OUTPUT); // Donde está el Led 13.
  pinMode(8,INPUT);   //
  Serial.begin(115200);

  estadoBoton = digitalRead(8);

  // imprime el estado al encender arduino
  if(estadoBoton == 0){
    Serial.println("LOW");
  }
  
  if (estadoBoton == 1){
    Serial.println("HIGH");
  }
    estadoBotonAnt = estadoBoton;
}

void loop() {

  estadoBoton = digitalRead(8);

    // Si el pulsador está pulsado, se enciende el Led 13 y
    // envía comando HIGH por el puerto serie.
    if (estadoBoton == HIGH && estadoBotonAnt == LOW)
    {
        digitalWrite(13,HIGH);
        Serial.println("HIGH");
        delay(50);
    }
    
    // De lo contrario, Led 13 epagado y envía LOW al puerto serie.
    if (estadoBoton == LOW && estadoBotonAnt == HIGH) {
        digitalWrite(13,LOW);
        Serial.println("LOW");
        delay(50);
    }
}

Regards.

Someday, I'm sure you'll tell us how the switch is wired, and why you are doing it the hard way.

The switch is push-up.

|500x183

There is no push up anything. Are you using the circuit on the left, or the one on the right?

There is no push up anything.

Well, there ARE pushup bras. But, I doubt that they’ll do much except side track the discussion…

add this line:

    estadoBotonAnt = estadoBoton;

to the end of your loop

Hi:

The code so I understand them so:

byte estadoBoton = 0; // Guardará el estado del botón HIGH o LOW.
byte estadoBotonAnt = 0;
char caracter;
String comando;

void setup()
{
  pinMode(13,OUTPUT); // Donde está el Led 13.
  pinMode(8,INPUT);   //
  Serial.begin(115200);

  estadoBoton = digitalRead(8);

  // imprime el estado al encender arduino
  if(estadoBoton == 0){
    Serial.println("LOW");
  }else{
    Serial.println("HIGH");
  }
}

void loop() {

  estadoBoton = digitalRead(8);

    // Si el pulsador está pulsado, se enciende el Led 13 y
    // envía comando HIGH por el puerto serie.
    if (estadoBoton == HIGH && estadoBotonAnt == LOW)
    {
        digitalWrite(13,HIGH);
        Serial.println("HIGH");
        delay(50);
    }
    
    // De lo contrario, Led 13 epagado y envía LOW al puerto serie.
    if (estadoBoton == LOW && estadoBotonAnt == HIGH) {
        digitalWrite(13,LOW);
        Serial.println("LOW");
        delay(50);
    }

        estadoBotonAnt = estadoBoton;
}

Everything works fine except one thing. Whenever I have the switch on HIGH or 1 when resetting Arduino UNO I think in the Monitor Series HIGH twice when in fact it has to be once.

You have to appear in only one HIGH "Monitor Series" to reset or turn Arduino UNO.

;)

You read the pin in setup() and assign the value to estadoBoton. You read the pin again in loop() and assign the value to estadoBoton. Then, you compare estadoBoton to estadoBotonAnt.

You need to assign the value read in setup() to estadoBotonAnt, and NOT print anything.

Hi:

Finally it works !!!

byte estadoBoton = 0; // Guardará el estado del botón HIGH o LOW.
byte estadoBotonAnt = 0;
char caracter;
String comando;

void setup()
{
  pinMode(13,OUTPUT); // Donde está el Led 13.
  pinMode(8,INPUT);   //
  Serial.begin(115200);

  estadoBoton = digitalRead(8);

  // imprime el estado al encender arduino
  if(estadoBoton == 0){
    Serial.println("LOW");
  }else{
    Serial.println("HIGH");
  }
  estadoBotonAnt = estadoBoton;
}

void loop() {

  estadoBoton = digitalRead(8);

    // Si el pulsador está pulsado, se enciende el Led 13 y
    // envía comando HIGH por el puerto serie.
    if (estadoBoton == HIGH && estadoBotonAnt == LOW)
    {
        digitalWrite(13,HIGH);
        Serial.println("HIGH");
        delay(50);
    }
   
    // De lo contrario, Led 13 epagado y envía LOW al puerto serie.
    if (estadoBoton == LOW && estadoBotonAnt == HIGH) {
        digitalWrite(13,LOW);
        Serial.println("LOW");
        delay(50);
    }

        estadoBotonAnt = estadoBoton;
}

Thanks to all.