Im a noob and need some help with buttons

Hi, I have an Arduino nano, 3x4 Button Pad and a 2004 LCD. I want to make a guessing game (with numbers) up to 4 players. But I ran into problems while coding. Actually I just started. Here is the Code I got

#include <SoftPathElectronics.h>

CustomKeyboard keyboard;


#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);

//Variablen
int player_n;

void setup() {
  
  // Setup Keyboard
  Serial.begin(115200);

    String key = "3 12 6 1 1022 315 186 511 241 158 340 195 136 255 164 120 0 0 0 0";
    keyboard.setupKey(key);

  // Setup LCD
  lcd.init();
  lcd.backlight();

  // Oyda
  lcd.setCursor(3, 1);
  lcd.print("Willkommen bei");
  lcd.setCursor(4, 2);
  lcd.print("Trivia Guess");

}

void loop() {

  int key = keyboard.getKeyPressed();

  if (key > 0) {
    lcd.clear();
    lcd.setCursor(2,1);
    lcd.print("Spieleranzahl:");
    lcd.blink();
    delay(100);
  }
}

As you can see, the keyboard uses a custom Library. If you push a button, u can read a key from 1 to 12. All I got for now is a welcome screen and if you push any button, u can read "player count:" and now u should be able to enter a number between 1 & 4 and then push enter to start the game.

Now, how do I put this function in the void loop()? I can't figure out a right trigger, because if you press a key, the int = key stays this key as long until you push another button. This causes unlimited triggers in the loop sometimes :confused:

pls help im dying

What function, try yourself, then if you need help show us ALL the code in code tags.

Hallo brotchen

All you need is a lock of ‘new value’ against ‘old value’

Check this proposal:
----- snip -----

enum OldNew {Old, New}; // make names for the array
static int key[2] // init of array

key[New] = keyboard.getKeyPressed();

if (key[Old] != key[New])
{
  key[Old] = key[New];
  // Here I recommend working with the switch/case statement. 
  if (key[New] > 0)
  {
    lcd.clear();
    lcd.setCursor(2, 1);
    lcd.print("Spieleranzahl:");
    lcd.blink();
    delay(100);
  }
}

----- snip -----

Have a nice day and enjoy coding in C++.

1 Like

Thank you, u saved my day :folded_hands::folded_hands:

1 Like

I’m working on something similar but with a mega game concept, and I ran into some issues too. Maybe you could try debugging your button input logic, or check the wiring for the LCD sometimes little errors can cause big headaches. Hope that helps!

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