Here is my little project on using a 3*4 key pad. And I have to store what were entered by the user into an array and display them later.
I ues boarduino.
User is required to enter a maximum of 8 digits float including the radix point. Since there can be only one radix point in a float, so if the point is being pressed twice, it won’t be shown. and the # is used to confirm after entering the number.
What I got from the program below is keep showing array [], the [] is actually a square(like when the arduino is not showing normal character). I am stuck with this, hope anyone could help. Thank you very much!!!
```

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, 2}; //connect to the column pinouts of the keypad

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

char charGain;  //this is the array use to store the total Gain, the maximum value is 1280, and I set 8 digits to store the gain, maximum 3 decimals
int charindex=0;   //this is for the array index
int pointindex=0;  //since there can be only one radix point ‘.’ in a float, pointindex is set to record how many ‘.’ are entered

//void enterGain(){  //block this sentence
void loop(){

keyArray();
printArray();

}

void keyArray(){  // function of storing what the user put in an array

//  for(charindex = 0;charindex < 8; charindex++){

if (key != NO_KEY){  //if there is any key being put
if (key = ‘.’){    //if the radix point is pressed for once, display the ‘.’
pointindex++;
if (pointindex=1){
charGain[charindex]=key;
charindex++;
Serial.print(charGain[charindex],BYTE);
}
else{            //if the radix point is pressed for more than once, do not display the ‘.’, but display hwo many times are pressed
pointindex++;
Serial.print(“pointindex”);
Serial.print(pointindex);
}
}
else if (key = ‘#’){  // if # being pressed, this is for the user to confirm whether the number is what he want
//      Serial.println(‘Are you sure about your enter? Press # for sure, press 0 for re-enter’);
//      confirm_or_enter();
for(charindex;charindex < 7;charindex++){  //if the user didn’t enter 8 digits float from the keypad, then other char in the array is set to be 0
charGain[charindex]=0;
pointindex=0;
charindex=0;
}
}
else{                        //if the user enter 0-9, then store it into array and display it
charGain[charindex]=key;
charindex++;
Serial.print(charGain[charindex],BYTE);
}
//  }
charGain = 0;              //the last char in the array is set to be 0
//    charGain[charindex]=0;
}
}

void printArray(){                //would like to show every char in the array after entering from keypad

for (charindex = 0;charindex < 9;charindex++){
//Serial.println(“array [” ++ charindex ++ “]”++ charGain[charindex]);
Serial.print(“array [”);
delay (1000);
Serial.print(charindex);
delay (1000);
Serial.print("] ");
delay (1000);
Serial.println(charGain[charindex],BYTE);
delay (1000);

}

}*
```

``````if (pointindex=1){
``````

= is the assignment operator. == is the equality operator, which is what you want to use here (and other places).

Thank you very much! But I have more problem now :-[

Did you fix all the places where you were using = instead of ==?

What problems, now?

Thank you very much. Yes I have already change those = to ==, and add more code this time. And I think the problem is mainly in the Main Loop and sub loops in the program.
What I want to achieve is , I have to put in 2 different number, the first one is Total Gain,the array named charGain is used to store; the second one is offSet voltage, array named charoffSet is used to store. They are in order.
Function keyArray() is used to get every char entered from keypad into the array.
When there is only one function of keyArray() to get the number of total Gain, it works perfect; however, when I would like to realize recieving two number, problems turned out. I have keyArray1() to store the Total Gain, and keyArray2() to store the The program is looping to fast so that the two function keyArray1() and keyArray2() is running one after another non-stop.
Here is my code and I will add the Serial result in the later reply.
Thank you very much.

``````#include <Keypad.h>

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, 2}; //connect to the column pinouts of the keypad

char charGain;  //this is the array use to store the total Gain, the maximum value is 1280, and I set 8 digits to store the gain, maximum 3 decimal
char charOffSet;
int charindex1=0;   //this is for the array index
int pointindex1=0;  //since there can be only one radix point '.' in a float, pointindex is set to record how many '.' are entered
int charindex2=0;   //this is for the array index
int pointindex2=0;  //since there can be only one radix point '.' in a float, pointindex is set to record how many '.' are entered
float thenumber1=0;
float thenumber2=0;

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

for(charindex1 = 0; charindex1 < 9; charindex1++){
charGain[charindex1] = 0;
}

charindex1 = 0;

for(charindex2 = 0; charindex2 < 5; charindex2++){
charOffSet[charindex2] = 0;
}

charindex2 = 0;

}

