Capture digital inputs and save it in a variable

Hello:

In an Arduino program I want to know if there are any instructions or codes that capture pushbuttons. The buttons I use are A1, A2, A3, A4 and A5 as digital inputs.

In the C# language you have that possibility with the keys of the computer or PC. This is an example in C# on capturing a keystroke and storing it in a variable.

Is there a possibility to do it with Arduino?

Thank you.

You can, of course, read the state of a digital input connected to a pushbutton and save its state to a variable but I presume that is not what you want to do

Please provide more details of what you want to do

Hello good.

Using the C# console, there is a command called

         // Capture key to then validate.
         ConsoleKey key;

In which you save any key pressed in a variable in this example called "key".

I want to know if I can do the same in Arduino, that is, I have 5 buttons called A1 to A5 that are digital inputs, that I detect them and store them in a variable.

Is it possible to do it on Arduino?

Thank you.

Hello Metaconta
Take a view into the IDE to find some buttons related examples.
Have a nice day and enjoy coding in C++.
Or ask Steven.
Дайте миру шанс!

Digital read returns a type bool (0 or 1) so you can read a button and assign the returned value to a type-bool variable that you create (one for each button).

The tricky part might be when to read and save the button status.

The Digital Read Serial Example saves the reading in a variable called buttonState. Of course you can create buttonState1 and buttonState2, or whatever.

The example updates buttonState every time through the loop, so it's not saved very long before it's overwritten, and that may not be what you want.

Now it works for me. The only flaw is that the pushbuttons do not have debounces.

I leave the function in case someone wants to see it.

