Mejorar Código Push Button
INPUT_PULLUP, de modo que el estado normal será HIGH y al presionar cambiará a LOW.NotasCpp
int bot[] = {1, 2, 3}; int led[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; int numLeds = 10; int numBots = 3; int tiempoRet = 400; int tiempoRetOciosa = 200; int estePin; int cuenta_der = 0; int cuenta_alto = 0; int cuenta_izq = 0; int cuenta_ocio = 0; boolean bot_derecha; boolean bot_alto; boolean bot_izquierda; // Función setup void setup() { // Configura los pulsadores usando el pull-up interno for (estePin = 0; estePin < numBots; estePin++) { pinMode(bot[estePin], INPUT_PULLUP); } for (estePin = 0; estePin < numLeds; estePin++) { pinMode(led[estePin], OUTPUT); } } // Función loop void loop() { // Apaga todos los LEDs antes de encender los correspondientes for (estePin = 0; estePin < numLeds; estePin++) { digitalWrite(led[estePin], LOW); } // Con pull-up, el botón sin presionar da HIGH y presionado LOW bot_derecha = digitalRead(bot[0]); bot_alto = digitalRead(bot[1]); bot_izquierda = digitalRead(bot[2]); if (bot_derecha == LOW) { Derecha(); } else if (bot_alto == LOW) { Alto(); } else if (bot_izquierda == LOW) { Izquierda(); } else { Ociosa(); } } // Función Derecha void Derecha() { switch (cuenta_der) { case 0: digitalWrite(led[0], HIGH); digitalWrite(led[8], HIGH); digitalWrite(led[6], HIGH); break; case 1: digitalWrite(led[1], HIGH); digitalWrite(led[9], HIGH); digitalWrite(led[5], HIGH); break; case 2: digitalWrite(led[2], HIGH); digitalWrite(led[3], HIGH); digitalWrite(led[4], HIGH); break; default: break; } delay(tiempoRet); cuenta_der++; if (cuenta_der > 3) { cuenta_der = 0; } } // Función Alto void Alto() { if (cuenta_alto == 1) { for (estePin = 0; estePin < numLeds; estePin++) { digitalWrite(led[estePin], HIGH); } } else { for (estePin = 0; estePin < numLeds; estePin++) { digitalWrite(led[estePin], LOW); } } delay(tiempoRet); cuenta_alto++; if (cuenta_alto > 1) { cuenta_alto = 0; } } // Función Izquierda void Izquierda() { switch (cuenta_izq) { case 0: digitalWrite(led[2], HIGH); digitalWrite(led[9], HIGH); digitalWrite(led[4], HIGH); break; case 1: digitalWrite(led[1], HIGH); digitalWrite(led[8], HIGH); digitalWrite(led[5], HIGH); break; case 2: digitalWrite(led[0], HIGH); digitalWrite(led[7], HIGH); digitalWrite(led[6], HIGH); break; default: break; } delay(tiempoRet); cuenta_izq++; if (cuenta_izq > 3) { cuenta_izq = 0; } } // Función Ociosa void Ociosa() { digitalWrite(led[cuenta_ocio], HIGH); digitalWrite(led[cuenta_ocio + 4], HIGH); delay(tiempoRetOciosa); cuenta_ocio++; if (cuenta_ocio > 3) { cuenta_ocio = 0; } }
INPUT_PULLUP en vez de INPUT para activar los resistores de pull-up internos.HIGH y al presionar el pulsador se detecta como LOW.loop() para detectar la pulsación al comprobar si el valor leído es LOW.Talk to Flux to get started.
When components get added to the schematic, they'll show up here.