[CLOSED] Input a variable using the keypad

Hello,

I am trying to input a variable (d) using the keypad to set the delay of a count in mS.

I had this working using the serial monitor and keyboard, then with the LCD.

Program (sketch) is a nested loop where it counts 1 to 10 with a delay (d), then it starts over and keeps track and numbers the loops.

I do not know the syntax of how to read a variable using the keypad.

Attached is the program with a set 300mS delay (temporary) to make it workable. (see code)
Any help is appreciated!

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; //  columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = { 5, 4, 3, 2 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.

int i=1;      // declare variable i an integer
int redLed = 11;  //declare red led an integer on digital pin 11
int d;   //declare variable d an integer for the delay period 

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

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Addr, En, Rw, Rs, d4, d5, d6, d7, backlighpin, polarity

void setup() {

   lcd.begin(20,4);      //set lcd 16x2 or 20x4
   lcd.backlight();      //turn on backlight
   lcd.setCursor(0, 0);  //set cursor column and row
   lcd.print("Enter delay:");
  
pinMode(redLed, OUTPUT);

  lcd.setCursor(0, 0); 
  lcd.print("   Delay mS ");
  //Read int d from key pad
  
  lcd.setCursor(0,1);
  lcd.print("Loop number 1");
}

void loop() {
  
 char key = keypad.getKey();
 delay(30); //debounce keypad
 
  if (key){
    
    lcd.setCursor(0, 12);
    lcd.print(key); 
  }
  int j=1;                  //  declare variable j an integer
  
  while (j<=10) {          //  nested loop (inside loop)
  lcd.setCursor(0,0);
  lcd.print(j);
  
   delay(300);                //added for testing (remove later)
  //delay(d);                 //delay by variable d (300 works well)
  j++;                     //increment by 1 inside nested loop
 }
{
i =i+1;                   // increment by 1 in main loop

lcd.setCursor(0,1);         //column 1 row 2
lcd.print("Loop number");
lcd.setCursor(12,1);
lcd.print(i);

digitalWrite(redLed, HIGH);
delay(500);

if (j = 10)
{
lcd.setCursor(1,0);    //column 2 row 1
lcd.print(" "); //removes zero on ten when count starts over
}
digitalWrite(redLed,LOW);
 }
}

I have not played with such a keypad, but it looks like you have defined the keystrokes in the keypad initialization as characters. Your keystrokes come in one at a time, so if you are trying to type "300", you will have to take three consecutive readings, '3', '0', '0', and put them together into the string "300". In order to know when you have all the characters you intended to type, you might need to send a terminator character, say 'A'.

So whenever keyboard activity begins, assemble the incoming characters into a string, stopping when you get an 'A'. Then use the string to integer function (whose exact form I forget) to convert to an integer.

I do not know the syntax of how to read a variable using the keypad.

That's because there is no syntax to do that.

You read values from a keypad. You might store the values in an array, and then convert the array to another value. You might then assign the value to a variable. You do not a read a variable from a keypad, though.

int redLed = 11;  //declare red led an integer on digital pin 7

When you have useless comments, you MUST make sure that they are correct, useless comments.

if (j = 10);

12 characters; 2 mistakes; must be some kind of record.

Ok Paul, your "helping" method may work with some, but bet you have driven off more than a few beginners. But any help is better than no help so I'll take it.

  1. Yes, my "useless comment": I didn't update the correct pin # when I added the keypad, my mistake. It's corrected now and the "useless" comment stays.

  2. The record two errors in 12 characters: "if (j = 10);" Ok, removed the semicolon that's one. I'm guessing you think the other error should be an equivalent == and not an equal =?? Works with the "=" but not the "==". Also note program compiled and worked as written even with the ;.

  3. The real help I need: Yes, I know there is no syntax to read in a variable because it is not in the program. Why, because that is where I need the help. Creating an array, how does the keypad input get saved into each register etc.?
    A sample piece of code would do wonders.

I've seen a few programs with the keypad/password and some of the code/password library is not relevant and do not want to get bogged down with non-essential code. I am trying to keep it as simple as possible to understand exactly what is going on so I can at some point be helpful on the Arduino programming site (this site) and put you out of a job. lol.

Also- Thanks for your input jrdoner!

1 Like

You fixed this?

if (j = 10);
{
lcd.setCursor(1,0);    //column 2 row 1
lcd.print(" "); //removes zero on ten when count starts over
}

