Storing number from matrix keypad in float

Hi guys.
Sorry if this is the incorrect place to post.

I'm attempting to make my own basic Source Measure Unit and will be using a typical black 4x4 matrix keypad (I'll attach a picture) for inputting the desired voltage or current. I have been looking around online but cannot find a reliable source, post that isn't a dead end or I'm not searching the correct thing. I am needing to know how I can store the inputted value from the keypad into a float as I will be using values with decimals. For example, I will have it open a menu window when the required action is executed via another button press and that will ask what I want to set the voltage or current to and I will be able to enter say 12.358 volts and once I hit enter it then stores that exact value in the float and that will later get processed by a separate function and the output will be adjusted accordingly. The max value that will be stored in this float will be either 20.000 or 30.000, I'm not sure how much voltage I want to be able to output yet. For current output I'm thinking of only 1.000.

At the moment I have just got the keypad outputting button presses to serial via this code below.
The float named "SetValue" is where i would like to store this value with decimals.

Any help will be greatly appreciated.

#include <Keypad.h>

#define ROW_NUM     4 // four rows
#define COLUMN_NUM  4 // four columns

// Set limiting Values, Values Inputed from keypad 

float SetValue = 0;



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

byte pin_rows[ROW_NUM]      = {19, 18, 17, 5}; // GPIO19, GPIO18, GPIO17, GPIO5 connect to the row pins
byte pin_column[COLUMN_NUM] = {16, 4, 2, 15};   // GPIO16, GPIO4, GPIO2, GPIO15 connect to the column pins

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

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

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

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

Hi
you need to define your strategy a bit more.

  • is there a backspace key? This may affect how you 'accumulate' and display the number
  • Do you want to debug this as far as you can using Serial Monitor(awkward), or is there a display to be worked with that we're not seeing? (I expect so). I ask this because the strategy of using Serial Monitor may lead you down a path that doesn't translate well to, for example, an LCD or OLED.

OT, feel free to ignore - Minor design comment. If this is a Nano, Uno, or the like, you may want to put on an LCD or OLED display. The most effective way to do that is to use an LCD or OLED that is I2C based, and that is done using pins A4 and A5, aka pins 18 and 19. Suggest you reserve those two, and use other pins for your keypad. Also, you may also want to look at I2C options for your keypad.

Take a look at Keypad data entry. A beginners guide and in particular post #8 of that topic

2 Likes

Hey thanks for the fast response.

  • I forgot about it but a backspace key would be handy to have and make it alot quicker to correct an incorrectly inputted voltage

  • I would like to have serial just for debugging but in the end all required values will be displayed on an LCD yeah. I will be using a 4" LCD that's connected via SPI and i was going to use an ESP32 as they are faster than a nano or uno. I have thought about using a teensy but they seem absurdly expensive now days.

I haven't looked into it but having the keypad running via i2c would be great, less wires the better to a degree.

The numbers in the comments don't match the code!

Yeah sorry i was using some code off a site and the pinout did not working with my ESP32 module as GPIO0 does not exist on my module. I have updated the original post so the comment reflects the actual code.

This is exactly what I'm after thank you. :smiley:

ASCII codes for the digits/symbols 1 2 . 3 5 8 will be stored in a char type array as they are entered from the Keypad. After that, insert a null-character at the end of the array and then call the atof() function to get numerical value for the ASCII coded float number. For example:

char myData[] = {'1', '2', '.', '3', '5', '8', '\0'};
float numberFloat = atof(myData); //numberFloat contains 12.358
1 Like

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