How to collect the typed keys from 4x4 keypad to send sms

Hi, i need some help taking the typed number from my 4x4 keypad and using them to send message, i need help, i know the my codes are quite dumb cause i'm still learning, i'm trying to make a phone using GSM/GPRS shield, 4x4 keypad, and lcd, here's my code

#include <LiquidCrystal.h>
#include <Wire.h>
#include <Keypad.h>
const byte rows = 4;
const byte cols = 4;
const int  buttonPin = A0;


char keys[rows][cols] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[rows] = {13, 12, 11, 10};
byte colPins[cols] = {9, 8, 7, 6};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
LiquidCrystal lcd(5, 4, 3, 2, 1, 0);

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

void initLCDKeys()
{
  for (int i = 0; i < sizeof(rowPins); i++)
    pinMode(rowPins[i], OUTPUT);
  for (int i = 0; i < sizeof(colPins); i++)
  {
    pinMode(colPins[i], INPUT);
    digitalWrite(colPins[i], LOW);
  }
}


void setup() {
  char key = keypad.getKey();
  lcd.clear();
  delay(50);
  lcd.begin(16, 2);
  lcd.print("enter no.:");
  lcd.setCursor(0, 1);
  lcd.print("no. of pushes:");
  pinMode(buttonPin, INPUT);
}


void loop() {
  buttonState = digitalRead(buttonPin);
  char key = keypad.getKey();
  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      lcd.begin(16, 2);
      lcd.print("enter no.:");
      lcd.print(key);
      lcd.setCursor(0, 1);
      lcd.print("no. of pushes:");
      lcd.println(buttonPushCounter);
    }
    delay(50);
  }
  lastButtonState = buttonState;
  buttonState = digitalRead(buttonPin);
  if (int(key)  != 0)
  {
    lcd.begin(16, 2);
    lcd.print("enter no.:");
    lcd.print(key);
    lcd.setCursor(0, 1);
    lcd.print("no. of pushes:");
    lcd.println(buttonPushCounter);
  }

  if (buttonPushCounter %  0 != 0) {
    lcd.setCursor(0, 1);
    lcd.print("no. of pushes:");
    lcd.println(buttonPushCounter);
  }


}

EB2FT.ino (1.7 KB)

Welcome to the Forum. With such a small sketch, you would be doing everyone a big favour by posting it in readable format instead of an attachment. Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags ("</>") when posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button.

thank you for the heads up, sorry for the inconvenience, still learning =).

Thank you. I'll step back, as I have no experience with the Keypad library.

How about, GSM/GPRS shield? I need help on learning how to input a phone number and using it, even if it is in serial monitor. =)

i need some help taking the typed number from my 4x4 keypad and

And store them where? The first thing you need is an array to store the data in, and an index into the array.

The next thing you need to do is stop starting new threads.

okay, sorry about that, i'm trying to temporarily store them, so that i could use them for sending message using gsm/gprs module, at the moment of input, and display the phone number on the LCD while using typing it in, should i use the variable declaration "int key[]"?

should i use the variable declaration "int key[]"?

To collect characters? Probably not. I'm also sure that you need a value in the brackets.

how bout this sir? "char key[10] = keypad.getKey();" "10" so that i could use 11 char cters right?

how bout this sir? "char key[10] = keypad.getKey();" "10" so that i could use 11 char cters right?

No. You can't read all 10 keys at one time.

char phoneNumber[20]; // Or however big it needs to be
byte phoneIndex = 0;

void loop()
{
   char key = keypad.getKey();
   if(key != NO_KEY)
   {
      phoneNumber[phoneIndex] = key;
      phoneNumber[phoneIndex++] = '\0';
   }

   // More stuff...
}

You'll need to consider an oops key (as in "damn, that was the wrong key") and a select key (as in "Use that number!") and a reset key (as in "Lets start over").

ayt, great help tnx!

Hi paul, Just tried what you said, I don't know how to use them properly, here's the result

