Go Down

Topic: About taking multiple keystrokes in 3x6 keypad and decimal float value (Read 93 times) previous topic - next topic

rt009

Basically, i am having a certain project in which i have to take and store values like 11.20, 14.25 as well as 13.00, 100.00 etc so i was working with a basic string base code so i want a little guidance about how i can store these types of values using following code

Code: [Select]
#include <openGLCD.h>
#include <openGLCD_Buildinfo.h>
#include <openGLCD_Config.h>

#include <Keypad.h>
//#include <LiquidCrystal.h> //import lcd library


//LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //lcd pins

//LiquidCrystal lcd(5,4,3,2,1,0);
const byte ROWS = 3; //four rows
const byte COLS = 6; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A', '*', 'D'},
  {'4', '5', '6', 'B', 'G', 'X'},
  {'7', '8', '9', '0', '.', 'Y'},

};

byte rowPins[ROWS] = {A14, A13, A12}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A15, 22, 23, 24, 25, 26}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//create the keypad
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

//variables declaration
boolean valOnePresent = false;
boolean next = false;
boolean final = false;
String num1, num2,num3;
int ans;
char op;

void setup(){
   Serial.begin(9600);
  //lcd.setCursor(2,0);
 // lcd.print("Calculator");
 // delay(2500);
 // lcd.clear(); //clears the LCD screen and positions the cursor in the upper-left corner.
}

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

  if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'||key=='.')){
    if (valOnePresent != true){
      num1 = num1 + key;
      int numLength = num1.length();
      num1.toFloat();
      num2.toFloat();
     // lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
       Serial.println( num1.toFloat());
       
    }
}

PaulS

Code: [Select]
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

Why do you have two instances of the Keypad class?

Quote
i want a little guidance about how i can store these types of values
Maybe I'm missing something, but it seems quite obvious that you need to store float values in float variables.

Code: [Select]
String num1, num2,num3;
There is NO excuse for this.

Code: [Select]
  if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'||key=='.')){

Using nested ifs would be better.
Code: [Select]
if(key != NO_KEY)
{
   if((key >= '1' && key <= '9') || key == '.')
   {


Code: [Select]
      int numLength = num1.length();
      num1.toFloat();
      num2.toFloat();
     // lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
       Serial.println( num1.toFloat());
       
    }

numLength goes out of scope without ever being used, so it is pointless to declare or initialize it.

num2 hasn't been assigned a value, so calling toFloat() on an empty String is pointless.

The art of getting good answers lies in asking good questions.


Go Up