void loop(){
delay(500);
keyArray1();
delay(500);
keyArray2();

}

void keyArray1(){  // function of storing what the user put in an array

if (key != NO_KEY){  //if there is any key being put
if (key == '.'){    //if the radix point is pressed for once, display the '.'
pointindex1++;
if (pointindex1==1){
charGain[charindex1]=key;

Serial.print(charGain[charindex1],BYTE);
charindex1++;

}
else{            //if the radix point is pressed for more than once, do not display the '.', but display hwo many times are pressed
}
}
else if (key == '#')
{  // if # being pressed, this is for the user to confirm whether the number is what he want
//        hash_flag = true;
Serial.println("");
printnum1();
Serial.print("? Press # for sure, press 0 for reset");

char key2 = NO_KEY;
while(key2 != '#' && key2 != '0')
{
if (key2 == '#')
{
Serial.println("");
Serial.println("Confirm");
printnum1();
//          keyArray2();
}
else if (key2 == '0')
{
Serial.println("");
Serial.println("clear");
for(charindex1 = 0; charindex1 < 9; charindex1++)
{
charGain[charindex1] = 0;
}
charindex1 = 0;
keyArray1();

}
/*        else
{
Serial.println("");
Serial.println("unkown");
//break;
}*/
}

for(charindex1 = 0; charindex1 < 9; charindex1++)
charGain[charindex1] = 0;
charindex1 = 0;
pointindex1 = 0;

}

else{                        //if the user enter 0-9, then store it into array and display it

charGain[charindex1] = key;
Serial.print(charGain[charindex1],BYTE);
charindex1++;
}
}
Serial.println("bug");
}

void printnum1(){

thenumber1 = atof(charGain);
Serial.println("");
Serial.println(thenumber1, 6);

}

void keyArray2(){  // function of storing what the user put in an array

delay (500);

if (key != NO_KEY){  //if there is any key being put
if (key == '.'){    //if the radix point is pressed for once, display the '.'
pointindex2++;
if (pointindex2==1){
charOffSet[charindex2]=key;

Serial.print(charOffSet[charindex2],BYTE);
charindex2++;

}
else
{            //if the radix point is pressed for more than once, do not display the '.', but display hwo many times are pressed
}
}
else if (key == '#')
{  // if # being pressed, this is for the user to confirm whether the number is what he want
//        hash_flag = true;
Serial.println("");
printnum2();
Serial.print("? Press # for sure, press 0 for reset");

char key2 = NO_KEY;
while(key2 != '#' && key2 != '0')
{
if (key2 == '#')
{
Serial.println("");
Serial.println("Confirm");
printnum2();
}
else if (key2 == '0')
{
Serial.println("");
Serial.println("clear");
for(charindex2 = 0; charindex2 < 5; charindex2++)
{
charOffSet[charindex2] = 0;
}
charindex2 = 0;
keyArray2();

}
}

for(charindex2 = 0; charindex2 < 5; charindex2++)
charOffSet[charindex2] = 0;
charindex2 = 0;
pointindex2 = 0;

}

else{                        //if the user enter 0-9, then store it into array and display it

charOffSet[charindex2] = key;
Serial.print(charOffSet[charindex2],BYTE);
charindex2++;

}
}
Serial.println("bugbug");
}

void printnum2(){
thenumber2 = atof(charOffSet);
Serial.println("");
Serial.println(thenumber2, 6);
}
``````

Thank you very much, and here continues the first part
And here I test for twice
If in
Main Loop()
{
KeyArray1();
KeyArray2();
}

Then the result is the program keep looping both, but not getting all the char for Total Gain by keyArray1() and then go to the the KeyArray2() to get all the char for Offset Voltage. And here is the result.

``````Result of the second Program
bugbug
bug
bugbug
bug
bugbug
bug
bugbug
bug
4bugbug
bug
bugbug
bug
bugbug
6bug
.bugbug
5bug