to

if (j == 10)
{
lcd.setCursor(1,0);    //column 2 row 1
lcd.print(" "); //removes zero on ten when count starts over
}

Hi,

Yes, I removed the ; if (j = 10)

if (j == 10) does not work and holds the zero.

Also note program compiled and worked as written even with the ;.

For some incomplete/incorrect definition of “work”.

if (j == 10) does not work and holds the zero.

The if statement can be said to work if it correctly evaluates that j is, or is not, 10. Any claim that the if statement does not work is going to be met with a great deal of skepticism. You are going to have to provide a great deal of proof that the code does not correctly compare j and 10.

“and holds the zero” makes no sense AT ALL.

Creating an array, how does the keypad input get saved into each register etc.?

You make that happen. You need to define an array. You have not showed us that you have done that.

You need to define an index into that array. You have not showed us that you have done that.

You need to initialize the index at some place in your code. The correct place would be even better. You have not showed us that you have done that.

You need to increment the index at some place in your code. The correct place would be even better. You have not showed us that you have done that.

You need to assign the value read from the keypad to the correct location in the array. The “correct location” obviously (to me, anyway) is at the location stored in index.

You need to add a NULL after each character added to the array. You have not showed us that you have done that.

You need to define when the end of the input for a given value happens. You have not showed us that you have done that.

When the end of input signal happens, you can convert the contents of the array to an int and store the value somewhere.

Perhaps one reason that you are not getting a lot of sympathy here is that getting a number from a stream of characters is such a common task that there are tutorials all over the place. It appears as though you either haven’t bothered looking or haven’t made the connection between the stream of characters coming from the keypad and a stream of characters from another source, such as the serial port.

I’ll give you the benefit of the doubt and assume that it is the latter.

Paul, you get an A+ for attempting to belittle and manipulate/intimidating others into believing your boloney "help technique". You get an F for help. You are the one that failed! Too bad you don't put all that belittling effort into the actual helping of others.
I would like to see you removed from this site. You are giving this site a bad name.

Guess how new programmers learn?? From lots and lots of examples and experimenting to see how changes affect the outcome!! A concept you apparently do not grasp.

Work: In the context I used "work":... It did not work because it did not produce the desired outcome. This is technically a valid statement. So don't bother twisting it into something else.

You state "getting a number from a stream of characters is such a common task that there are tutorials all over the place.", then put a link to one or two so I can see them. That would be helpful. So where are they??

You are wrong about people wanting to help. It's likely they do not want to deal with you. It’s not your site and you don’t run it. I feel sorry for the naive newbies that are going to deal with you. I do not need nor want any responses from you (Paul) from any of my posts period.

Solved: if(j=10) Read below.

I put in a Serial.print j just before the if (j=10) to read j to diagnose why the == does not produce the desired outcome. This is what I found: j is an 11, the j++ added 1 to the 10 after the nested loop was complete. So the program logic is working with (j>=10). It produces the desired output. With the (j=10), it was telling it that j was 10, that’s why it “worked” produced the desired output too. Technically, this is a valid line of code too, but not desirable. I could also use (j==11). All of these will work.

People, do not let Paul intimidate you. He’s a no-buddy and if you give him any respect, you are empowering his egotistical behavior. Stand up for yourself! Be a real person.

Does anyone have sample code of how to Input a variable using the keypad? I would very much appreciate that. Thanks to all with an exclusion.

1 Like

I've always just started with the keypad.h library
http://playground.arduino.cc/Code/Keypad

Thanks CrossRoads, I'll check it out.

Stand up for yourself! Be a real person.

By whining to the moderators?

I'm closing this post since it has no programming value. I have found a few sources that I think will help with: inputting a variable from a keypad. I will post again when I have something helpful.
Jeremy Blum has some excellent tutorials on Arduino too. Newbies, check them out!

BTW: any statement that: if (j=xxxx) is automatically true; therefor you do not even need the "if" statement for the zero to get dropped.

any statement that: if (j=xxxx) is automatically true; t

. . . is a false statement, ironically.
If “xxxx” is zero, then the expression is false.

This is a very important fact to learn.

I’m closing this post

Also false, I’m afraid - you can’t.

Mark it "Closed" as you would mark a post "Solved"

You're right. Any int expect zero. Thanks for the tip.

triode90:
Mark it "Closed" as you would mark a post "Solved"

Yes, you can do that.