Need some help turning a series of integers into one 'float' number

I started this on another thread, but the thread started to skew too far from what the original post was about, so now I'm beginning a new, more relevant one.

I'm using an Arduino Mega Mini, a 4x4 Matrix Keypad, and a 16x2 LCD screen.

What I'd like to do is type in a set of numbers with the keypad, and turn them into a 'float' variable (which will be named 'num1'). The numbers will then be printed onto a 16x2 LCD screen

The first set of numbers will look something like this:

2.5546

The numbers I type will vary, but the amount of decimal places will stay the same.

Right now all I really know how to do is get the numbers to print onto the LCD screen. /:
I've tried a ton of things involving strings and arrays, but I always end up having to rebuild the code from scratch afterwards, when it goes awry.

Here it is in it's most basic form:

#include "Adafruit_LiquidCrystal.h"
#include "Keypad.h"

const byte ROWS = 4;
const byte COLS = 4;

float num1 = 0;
float num2 = 0;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'.', '0', ',', 'D'}
};

byte rowPins[ROWS] = {25, 27, 29, 31};
byte colPins[COLS] = {17, 19, 21, 23};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

unsigned int deBounce = 100;

Adafruit_LiquidCrystal lcd (8, 9, 3, 10, 11, 12, 13);

void setup() {
  customKeypad.setDebounceTime(deBounce);

  lcd.begin(16, 2); // Setup the LCD
  lcd.setCursor(0, 0);
  lcd.print(F("Version 8.6-Frm"));
  delay(5000);
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print(F("Res. Peak Calc."));
  lcd.setCursor(0, 1);
  lcd.print(F("Type I/C values"));
  delay(5000);
  lcd.clear();
}

void loop() {

  char customKey = customKeypad.getKey();
  if (customKey) {
    switch (customKey) {
      case '0' ... '9':
      lcd.print(customKey);
        break;
      case '.':
        lcd.print(customKey);
        break;
      case ',':
        lcd.print(customKey);
        break;
      case 'A':
        lcd.print(customKey);
        break;
      case 'B':
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print(F("Ind. = "));
        lcd.print(num1);
        lcd.setCursor(0, 1);
        lcd.print(num2);
        delay(2500);
        lcd.clear();
        break;
      case 'C':
        lcd.clear();
        num1 = 0;
        break;
      case 'D':
        lcd.print(customKey);
        break;
    }
  }
}

I want to type in my numbers and decimal, and then make all of them be the value for 'num1'.

Other users have mentioned that I have to type a number, turn it into an integer, type another number, turn it into another integer, and then turn those two digits into their own digit, etc.

I have no clue how to do this^^. That's where I'm needing some guidance, or some advice on another method.

Any suggestions on how I might do this, or does anybody know of a pre-existing code or example that does this already?

Thanks for any help! And to those who have helped, if you're reading this - thanks again!
You're awesome

Delta_G:
Or if you have ascii characters in an array:

char array[] = {'3','.','1','4','1','5'};

float pi = atof(array);

Probably ought to terminate that.

char array[] = {'3','.','1','4','1','5','\0'};

Hey Delta_G - Thanks for that!

Didn't skip math, but I must've skipped every Arduino/coding class. I'm still scratching my head, not slapping it (Forgive me!)

I hope you're still with me.

We've turned integers into one 'float' number - great! Step 1 done.
I'm still working on getting this to work with my keypad.

Now I'm a bit stuck trying to get the letters to increment every time a button is pressed, regardless of what button it is.

#include "Adafruit_LiquidCrystal.h"
#include "Keypad.h"

const byte ROWS = 4;
const byte COLS = 4;

int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;

int digit1;
int digit2;
int digit3;
int digit4;
int digit5;

float num1 = a + (0.1*b)+(0.01*c)+(0.001*d)+(0.0001*e);

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'.', '0', ',', 'D'}
};

byte rowPins[ROWS] = {25, 27, 29, 31};
byte colPins[COLS] = {17, 19, 21, 23};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

unsigned int deBounce = 100;

Adafruit_LiquidCrystal lcd (8, 9, 3, 10, 11, 12, 13);

void setup() {
  
customKeypad.setDebounceTime(deBounce);

  lcd.begin(16, 2);
}

void loop() {
  char customKey = customKeypad.getKey();
  if (customKey) {
  digit1 = (customKey-48);
  a = digit1;
  lcd.print(a);
  }
  if (a>=1) {
    if(customKey) {
      digit2 = (customKey-48);
      b = digit2;
      lcd.print(b);
    }
  }
  if (customKey == 'D') {
    lcd.clear();
    lcd.print(num1);
    delay(2000);
    lcd.clear();
  }
}

^^That was my best (failed) attempt. I'll go back into the Keypad library and a few of the Arduino Examples/tutorials and see if anything looks like it might work with this.

  • My second thought is to get an array going and use ++ to increment a value after each letter (a1, a2, a3, etc.)
for (byte n = 0; n < 5; n++) {
   a[n] = customKey;
    }

Here's some example code I had available. It uses my keypad arrangement and my lcd library. A null terminated character string entered from the keypad is converted to a float with atof. # is the key which completes the entry and 'D' is the key which clears the number and display

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // include i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config display for hd44780 chip

#include <Keypad.h>

const byte numRows = 4; //number of rows on the keypad
const byte numCols = 4; //number of columns on the keypad

char keymap[numRows][numCols] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'.', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {12, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols] = {5, 4, 3, 2}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

char enteredDigits[10]  = " ";//null terminated character array to hold entry
boolean entryComplete = false;
float num1;

void setup()
{
  lcd.begin(16, 2);
}

void loop()
{
  getNumber();
  if (entryComplete)
  {
    entryComplete = false;
    lcd.setCursor(0, 1);
    num1 = atof(enteredDigits);
    lcd.print(3 * num1, 4); //math example to show float
  }
}

void getNumber()
{
  static byte index = 0;
  char key = myKeypad.getKey();
  if (key != NO_KEY)
  {
    switch (key)
    {
      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9': case'.':
        lcd.print(key);//print entry on top row
        enteredDigits[index] = key;//build character string
        index++;
        enteredDigits[index] = '\0'; //null terminate
        break;

      case '#':
        entryComplete = true;
        index = 0;//reset for next entry
        break;

      case 'D'://clear entry and display
        memset(enteredDigits, '\0', 10); //clear buffer
        lcd.clear();
        index = 0;
        entryComplete = false;
        break;
    }
  }
}

Cattledog!! That is exactly what I needed!!!

Ahhh, thank you! :smiley: :smiley: :smiley:

Thanks Delta_G as well. You as well as others from the previous thread have been extremely helpful!