Hi, I need help changing this keypad calculator code so that it subtracts like a dart scoreboard. I'm testing it out in the serial monitor. I want to be able to display 501 and count down automatically when the score is inputted, and be able to reset. i plan to test on seven segments later but I'm not worried about that right now. Any advice would be helpful ! thank you .
#include <Keypad.h>
const byte numRows = 4; //number of rows on the keypad
const byte numCols = 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols] =
{
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', 'C'},
{'0', ' ', '=', 'D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {5, 4, 3, 2}; //connect to the row pinouts of the kpd
byte colPins[numCols] = {9, 8, 7, 6}; //connect to the column pinouts of the kpd
//initializes an instance of the Keypad class
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
long num1, num2, answer;
boolean mySwitch = false;
boolean do_subtraction_flag = false; // when true we will apply subtraction
int x = 501;
void setup()
{
Serial.begin(9600);
num1 = 0;
num2 = 0;
}
//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is pressed during the whole keypad scan process
void loop()
{
char keypressed = myKeypad.getKey();
if(keypressed != NO_KEY)
{
//Serial.print(keypressed - 48);
Serial.print(keypressed);
if(keypressed > 47 && keypressed < 58) // is between '0' and '9'
{
if(!mySwitch)
{
num1 = (num1 * 10) + (keypressed - 48);
}
else
{
num2 = (num2 * 10) + (keypressed - 48);
}
}
if(keypressed == '=')
{
if(do_subtraction_flag) // we want to subtract the numbers
{
answer = num1 - num2;
}
else // we want to add the numbers
{
answer = num1 + num2;
}
Serial.println(answer);
num1 = 0;
num2 = 0;
mySwitch = false;
do_subtraction_flag = false;
}
else if(keypressed == '+')
{
mySwitch = true;
}
else if(keypressed == '-')
{
mySwitch = true;
do_subtraction_flag = true;
}
}
}
keypad_calculator.ino (2.27 KB)