Adding Backspace in Keypad

Hi, I'm making an ATM in which we have to enter our **BMI(Body Mass Index)**and I am having some difficulties in working with keypad see I have programmed the Arduino in the way that I can Enter my BMI and I will be give money as BMI * 10 but I want to add backspace when I click the # Key. So can anyone help me with this problem
This is My Code

#include <Keypad.h>

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

byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A0, A1, A2, A3}; //connect to the column pinouts of the keypad

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

float enteredBMI = 0.0; // Variable to store entered BMI
float totalMoney = 0.0; // Variable to store total money

void setup() {
  Serial.begin(9600);
}

void loop() {
  char key = keypad.getKey();

  if (key) {
    if (key == 'A') {
      // The user pressed "A," calculate total money and display it
      totalMoney = enteredBMI * 10.0;
      Serial.print("Total Money: $");
      Serial.println(totalMoney);
      enteredBMI = 0.0; // Clear the entered BMI
    } else {
      Serial.print(key); // Print the entered key
      enteredBMI = enteredBMI * 10.0 + (key - '0'); // Append key to enteredBMI
    }
  }
}

The first thing to do is to change the else clause so that the total is only updated if a numeric key is pressed

Come back when you have done that

No, see I want the total to be submitted when I click the "A" key. So Else clause is necessary

The else clause is necessary but is currently wrong

What happens to the total if you press the 'A' key, for instance ?

The number will be multiplied by 10 when we press "A"

But what if you press the 'B' key ?

#include <Keypad.h>

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

const byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
const byte colPins[COLS] = {A0, A1, A2, A3}; //connect to the column pinouts of the keypad

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

int enteredBMI = 0; // Variable to store entered BMI
//int totalMoney = 0; // Variable to store total money

void setup() {
  Serial.begin(9600);
}

void loop() {
  char key = keypad.getKey();

  if (key != NO_KEY) {
    Serial.print(key); // Print the entered key
    if (key >= '0' && key <= '9') enteredBMI = enteredBMI * 10 + key - '0';
    if (key == '#') enteredBMI = enteredBMI / 10 ;
    if (key == 'A') {
      // The user pressed "A," calculate total money and display it
      //totalMoney = enteredBMI * 10;
      Serial.print("Total Money: $");
      Serial.println(enteredBMI);
      enteredBMI = 0; // Clear the entered BMI
    }
  }
}
1 Like

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