Running total and summing

I’m brand new to arduino and I’ve got an assignment to program a running total calculator using a keypad as an input. I will inout a number, and it should sum it to the previous number and so on, keeping running total of my inputs.

Using the example from arduino IDE, I’ve got it to where I can press a number and print it to my screen.

Where my own coding starts is converting my char key input into an integer as follows:

char key = keypad.getkey();

// converts key variable, which can’t be used in calculations because is char, into integer

int keyNum = key - ‘0’;

//make new variable to store running total

int total = total + keyNum;

if (key) {
Serial.println(total)
}

Before my setup I stored initial total to be 0 so the code has it. When I upload and input numbers, it only prints the numbers I inout from the keypad and does not keep a running total like I intended.

Ok,so post code.
In code tags

int total = total + keyNum;

if (key) {
 Serial.println(total)
}

You are creating a new, local, variable named 'total'. If there is a global variable named total it is not being used here.

You could say "total = total + keyNum;" to use the global 'total' (getting rid of the 'int' declaration) or you could use the shortcut for that which is "total += keyNum;"

Hello nathandotts
Welcome to the Arduino fora.
Before you do anything else please take a moment to read General guidance and
How to use this forum

Please post your code, all of it not just a snippet, in code tags as per the instructions.

Please post a schematic so we can see how you have wired things up. What is this keypad you mention? How is it connected?

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4;
 
//three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

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

int total = 0;

void setup(){
  Serial.begin(9600);
  
}
  
void loop(){
  char key = keypad.getKey();

//This converts the char value, which can't be used in math, into an integer by subracting 0 from it
int keyNum = (key - '0');


 int total = total + keyNum;
  
  if (key){
    Serial.println(total);
  }
}

TheMemberFormerlyKnownAsAWOL:
Ok,so post code.
In code tags

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4;

//three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

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

int total = 0;

void setup(){
  Serial.begin(9600);
  
}
  
void loop(){
  char key = keypad.getKey();

//This converts the char value, which can't be used in math, into an integer by subracting 0 from it
int keyNum = (key - '0');


 int total = total + keyNum;
  
  if (key){
    Serial.println(total);
  }
}

This is the keypad I’m using

That’s much better Nathan, although I can’t see your keypad…

I think John has pretty much given you the answer you need.

@nathandotts

Your alternate account has been banned.
Single accounts only from regular users.

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.

Bob.