Mejorar Código Push Button

A continuación se muestra el código modificado para aprovechar los pull-up internos de Arduino (sin resistencias externas). En este ejemplo, se configura cada pulsador con INPUT_PULLUP, de modo que el estado normal será HIGH y al presionar cambiará a LOW.

Cpp


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;
  }
}
Notas
  • Se utiliza INPUT_PULLUP en vez de INPUT para activar los resistores de pull-up internos.
  • Debido a esto, el circuito queda configurado de forma que el estado en reposo es HIGH y al presionar el pulsador se detecta como LOW.
  • Se han actualizado las condiciones en loop() para detectar la pulsación al comprobar si el valor leído es LOW.
Agregar debounce
Sustituir delay()
Explicación INPUT_PULLUP
No schematic yet

Talk to Flux to get started.

No files yet

Documents

    Documents are markdown text files which describe project goals, capture details, or even simulation outputs.

    Assets

    No assets added.

    Random Azure P.K.E. Meter

    Random Azure P.K.E. Meter thumbnail
    Welcome to your new project. Imagine what you can build here.

    Properties

    Properties describe core aspects of the project.

    Pricing & Availability

    Distributor

    Qty 1

    Controls