#include <LiquidCrystal.h>
#include <Keypad.h> 
LiquidCrystal lcd(5, 4, 3, 2, 1, 0);
const byte ROWS = 4; 
const byte COLS = 4;
char keys [ROWS] [COLS] = {
  {'1', '2', '3', 'A'},
  {'8', '5', '2', 'B'},
  {'9', '6', '3', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9 ,8 ,7 ,6};
byte colPins[COLS] = {13, 12, 11, 10};
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
boolean MN = false;
boolean next = false;
boolean final = false;
char phoneNumber[20];
byte phoneIndex = 0;

void setup(){
  lcd.begin(16,2);
  lcd.print("sdadasdsa");
  lcd.setCursor(0,1);
  lcd.print(" sdasadasd");
  }

  void loop(){
  char o = myKeypad.getKey();

  if (o == 'D'){
    phoneNumber[phoneIndex] = o;
    phoneNumber[phoneIndex++] = '\0';
    lcd.clear();
    delay(50);
    lcd.begin(16,2);
    lcd.print("Enter mobile no.");
    lcd.setCursor(0,1);
    lcd.print(phoneNumber);
    }
  }
  char o = myKeypad.getKey();

  if (o == 'D'){
    phoneNumber[phoneIndex] = o;
    phoneNumber[phoneIndex++] = '\0';

If the user presses the 'D' key, store that in the phoneNumber array, and show the phone number on the LCD.

Does that make sense?

Seems to me that you'd want to store the key, if it is not NO_KEY and is a digit, in the array. The 'D' should say done - show what you've stored. Although the 'D' should mean that you can use, not display, the number. The number should be displayed all the time.

well, what i'm trying to do is, if the user presses 'D' the device will ask for the phone number so that the device could use it to send it's message, i revised it and here it is

  if(o == 'D')
  {
    char key = myKeypad.getkey();
    lcd.clear();
    delay(50);
    lcd.begin(16,2);
    lcd.print("Enter mobile no.");
    lcd.setCursor(0,1);
    lcd.print(key);
    lcd.cursor();
    }
  }

but now it doesn't display the key that i'm pressing on the lcd. it only shows me this weird symbol before the cursor

Pressing the D does not cause anything to be stored. What it should do is set phoneIndex to 0, set phoneNumber[0] to '\0' and set a state that indicates that what is being entered is a phone number.

On every pass through loop(), check for a key press. If there is one, what to do with it depends on the state and/or which key was pressed.

PaulS:
Pressing the D does not cause anything to be stored. What it should do is set phoneIndex to 0, set phoneNumber[0] to '\0' and set a state that indicates that what is being entered is a phone number.

yeah! that's what i'm trying to do, i've inserted the commands that you gave me in my if(o=='D') statement.

#include <LiquidCrystal.h>
#include <Keypad.h>
LiquidCrystal lcd(5, 4, 3, 2, 1, 0);
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS] [COLS] = {
  {'1', '2', '3', 'A'},
  {'8', '5', '2', 'B'},
  {'9', '6', '3', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {13, 12, 11, 10};
byte colPins[COLS] = {9 , 8 , 7 , 6};
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
boolean MN = false;
boolean next = false;
boolean final = false;

void setup() {
  lcd.begin(16, 2);
  lcd.print("Empty bottles to");
  lcd.setCursor(0, 1);
  lcd.print("free texts");
}
void loop() {
  char o = myKeypad.getKey();
  if (o == 'D')
  {
    char key = myKeypad.getKey();
    char phoneNumber[11];
    byte phoneIndex = 0;
    phoneNumber[phoneIndex] = key;
    phoneNumber[phoneIndex++] = '\0';
    lcd.clear();
    delay(50);
    lcd.begin(16, 2);
    lcd.print("Enter mobile no.");
    lcd.setCursor(0, 1);
    lcd.print(phoneNumber[phoneIndex++]);
    lcd.cursor();
  }
}

You need a while loop in the if(o == 'D') block, that loops until the whole number has been entered.

You need to test that key has a value before you use it.

You need to THINK!

When do you want to prompt the user to enter a phone number and when do you want to use the entered data? At the same time is the wrong answer.

After he pressed 'B', so first the user will press 'D' and then, user will press 'C' to display the number, then pressing 'B' will send the message? sorry for the dumb questions =)

and, Can I declare, a variable as another getKey() inside a loop if i already declared it as a variable outside?

and, Can I declare, a variable as another getKey() inside a loop if i already declared it as a variable outside?

No. You can define another variable, anywhere you want, and assign it the value returned by getKey().

Personally, I think that you need to get out of the deep end of the pool until you learn to swim.

Your project is doomed to failure AT YOUR CURRENT EXPERIENCE LEVEL.