Counter - keypad several digit number input

Hi! I'm trying to program a counter, with a numbpad 4x4. The A,B,C,D are buttons for +1/-1 and lap +1/-1. As for the numeric inputs i wanted it to be a manual input to add to the count ( being # the "ok" button). I haven´t been able to store more than one digit input tho.. i will leave the code here and hopefully someone can gimme a hand!
Thanks in advance!

#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 4; 

int count = 0;
int count2;
int carga = 1;
int carga2;


char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; 
byte colPins[COLS] = {5, 4, 3, 2}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  Serial.begin(9600);
}
  
void loop(){
  char customKey = customKeypad.getKey();
  String sms = "No Input";  /* RETIRAR */
  String inputnumbpad;
  int numbpad;
  
  if (customKey == 'A') {
    sms.replace("No Input", "+1") ; /* RETIRAR */
    Serial.println(sms);  /* RETIRAR */
    count += 1;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count ) ;
  }
  
  if ((customKey == 'B') && (count>0)) {
    sms.replace("No Input", "-1") ; /* RETIRAR */
    Serial.println(sms);  /* RETIRAR */
    count -= 1;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count ) ;
  }
  
  if (customKey == 'C') {
    sms.replace("No Input", "C+1") ;  /* RETIRAR */
    Serial.println(sms);  /* RETIRAR */
    carga2 = carga;
    count2 = count;
    carga += 1;
    count = 0;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count ) ;
  }
  
  if ((customKey == 'D') && (carga>1)) {
    sms.replace("No Input", "C-1") ;  /* RETIRAR */
    Serial.println(sms);  /* RETIRAR */
    carga -= 1;
    count = count2;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count ) ;
  }
  
  if (customKey == '*') {
    sms.replace("No Input", "RESET") ;  /* RETIRAR */
    Serial.println(sms);  /* RETIRAR */
    carga = 1;
    count = 0;
    carga2 = 0;
    count2= 0;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count) ;
  }
  
  while (customKey <= '0' && customKey >= '9' && customKey != '#') {
    inputnumbpad += customKey;
    Serial.print(inputnumbpad);
  }
   
   if (customKey == '#') {
    sms.replace("No Input", "OK") ; /* RETIRAR */
    Serial.println(sms);  /* RETIRAR */
    numbpad = inputnumbpad.toInt();
    Serial.println(numbpad);
    count = count + numbpad;
    Serial.println(count);
   }
}

See Keypad data entry

1 Like

if you're trying to capture the value of the digit keys (0-9), shouldn't this be

  if ('0' <= customKey && customKey <= '9') {
1 Like

Thanks a lot! It helped a lot! Its already running like a wanted! I will post my version here maybe it can help someone idk x)

#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 4; 
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; 
byte colPins[COLS] = {5, 4, 3, 2}; 

Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

char key;
byte keyAsANumber;
boolean entryComplete;
unsigned int numbpad = 0;
int count = 0;
int count2;
int carga = 1;
int carga2;
String sms = "No Input";  /* RETIRAR */



void setup(){
  Serial.begin(9600);
}
  
void loop(){
  entryComplete = false;
  readKeypad();
  if (entryComplete){
    numbpad=0;
  }
  
  
}

void readKeypad(){
  key = keypad.getKey();
  if (key == '#'){
    if (numbpad == 0){
      Serial.println("No input");
    }
    if (numbpad != 0){
      Serial.println(" - Entry complete!");
      count = count + numbpad;
      Serial.println((String) "Carga: " + carga + " Mortos: " + count );
      entryComplete = true; 
      return;
    }
  }*
  if (key >= '0' && key <= '9'){
    keyAsANumber = key - 48;
    numbpad = numbpad *10;
    numbpad = numbpad + keyAsANumber;
    Serial.print(keyAsANumber);
  }
  if (key == 'A') {
    count += 1;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count ) ;
  }
  
  if ((key == 'B') && (count>0)) {
    count -= 1;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count ) ;
  }
  
  if (key == 'C') {
    carga2 = carga;
    count2 = count;
    carga += 1;
    count = 0;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count ) ;
  }
  
  if ((key == 'D') && (carga>1)) {
    carga -= 1;
    count = count2;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count ) ;
  }
  
  if (key == '*') {
    carga = 1;
    count = 0;
    carga2 = 0;
    count2= 0;
    Serial.println((String) "Carga: " + carga + " Mortos: " + count) ;
  }
}

Fat fingers :stuck_out_tongue: but yes it should xD

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