Pages: [1] 2 3   Go Down
Author Topic: Entering 3 digits  (Read 3228 times)
0 Members and 1 Guest are viewing this topic.
Delta, PA USA
Offline Offline
Newbie
*
Karma: 0
Posts: 30
I'm Lost !!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nub here again..
I'm actually making progress with my project.. and thanks to all who have helped..As it stands now, I can enter digits but the display only displays 1 digit at a time..

What do I need to add to allow me to enter 3 digits from the keypad and have the three digits display on my LCD when I hit the * key..

The code I'm using is :


/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {28, 26, 24, 22}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {31, 33, 35}; //connect to the column pinouts of the keypad

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

void setup(){
  Serial.begin(9600);
 
}
 
void loop(){
  char key = keypad.getKey();
 
  if (key){
    lcd.begin(16,0);
    lcd.setCursor(6,0);
    lcd.print(key);
    lcd.print("  ");
    Serial.println(key);
  }
}
Logged

I'm Lost !! At least on the Arduino...

Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3595
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Change your loop to this.

Code:
void loop(){

  char key = keypad.getKey();
 
  if (key != '*'){
String keyIn += key;
}

else {
    lcd.begin(16,0);
    lcd.setCursor(6,0);
// int keyDisp = keyIn.toInt();
    lcd.print(keyIn); // if you don't get the correct data, then uncomment previous line and change keyIn to keyDisp
    lcd.print("  ");
    Serial.println(keyIn);
    keyIn = "";  //clear for new data
  }
}

I made an edit
« Last Edit: December 13, 2012, 06:44:18 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Delta, PA USA
Offline Offline
Newbie
*
Karma: 0
Posts: 30
I'm Lost !!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

added the new loop.. Now I get a compile error at the: String keyIn += Key line

HELP !!!!
Logged

I'm Lost !! At least on the Arduino...

Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3595
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

declair "String keyIn" at the top of your code with everything else and delete "String" inside the loop.

I thought that might happen, sorry
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Delta, PA USA
Offline Offline
Newbie
*
Karma: 0
Posts: 30
I'm Lost !!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I declared it ... Remarked out the string... now it just keeps erroring on anything added
Logged

I'm Lost !! At least on the Arduino...

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 612
Posts: 49199
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Of course, using the String class to collect a fixed number of characters is not necessary. Doing so will make it difficult to implement a backspace key.

A 4 element char array and an index would be much better - 3 characters and a NULL terminator. Increment the index each time you add a character and NULL. Decrement the index each time the backspace key is pressed.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3595
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What errors did you get, copy and paste them.

Also where did you get the keypad library, could you post a link?
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3595
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont understand why you got errors, but this compiled. Try it.

Code:
/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
String keyIn;

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {28, 26, 24, 22}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {31, 33, 35}; //connect to the column pinouts of the keypad

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

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

  char key = keypad.getKey();
 
  if (key != '*'){
    keyIn += key;
}

else {
    lcd.begin(16,0);
    lcd.setCursor(6,0);
// int keyDisp = keyIn.toInt();
    lcd.print(keyIn); // if you don't get the correct data, then uncomment previous line and change keyIn to keyDisp
    lcd.print("  ");
    Serial.println(keyIn);
    keyIn = "";  //clear for new data
  }
}

@PaulS
What backspace button, the only buttons dcr_inc has are what is on that keypad. Unless dcr_inc uses "#" as the backspace button.
« Last Edit: December 13, 2012, 07:29:57 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 612
Posts: 49199
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What backspace button, the only buttons dcr_inc has are what is on that keypad. Unless dcr_inc uses "#" as the backspace button.
There may not be a backspace key yet. But, eventually, OP may decide that one is needed. It's best to plan for what may be needed, not just what IS needed.

A String to handle 3 character entry is just plain lazy.
Logged

Delta, PA USA
Offline Offline
Newbie
*
Karma: 0
Posts: 30
I'm Lost !!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok.. I used the code that was last supplied,.. I un remarked the int keyDisp = keyIn.toInt(); line.. Now I get a 0 on the lcd when I input the * key.. The TX LED on the Mega stays on for approx 30 seconds.. No other key does any thing.. No digits show on the LCD or Term.. The code is exactly what was provided by HazardsMind
Logged

I'm Lost !! At least on the Arduino...

Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3595
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

True, but it is less coding and less confusing. I always go with simple codes first, then make them more complex. That way if the more complex code doesn't work then I have something to fall back on.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A String to handle 3 character entry is just plain lazy.

Do this instead of String

char keyIn[6]; //If you want 5 digits.

String is lazy and will crash your project due to leaky free().
Logged


Delta, PA USA
Offline Offline
Newbie
*
Karma: 0
Posts: 30
I'm Lost !!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Folks,
I am to say the least LOST.. What should I delete to add the:  char keyLin[6];

Im sorry I'm so lost
Logged

I'm Lost !! At least on the Arduino...

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

OP,

Sorry for the confusion. I will make an attempt to provide a more complete code with the char keyIn version.

HazzardMind has forgotten that getKey can also return NoKey. You need to exclude no key situation so you won't attempt to fill String with NoKey or NOKEY.
Logged


Queens, New York
Offline Offline
Faraday Member
**
Karma: 98
Posts: 3595
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I appologize for the code not working, but keep in mind that I do not have a keypad to test myself, so i'm relying on what dcr_inc is getting.
I could probably simulate it with the serial monitor but thats about it. I don't have any tact buttons or the time to buy or make one right now, so all of this is theoretical.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Pages: [1] 2 3   Go Up
Jump to: