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;
}
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.
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);
}
}
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.
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;
}