Go Down

Topic: Entering 3 digits (Read 3731 times) previous topic - next topic

dcr_inc

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);
  }
}
I'm Lost !! At least on the Arduino...

HazardsMind

#1
Dec 14, 2012, 12:38 am Last Edit: Dec 14, 2012, 12:44 am by HazardsMind Reason: 1
Change your loop to this.

Code: [Select]
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
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

dcr_inc

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

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

HazardsMind

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

I thought that might happen, sorry
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

dcr_inc

I declared it ... Remarked out the string... now it just keeps erroring on anything added
I'm Lost !! At least on the Arduino...

PaulS

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.

HazardsMind

What errors did you get, copy and paste them.

Also where did you get the keypad library, could you post a link?
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

HazardsMind

#7
Dec 14, 2012, 01:26 am Last Edit: Dec 14, 2012, 01:29 am by HazardsMind Reason: 1
I dont understand why you got errors, but this compiled. Try it.

Code: [Select]
/* @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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

PaulS

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.

dcr_inc

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
I'm Lost !! At least on the Arduino...

HazardsMind

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

liudr


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().

dcr_inc

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

Im sorry I'm so lost
I'm Lost !! At least on the Arduino...

liudr

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.

HazardsMind

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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Go Up