Keypad project

good day

I am using a keypad to enter machine number, once i have entered the machine number i want to enter the job number. i am able to enter machine value but it does not allow me to enter job number. it go back to enter machine number

void loop()
{
lcd.setCursor(0,0);
lcd.print(“ENTER MC NO.”);
lcd.setCursor(0,1);

customKey = customKeypad.getKey();
if (customKey == ‘1’){
lcd.print(customKey);
delay(1000);
lcd.clear();
lcd.print(“Machine 1”);
delay(1000);
lcd.clear();
lcd.print(“job number”);
delay(1000);
lcd.setCursor(0,1);
Key = customKeypad.getKey();
if(Key == ‘1’){
lcd.print(Key);
delay(1000);
lcd.clear();
lcd.print(“DYE LOT SEL.”);
delay(1000);
lcd.clear();

}
}
}

sketch_aug12b.ino (1.07 KB)

i am able to enter machine value but it does not allow me to enter job number.

That's not really surprising if you look at the code. If you don't enter a job number within 1 second of entering the machine value then the loop() function will start again.

How to put it right ?

Create a new variable, let's name it inputPhase, and set it to 0. Then, in loop() if inputPhase is 0 prompt for the machine value. When you have the machine value, perhaps signalled by the entry of a '', change inputPhase to 1. In loop() if inputPhase is 1 prompt for the job number. When you have the job number, perhaps signalled by the entry of a '', run any code that you want using the 2 values then set inputPhase back to 0 to restart the process

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

I think you will want a “State Machine” design, like this:

//#include "DFRobot_RGBLCD.h"
#include <Keypad.h>


int MachineNumber = 0;
int JobNumber = 0;


const byte ROWS = 4;
const byte COLS = 4;


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


byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};


Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//DFRobot_RGBLCD lcd(16, 2);


void setup()
{
  Serial.begin(9600);
  // lcd.init();


  // lcd.setCursor(0, 0);
  // lcd.print("ENTER MC NO.");
  // lcd.setCursor(0, 1);
}


enum States {WAITING_FOR_MACHINE_NUM, WAITING_FOR_JOB_NUM, READY} State = WAITING_FOR_MACHINE_NUM;


void loop()
{
  byte customKey = customKeypad.getKey();
  
  if (customKey != NO_KEY)
  {
    switch (State)
    {
      case WAITING_FOR_MACHINE_NUM:
        if (customKey >= '0' && customKey <= '9')
        {
          MachineNumber = customKey - '0';
          State = WAITING_FOR_JOB_NUM;
        }
        break;


      case WAITING_FOR_JOB_NUM:
        if (customKey >= '0' && customKey <= '9')
        {
          JobNumber = customKey - '0';
          State = READY;
        }
        break;


      case READY:
        // The Machine Number and Job Number are set.
        break;
    }
  }
}

my problem is i have machine 1 to 25, so i need my code to allow me to enter machine number from that range. and enter a job number, i have job numbers from 1 to 5. once i have entered the job number i need to input time it will take for that job to be done and sound an alarm when the time has elapsed. now i am stuck at allowing to enter job number after i have entered machine number.

i have tried the recommended updates but its does not seem to be coming right.

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

}

void loop()
{

lcd.setCursor(0,0);
lcd.print("ENTER MC NO.");
lcd.setCursor(0,1);

customKey = customKeypad.getKey();
if( inputPhase = 0){
if (customKey == '1'){
lcd.print(customKey);
delay(1000);
lcd.clear();
lcd.print("Machine 1");
delay(1000);
lcd.clear();
}
inputPhase++;
if (inputPhase = 1){
lcd.print("job number");
delay(1000);
lcd.setCursor(0,1);
Key = customKeypad.getKey();
if(Key == '1'){
lcd.print(Key);
delay(1000);
lcd.clear();
lcd.print("DYE LOT SEL.");
delay(1000);
lcd.clear();
lcd.print("Enter Tim in min");
delay(1000);
}

}
}
}

I dont have a circuit diagram or any sketch, I am only using arduino mega, lcd and keypad

Blue10:
my problem is i have machine 1 to 25

Im reading on my mobile so may missed something, but afaics the code you have doesnt cater for numbers of more than one digit.

I have a function that reads multi-digit numbers: will post it later if you need it.

Using that it will be very simple to call it twice to populate two variables.

no for now i am sticking to single digit and as soon as it work i will extend to two digit

That thinking’s going to bite your arse, but hey, it’s your arse.

It would be much neater to use a function to populate your variables even if they are only one digit each.

how can i use them coz i have tried array and it giving me errors

#include <Wire.h>
#include “DFRobot_RGBLCD.h”
#include <Keypad.h>