4.000000
? Press # for sure, press 0 for reset
clear
bugbug
bugbug
bug
5bugbug
bug
bugbug
bug
bugbug
4bug

5.000000
? Press # for sure, press 0 for reset
Confirm

5.000000
bugbug
bug
bugbug
5bug
bugbug
1bug
.bugbug
bug
bugbug
bug

0.000000
? Press # for sure, press 0 for reset
``````

Then I change a little bit, I put only keyArray1() into the Main Loop and call keyArray2() in the function of keyArray1() when the user has already confirm what they have enter for the Total Gain. However, the program only run keyArray2() for once to get the char for Offset Voltage and then keep looping the keyArray1() to get the Total Gain again!!

Here is the code:

``````#include <Keypad.h>

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, 2}; //connect to the column pinouts of the keypad

char charGain;  //this is the array use to store the total Gain, the maximum value is 1280, and I set 8 digits to store the gain, maximum 3 decimal
char charOffSet;
int charindex1=0;   //this is for the array index
int pointindex1=0;  //since there can be only one radix point '.' in a float, pointindex is set to record how many '.' are entered
int charindex2=0;   //this is for the array index
int pointindex2=0;  //since there can be only one radix point '.' in a float, pointindex is set to record how many '.' are entered
float thenumber1=0;
float thenumber2=0;

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

for(charindex1 = 0; charindex1 < 9; charindex1++){
charGain[charindex1] = 0;
}

charindex1 = 0;

for(charindex2 = 0; charindex2 < 5; charindex2++){
charOffSet[charindex2] = 0;
}

charindex2 = 0;

}

void loop(){
delay(500);
keyArray1();
delay(500);

}

void keyArray1(){  // function of storing what the user put in an array

if (key != NO_KEY){  //if there is any key being put
if (key == '.'){    //if the radix point is pressed for once, display the '.'
pointindex1++;
if (pointindex1==1){
charGain[charindex1]=key;

Serial.print(charGain[charindex1],BYTE);
charindex1++;

}
else{            //if the radix point is pressed for more than once, do not display the '.', but display hwo many times are pressed
}
}
else if (key == '#')
{  // if # being pressed, this is for the user to confirm whether the number is what he want
//        hash_flag = true;
Serial.println("");
printnum1();
Serial.print("? Press # for sure, press 0 for reset");

char key2 = NO_KEY;
while(key2 != '#' && key2 != '0')
{
if (key2 == '#')
{
Serial.println("");
Serial.println("Confirm");
printnum1();
keyArray2();
}
else if (key2 == '0')
{
Serial.println("");
Serial.println("clear");
for(charindex1 = 0; charindex1 < 9; charindex1++)
{
charGain[charindex1] = 0;
}
charindex1 = 0;
keyArray1();

}
/*        else
{
Serial.println("");
Serial.println("unkown");
//break;
}*/
}

for(charindex1 = 0; charindex1 < 9; charindex1++)
charGain[charindex1] = 0;
charindex1 = 0;
pointindex1 = 0;

}

else{                        //if the user enter 0-9, then store it into array and display it

charGain[charindex1] = key;
Serial.print(charGain[charindex1],BYTE);
charindex1++;
}
}
Serial.println("bug");
}

void printnum1(){

thenumber1 = atof(charGain);
Serial.println("");
Serial.println(thenumber1, 6);

}

