1

I am using 20 x 4 LCD And 3 x4 keypad I want to enter and store 3 inputs in different variables the code I am using can store 3 inputs and display it right in serial monitor example I type 5 digits using keypad it will store and display correctly in LCD and serial monitor the right value however if its more than five it has negative output and very far to what inputted. Thank you for help .The code is... enter image description here enter image description here

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
SoftwareSerial mySerial(6,5);
#include <Keypad.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
 {'1','2','3'},
 {'4','5','6'},
 {'7','8','9'},
 {'*','0','#'}
};
byte rowPins[ROWS] = {19, 18, 17, 16}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {15, 14, 13}; //connect to the column pinouts of the keypad
int count=0;
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char entryStr[12]; // This can hold up to 4 digits
int i=0;
void setup()
{
 mySerial.begin(9600); 
 Serial.begin(9600);
 lcd.begin(20, 4);
}
void loop(){
 long n1, n2 , n3;
 lcd.setCursor(0,0);
 lcd.print("Enter n1: "); 
 lcd.setCursor(0,1);
 n1 = GetNumber();
 Serial.println ("n1: ");
 Serial.println (n1);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Enter n2: "); 
 lcd.setCursor(0,1);
 n2= GetNumber();
 Serial.println ("n2: ");
 Serial.println (n2); 
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Enter n3: "); 
 lcd.setCursor(0,1);
 n3 = GetNumber();
 Serial.println ("n3: ");
 Serial.println (n3); 
}
int GetNumber()
{
 long num = 0;
 char key = kpd.getKey();
 while(key != '#')
 {
 switch (key)
 {
 case NO_KEY:
 break;
 case '0': case '1': case '2': case '3': case '4':
 case '5': case '6': case '7': case '8': case '9':
 lcd.print(key);
 num = num * 10 + (key -'0');
 break;
 case '*':
 num = 0;
 lcd.clear();
 break;
 }
 key = kpd.getKey();
 }
 return num;
}
jfpoilpret
9,1627 gold badges38 silver badges54 bronze badges
asked Oct 10, 2016 at 15:15
1
  • 1
    Phone numbers should be handled as strings. Otherwise 1, 01 and 000000001 will be the same. Commented Oct 10, 2016 at 15:40

1 Answer 1

1
int GetNumber()

should be changed to

long GetNumber()

It is because the function type is the type of value that returning. In this case you are returning a long so you have to change it. An int can handle a number from -32 768 to 32 767. A long on the other side can handle a number from -2,147,483,648 to 2,147,483,647.

answered Oct 10, 2016 at 15:25
4
  • thank you I followed what you advised it displayed the right value however if i tried to typed 091234 i can only see 91234 thank you very much I a newbie I appreciate your help a lot thanks Commented Oct 10, 2016 at 15:31
  • can be risky but use a char instead. the arduino sees your number as a litteral mathematical number. it will simplify your number by removing the 0s like when you do a calculation in math. please dont tell me you do 0003 x 0009 = 0027. using a char is like sending a complete word like HELLO then you can decompose it into multiple small bits such as H, E, L, L, O,. It also works with numbers like if your password was 0192, you can decompose it to 0_1_9_2 Commented Oct 10, 2016 at 15:40
  • Thank you I'll try what you advices thank you again 🙂I appreciated your help and sharing your time thank you . Commented Oct 10, 2016 at 15:46
  • its char* btw. char is different then char* dont forget the * Commented Oct 10, 2016 at 15:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.