#define MachineNumber 5
char Machine[MachineNumber] = {‘1’,‘2’,‘3’,‘4’};
char customKey;

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
{‘1’, ‘4’, ‘7’, ‘*’},
{‘2’, ‘5’, ‘8’, ‘0’},
{‘3’, ‘6’, ‘9’, ‘#’},
{‘A’, ‘B’, ‘C’, ‘D’}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

DFRobot_RGBLCD lcd(16, 2);

void setup(){
lcd.init();

}

void loop(){
lcd.setCursor(0,0);

customKey = customKeypad.getKey();
if(customKey = Machine){
lcd.print(customKey);
delay (1000);
lcd.clear();

}

}

Run the following code to see how it works. It calls a function, getKeypadInteger() and returns the value to a variable, myValue.

When you follow how it works, copy the only the function once only into any other sketch, and then call the same function as many times as you like, each into a different variable eg your machine and job numbers.

(There is a similar but slightly different sketch somewhere in the forum. It’s better than mine in that it allows you to backspace out any typos. so with mine, just type carefully :wink: These and similar approaches all accumulate the integer by multiplying what you have so far by 10, to nudge the units into the tens and the tens into the hundreds etc.)

// with help from https://stackoverflow.com/questions/39232634/how-to-input-a-multi-digit-integer-into-an-arduino-using-a-4x4-keypad

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
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 to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

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

void setup()
{
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Serial.begin(9600);
  int myValue = getKeypadInteger();
  Serial.print("I received ");
  Serial.println(myValue);
  //
  Serial.print("Just to prove it's a number not a string, multiplied by 2 it's ");
  Serial.println(2 * myValue);
}

void loop()
{
}//loop


int getKeypadInteger()
{
  int value = 0;                                // the number accumulator
  int keyval;                                     // the key press
  int isnum;                                    //is it a number?
  Serial.println("Enter digits, any non-digit to end ");
  Serial.print("So far: ");
  do
  {
    keyval = keypad.getKey();                          // input the key
    isnum = (keyval >= '0' && keyval <= '9');         // is it a digit?
    if (isnum)
    {
      Serial.print(keyval - '0');
      value = value * 10 + keyval - '0';               // accumulate the input number
    }

  } while (isnum || !keyval);                          // until not a digit or while no key pressed
  Serial.println(" "); Serial.print("Returning "); Serial.println(value);
  return value;

}//getKeypadInteger

Hi,
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Note;

if(customKey = Machine)

Should be;

 if(customKey == Machine)

What errors are you getting?
Are they compile errors?
Unless you tell us what the errors are, we cannot really help you.

Tom... :slight_smile:

my problem is that i cant input machine job number after inputting time, it just skip that line of code and go to start and request for machine number again

With my function you can input as many multi-digit integers to as many variables as you like: so forget the code for a moment and describe in words the sequence of what's supposed to happen.

firstly I want to input any machine number from 1 to 25, after i have input machine number i want to input job description which i used numbers from 1 to 5 to represent each job description. once i have entered the job number i want to enter time in minutes that it will take to do that job and when that time has elapsed sound an siren

Blue10:
firstly I want to input any machine number from 1 to 25, after i have input machine number i want to input job description which i used numbers from 1 to 5 to represent each job description. once i have entered the job number i want to enter time in minutes that it will take to do that job and when that time has elapsed sound an siren

Blue10 I have written a sketch using a state machine approach, implemented with switch...case, and which:

  • Starts in an INPUT state and uses getKeypadInteger() to prompt for the machine number, job number and job time
  • Moves to a RUNNING state where (since we don't know what your actual process is) turns on an LED for the requested time
  • Moves to a SIREN state where a siren (actually a led in my test case) is turned on, awaiting user to press a button to cancel the siren
  • Moves back to INPUT for the next job

Here's the state diagram showing what I did. If you want the sketch, post back.

(added a couple of links in the previous post)

this is the code I have right now. if you look through the code you will see that I have used waitForkey command because when I used the getKey command its do not allow me to enter the job number and time.

#include <Wire.h>
#include “DFRobot_RGBLCD.h”
#include <Keypad.h>

#define MachineNumber 9
#define TimeInMinutes 4 // to user must convert hours to minutes before entering
char Machine[MachineNumber] = {‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’};
char Minutes[TimeInMinutes];
char JobNumber1 = ‘1’;
char JobNumber2 = ‘2’;
char JobNumber3 = ‘3’;
char JobNumber4 = ‘4’;
char JobNumber5 = ‘5’;
char customKey;
byte Data_count = 0;
int x;

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
{‘1’, ‘4’, ‘7’, ‘*’},
{‘2’, ‘5’, ‘8’, ‘0’},
{‘3’, ‘6’, ‘9’, ‘#’},
{‘A’, ‘B’, ‘C’, ‘D’}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

DFRobot_RGBLCD lcd(16, 2);

void setup(){
lcd.init();

}

void loop(){
lcd.setCursor(0,0);
lcd.print(“ENTER MACH NO.”);

customKey = customKeypad.getKey();
for(byte i = 0; i < MachineNumber; i++) {
if(customKey == Machine*){*

  • lcd.setCursor(0,1);*

  • lcd.print(customKey);*

  • delay (1000);*

  • lcd.clear();*

  • lcd.print(“Machine Number”);*

  • lcd.print(customKey);*

  • delay(1000);*

  • lcd.clear();*

  • lcd.print(“Enter Job No.”);*

  • lcd.setCursor(0,1);*

  • customKey = customKeypad.waitForKey();*

  • if(customKey == JobNumber1){*

  • lcd.print(“DYE LOT SEL.”);*

  • delay(1000);*

  • lcd.clear();*

  • lcd.print(“ENTER TIME”);*

  • customKey = customKeypad.waitForKey();*

  • if(customKey){*

  • Minutes[Data_count] = customKey;*

  • lcd.setCursor(Data_count,1);*

  • lcd.print(Minutes[Data_count]);*

  • Data_count++;*

  • }*

  • }*

  • }*

  • }*
    }

Can you please provide me with a sketch for it