Writing a code for a bowling score calculator

I am currently attempting to code a calculator for a bowling score. I am using a 4x4 keypad and a LCD(20,4) with an I2C which is working. I have it set up to where I can print the score and the setup for the LCD is correct, but I am struggling to write the code for the calculator. I need to store each shot, and I have not been able to do it well. Another issue is that after I get the output correct, I need to keep it even after the next input
Here is my code:

#include <LiquidCrystal_I2C.h>
#include <Key.h>
#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'},
{'X','-','/','D'}
};

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

Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, rows, cols );

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {
Serial.begin(9600);
lcd.backlight();
lcd.init();

lcd.begin(20,4);

lcd.setCursor(1,0); //all setup for lcd frames working correctly, framework doesn't need to be touched              
lcd.print("1");

lcd.setCursor(5,0);
lcd.print("2");

lcd.setCursor(9,0);
lcd.print("3");

lcd.setCursor(13,0);
lcd.print("4");

lcd.setCursor(17,0);
lcd.print("5");

lcd.setCursor(1,2);
lcd.print("6");

lcd.setCursor(5,2);
lcd.print("7");

lcd.setCursor(9,2);
lcd.print("8");

lcd.setCursor(13,2);
lcd.print("9");

lcd.setCursor(17,2);
lcd.print("1");

lcd.setCursor(18,2);
lcd.print("0");

}

void loop() {
char customKey = customKeypad.getKey();
char waitForKey();
char a;

int x = 1;
for (int i = 0; i < 12; i ++) {

if (customKey) {
Serial.println(customKey);
char a = customKey;
lcd.setCursor(1,1);
lcd.print(a);
Serial.println(a);

if ( (a <= 9) && (customKey) ) {
Serial.println(customKey);
char b = customKey;
lcd.setCursor(2,1);
lcd.println(b);
Serial.println(b);
}
}

if (customKey) {
Serial.println(customKey);
lcd.setCursor(5,1);
lcd.println(customKey);

}
}
}

If anyone could help I would greatly appreciate the help, I am semi-new to coding and have not been able to find anything online.

Please, this is not a detailed description of the problem. What is "doing it well", or not? What is the actual problem with that? What does, "keeping" the input all about? Normally, it is extremely mundane and common to "keep" information in a program, you just assign a value to a variable and there it is for you to use later!

Please explain your problem in a more detailed, specific way. Give examples and provide serial debug printouts if you have them.

Also please respect the forum guidelines and edit your post to put the code inside code tags. Instructions were provided when you first posted. Did you fly by them?

+1.

More details.

Describe in English what you are going for.

Pretend, perhaps, you are writing the one page or two manual that begins

“Congratulations on your purchase of the Arduino-Bowl Score Caluator Mark V…”

Draw by hand a picture of how the display would look after a few frames have been played.

Oh, and welcome to the community!

a7

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Sorry about that, this is my first time posting in the forum and thought that I had placed the code inside the tags. Please let me know if this works better.

#include <LiquidCrystal_I2C.h>
#include <Key.h>
#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'},
  {'X','-','/','D'}
};

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

Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, rows, cols );

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {
  Serial.begin(9600);
  lcd.backlight();
  lcd.init(); 

  lcd.begin(20,4);

    lcd.setCursor(1,0); //all setup for lcd frames working correctly, framework doesn't need to be touched              
    lcd.print("1");

    lcd.setCursor(5,0);
    lcd.print("2");

    lcd.setCursor(9,0);
    lcd.print("3");

    lcd.setCursor(13,0);
    lcd.print("4");

    lcd.setCursor(17,0);
    lcd.print("5");

    lcd.setCursor(1,2);
    lcd.print("6");

    lcd.setCursor(5,2);
    lcd.print("7");

    lcd.setCursor(9,2);
    lcd.print("8");

    lcd.setCursor(13,2);
    lcd.print("9");

    lcd.setCursor(17,2);
    lcd.print("1");

    lcd.setCursor(18,2);
    lcd.print("0");
}

void loop() {
  char customKey = customKeypad.getKey();
  char waitForKey();
  char a1;
char b1;
  
  int x = 1;
    for (int i = 0; i < 12; i ++) {
  
   if (customKey) {
    Serial.println(customKey);
    char a1 = customKey;
    lcd.setCursor(1,1);
    lcd.print(a1);
    Serial.println(a1);
    


  if ( (a1 <= 9) && (customKey) ) {
    Serial.println(customKey);
    char b1 = customKey;
    lcd.setCursor(2,1);
    lcd.println(b1);
    Serial.println(b1);
  }
   }

  if (customKey) {
    Serial.println(customKey);
    lcd.setCursor(5,1);
    lcd.println(customKey);
  
  }
  }
  }

The goal for the program is to create a system in which a 4x4 keypad is used to input numbers that will then be stored as variables such as a1, a2, b1, ect. These variables will then be output numbers 0-9, X, and / to a LCD. The void setup for the LCD is permanent, and unaffected by inputs. After the inputs, I plan on writing a system to where if the input is an 'X' or '/', the next variables will be added to the total score which will be displayed on a 7-segment display. If an 'X' is inputed, the next two inputs will be added to that score, for example if the inputs go 'X' '5' '3', then the variable in which had 'X', will have the value of (10+5+3). After all the variables (a1, a2, b1, ect.) have a value, then the 7-segment display will read the sum of said variables.

I am using the input of customKey and then attempting to store it as char a1. I need to keep char a at the point (1,1) and then be unaffected by the next customKey input. If char a is less then 10 (meaning there was not a strike on the first char a value, then the next input will be printed on the lcd at (2,1) and will be stored as char a2. If char a is equal to 10, than the lcd will print the next input to (5,1). As of right now, void setup is working correctly, but my loop is not. I am working on getting the transition from a1 to b1 to work correctly, and after I will include a2. The char a1 is output in (1,1) correctly, but when I put in the next input it outputs to (1,1), instead of (5,1). I was wondering if there is an error in my code that anyone knew how to fix, because as of right now it compiles and uploads my program, without any error codes.

Please let me know if there is anymore information that you need, and if what I gave was specific enough.

Hello
Before start coding you may study the IPO-model and as result you can design the functions neccessary. Each function can be tested separetly and fusioned together after sucessful testing.
Have a nice day and enjoy coding in C++.

1 Like

Are you trying to call waitForKey()? This is not a call but a declaration (or prototype). It is not calling anything.

You iterate through the for loop 12 times but never re-read a key from the keypad into customKey

1 Like

Okay, I was going to focus on a specific need, but really it is too soon for that. I think you're thinking of coding techniques or procedures, far, far in advance of designing the logic that it has to implement. You've leaped right into variable assignments right away, before you can even be sure they are the right ones, or whether you will need them at all.

Please design top down. For example, consider isolating the task of accepting numbers completely, and make it a function so you can use it to accept numbers wherever it is required in the code.

If you don't, you will create a "monolithic monster" that is confusing, difficult to maintain, and error prone.

See reply #6.

1 Like

Well that’s some word salad, I more visually orientated.

But a peek at your code and the sense of your questions suggest to me that you might benefit from taking step back from this project to get a few more very basic concepts mastered.

The IDE has example sketches starting a near zero expectation of prior knowledge.

The internets are clogged with tutorials, videos, projects based on Arduinos &c. Try a few, stick with one that you like and seems to be at your level, or matches you learning style.

No one was born knowing any of this and you can trust me when I say is the rare person that becomes a master programmer over the weekend.

a7

1 Like

Much better thanks

1 Like

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