void Menu_Principal()
{
  // Contador de teclas y navegador.
  int opcion = 0;
  bool salir = false;
  const int seleccionMenu = 8;

  // Limpiar pantalla.
  lcd.clear();

  do
  {
    //******************************************************************
    // Dibujo el menú principal.
    String MENSAJES[] =
    {
      "** MENU PRINCIPAL **", // Posición 0.
      "  ESTADO PRINCIPAL  ", // 1
      "  NOMBRE RELES      ", // 2
      "  NOMBRE SENSORES   ", // 3
      "  ENTRADA ANALÓGICA ", // 4
      "  CONFIGURACION     ", // 5
      "  ACERCA DE...      ", // 6
      "  AYUDA             ", // 7
      "  EXTRA             ", // 8
      "  INICIO            ", // 9
      "                    ", // 10
      ">"                     // 11
    };

    switch (opcion)
    {
      case 0:
        lcd.setCursor(0, 0);     // Línea 1 del LCD.
        lcd.print(MENSAJES[0]);  // ** MENÚ PRINCIPAL **
        lcd.setCursor(0, 1);
        lcd.print(MENSAJES[1]);  // > ESTADO PRINCIPAL
        lcd.setCursor(0, 1);
        lcd.print(MENSAJES[11]); // >
        lcd.setCursor(0, 2);
        lcd.print(MENSAJES[2]);  //   NOMBRE RELÉS
        lcd.setCursor(0, 3);
        lcd.print(MENSAJES[3]);  //   NOMBRE SENSORES
        break;

      case 1:
        lcd.setCursor(0, 0);
        lcd.print(MENSAJES[0]);         // ** MENÚ PRINCIPAL **
        lcd.setCursor(0, 1);
        lcd.print(MENSAJES[1]);         //   ESTADO PRINCIPAL
        lcd.setCursor(0, 2);
        lcd.print(MENSAJES[2]);         // > NOMBRE RELÉS
        lcd.setCursor(0, 2);
        lcd.print(MENSAJES[11]);        // >
        lcd.setCursor(0, 3);
        lcd.print(MENSAJES[3]);         //   NOMBRE SENSORES
        break;

      case 2:
        lcd.setCursor(0, 0);
        lcd.print(MENSAJES[0]);         // ** MENÚ PRINCIPAL **
        lcd.setCursor(0, 1);
        lcd.print(MENSAJES[1]);         //   ESTADO PRINCIPAL
        lcd.setCursor(0, 2);
        lcd.print(MENSAJES[2]);         //   NOMBRE RELÉS
        lcd.setCursor(0, 3);
        lcd.print(MENSAJES[3]);         // > NOMBRE SENSORES
        lcd.setCursor(0, 3);
        lcd.print(MENSAJES[11]);        // >
        break;

      case 3:
        lcd.setCursor(0, 0);
        lcd.print(MENSAJES[0]);         // ** MENÚ PRINCIPAL **
        lcd.setCursor(0, 1);
        lcd.print(MENSAJES[4]);         // > ENTRADA ANALÓGICA
        lcd.setCursor(0, 1);
        lcd.print(MENSAJES[11]);        // >
        lcd.setCursor(0, 2);
        lcd.print(MENSAJES[5]);         //   CONFIGURACIÓN
        lcd.setCursor(0, 3);
        lcd.print(MENSAJES[6]);         //   ACERCA DE...
        break;

      case 4:
        lcd.setCursor(0, 0);
        lcd.print(MENSAJES[0]);         // ** MENÚ PRINCIPAL **
        lcd.setCursor(0, 1);
        lcd.print(MENSAJES[4]);         //   ENTRADA ANALÓGICA
        lcd.setCursor(0, 2);
        lcd.print(MENSAJES[5]);         // > CONFIGURACIÓN
        lcd.setCursor(0, 2);
        lcd.print(MENSAJES[11]);        // >
        lcd.setCursor(0, 3);
        lcd.print(MENSAJES[6]);         //   ACERCA DE...
        break;

      case 5:
        lcd.setCursor(0, 0);
        lcd.print(MENSAJES[0]);         // ** MENÚ PRINCIPAL **
        lcd.setCursor(0, 1);
        lcd.print(MENSAJES[4]);         //   ENTRADA ANALÓGICA
        lcd.setCursor(0, 2);
        lcd.print(MENSAJES[5]);         //   CONFIGURACIÓN
        lcd.setCursor(0, 3);
        lcd.print(MENSAJES[6]);         // > ACERCA DE...
        lcd.setCursor(0, 3);
        lcd.print(MENSAJES[11]);        // >
        break;

      case 6:
        lcd.setCursor(0, 0);
        lcd.print(MENSAJES[0]);         // ** MENÚ PRINCIPAL **
        lcd.setCursor(0, 1);
        lcd.print(MENSAJES[7]);         // > AYUDA
        lcd.setCursor(0, 1);
        lcd.print(MENSAJES[11]);        // >
        lcd.setCursor(0, 2);
        lcd.print(MENSAJES[8]);         //   EXTRA
        lcd.setCursor(0, 3);
        lcd.print(MENSAJES[9]);         //   INICIO
        break;

      case 7:
        lcd.setCursor(0, 0);
        lcd.print(MENSAJES[0]);         // ** MENÚ PRINCIPAL **
        lcd.setCursor(0, 1);
        lcd.print(MENSAJES[7]);         //   AYUDA
        lcd.setCursor(0, 2);
        lcd.print(MENSAJES[8]);         // > EXTRA
        lcd.setCursor(0, 2);
        lcd.print(MENSAJES[11]);        // >
        lcd.setCursor(0, 3);
        lcd.print(MENSAJES[9]);         //   INICIO
        break;

      case 8:
        lcd.setCursor(0, 0);
        lcd.print(MENSAJES[0]);         // ** MENÚ PRINCIPAL **
        lcd.setCursor(0, 1);
        lcd.print(MENSAJES[7]);         //   AYUDA
        lcd.setCursor(0, 2);
        lcd.print(MENSAJES[8]);         //   EXTRA
        lcd.setCursor(0, 3);
        lcd.print(MENSAJES[9]);         // > INICIO
        lcd.setCursor(0, 3);
        lcd.print(MENSAJES[11]);        // >
        break;

      default:
        Serial.print(F("Fuera de rango"));
        break;
    }
    // Fin de pintar el menú principal.
    //******************************************************************

    // Leer pulsador ingresada por el usuario.

    // Validar el tipo de pulsador.
    if (digitalRead(A5) == HIGH)
    {
      switch (opcion)
      {
        case 0:
          //
          break;
        case 1:
          //
          break;
        case 2:
          //OpcionC();
          break;
        case 3:
          //
          break;
        case 4:
          //
          break;
        case 5:
          //
          break;
        case 6:
          //
          break;
        case 7:
          //
          break;
        case 8:
          salir = true;
          break;
        default:
          lcd.print(F("Fuera de rango.     "));
          break;
      }
    }

    // Entonces si pulsas pulsador Abajo.
    //tecla = digitalRead(A2);
    else if (digitalRead(A2) == HIGH)
    {
      opcion++;
    }

    // Entonces si pulsas pulsador Arriba.
    //tecla = digitalRead(A1);
    else if (digitalRead(A1) == HIGH)
    {
      opcion--;
    }

    // Si está en la última opción, salta a la primera.
    if (opcion > seleccionMenu)
    {
      opcion = 0;
    }

    // Si está en la primera posición, salta a la última.
    if (opcion < 0)
    {
      opcion = seleccionMenu;
    }

    // Uso la tecla escape como salida.
  } while (salir == false);
}
  1. Use a library like Bounce2 which can debounce the switches.
  2. Write your own software debounce code. See the debounce example in 02>Digital examples
  3. Use a hardware debounce circuit.

Of course there is no debounce. Making a comparison to C# where so much of the code is encapsulated or abstracted from the hardware is going to run into these problems.

Run through the basic tutorials and you'll be up to speed fairly quickly.

https://europe1.discourse-cdn.com/arduino/original/4X/4/9/9/4996d805b4b19a8fc811c2ade978763e9c31b799.png

I'll do it by hardware so I don't go crazy.

Thanks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.