void keyArray2(){  // function of storing what the user put in an array

delay (500);

if (key != NO_KEY){  //if there is any key being put
if (key == '.'){    //if the radix point is pressed for once, display the '.'
pointindex2++;
if (pointindex2==1){
charOffSet[charindex2]=key;

Serial.print(charOffSet[charindex2],BYTE);
charindex2++;

}
else
{            //if the radix point is pressed for more than once, do not display the '.', but display hwo many times are pressed
}
}
else if (key == '#')
{  // if # being pressed, this is for the user to confirm whether the number is what he want
//        hash_flag = true;
Serial.println("");
printnum2();
Serial.print("? Press # for sure, press 0 for reset");

char key2 = NO_KEY;
while(key2 != '#' && key2 != '0')
{
if (key2 == '#')
{
Serial.println("");
Serial.println("Confirm");
printnum2();
}
else if (key2 == '0')
{
Serial.println("");
Serial.println("clear");
for(charindex2 = 0; charindex2 < 5; charindex2++)
{
charOffSet[charindex2] = 0;
}
charindex2 = 0;
keyArray2();

}
}

for(charindex2 = 0; charindex2 < 5; charindex2++)
charOffSet[charindex2] = 0;
charindex2 = 0;
pointindex2 = 0;

}

else{                        //if the user enter 0-9, then store it into array and display it

charOffSet[charindex2] = key;
Serial.print(charOffSet[charindex2],BYTE);
charindex2++;

}
}
Serial.println("bugbug");
}

void printnum2(){
thenumber2 = atof(charOffSet);
Serial.println("");
Serial.println(thenumber2, 6);
}
``````

And here is the result:

``````Please enter the Total Gain
bug
bug
bug
bug
bug
bug

0.000000
? Press # for sure, press 0 for reset
clear
bug
bug
bug
bug
bug
4bug
5bug
8bug
.bug
4bug
bug
bug

458.399993
? Press # for sure, press 0 for reset
Confirm

458.399993
bugbug
bug
bug
bug
bug

0.000000
? Press # for sure, press 0 for reset
``````

When the Arduino starts up (when powered up or reset), it executes the setup function, then calls the loop function in an endless loop.

``````void loop()
{
delay(500);
keyArray1();
delay(500);
keyArray2();
}
``````

So, keyArray1 is called.

``````void keyArray1()
{  // function of storing what the user put in an array

if (key != NO_KEY)
{
// Do something with that key press
}
Serial.println("bug");
}
``````

Unless you are fast, you won't be able to get a key pressed before this code has printed "Please enter the Total Gain" and "bug" and gone on to keyArray2.

During the loop function, is the user supposed to be able to adjust the total gain? If not, keyArray1 should be called in setup. If so, loop should check for some trigger event before calling keyArray1, such as a button press.

The keyArray functions need to be re-written so that they loop until some terminating key is pressed. The # key, maybe.

I'm trying to do something similar to this. Do you mind if I use parts of this code? What does gain refer to?

Where is the number stored?

Basically I want to store a 4 digit number or smaller.

*

• Demonstrate the simplest use of the keypad library.
• Arduino using the pin numbers listed below in
• rowPins and colPins. If you want to use different
• pins then you can change the numbers below to
• Note: Make sure to use pullup resistors on each of
• the rowPins.
• Note: Pins are use multiple times in this example.
*/
#include <LiquidCrystal.h>

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

// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d4, d5, d6, d7 on pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

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

char charGain;
int charindex =0;

//void enterGain(){ //block this sentence
void loop()
{
keyArray();
printArray();
}

void keyArray(){

for(charindex == 0;charindex < 4; charindex++){

char key = kpd.getKey();
if (key != NO_KEY){ //if there is any key being put
if (key = ‘#’){
for(charindex;charindex < 4;charindex++){ //if the user didn’t enter 8 digits float from the keypad, then other char in the array is set to be 0
charGain[charindex]==0;
charindex==0;
}
}
else if (key = ‘*’){

else{ //if the user enter 0-9, then store it into array and display it
charGain[charindex]=key;
charindex++;
lcd.print(charGain[charindex]);
}
}
}

void printArray(){ //would like to show every char in the array after entering from keypad

for (charindex == 0;charindex < 4;charindex++){
//Serial.println(“array [” ++ charindex ++ “]”++ charGain[charindex]);
lcd.print(“array [”);
delay (1000);
lcd.print(charindex);
delay (1000);
lcd.print("] ");
delay (1000);
lcd.print(charGain[charindex]);
delay (1000);

}

}

I used big sections of the code. I used the print section just because I’d get an error if I didn’t refer to printArray().

Also I wanted the * key to cancel out all the digits of the code. If whoever helps me with this could explain it to me, so that I understand it and could do it on my own that would help a lot.

The keyArray functions need to be re-written so that they loop until some terminating key is pressed.

Perhaps something like this