Setting States usig Keypad

Hello,

I am currently working on a project which requires setting multiple variables using a keypad, and once all of the variables are set will run a program. From my research it looks like using States is the best option for this. I've modeled a code off of This Link. The problem that I am currently having is that it doesn't seem to be registering when I press a key. I feel like this is an easy fix, however I have very little coding/ Arduino knowledge.

Example of what code is intended to do: *In every case the user must input a value for all variables

  • User presses 'A' on keypad, then enters 5-0 on the keypad. The user then selects '#' to set the value of the speed variable to 50. The user continues this process to set the load and time as well (using 'B' and 'C' for the load and timer, respectively). When all of variables are entered, the user will press 'D' twice to start running the machine. The '*' is going to be used as a backspace/ clear key

Here is where I am currently at with the code:

// State Maachine Test Code 

#include <Keypad.h>


int v1 = 0;
int v2 = 0;
int v3 = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

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

 

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Creates Key map

// Possible States 
typedef enum { State_None, 
               State_L,
               State_S,
               State_T,
               State_D } states;

// Current State
states state = State_None;

// Variables which I would like assigned 
int Loadval = 0;
int Speedval = 0;
int Timerval = 0;

int recievedNumber = 0;    // Current Number
char key = kpd.getKey();

void setup ()
  {
    Serial.begin(9600);
    Serial.print("Welcome");
  }


void gotA (const char key)  // 'A' was pressed
  {
    char Key = 'A';
    int recievedNumber = 0;    // Current Number
      
    
    switch (state)
    {
      case State_None:
      Serial.print("Enter Desired Speed"); 
      state = State_S;
      break;

      case State_L:
      Serial.println("Value Not Stored");
      state = State_S;
      break;

      case State_S:
      Serial.println("Value Not Stored");
      state = State_None;
      break;

      case State_T:
      Serial.println("Value Not Stored");
      state = State_S;
      break;

      case State_D:
      Serial.println("Select 'D' to Start");
      Serial.print("Select '*' to Return"); 
      break; 
    }
  }

void gotB (const char key)  // B' was Pressed
  {'
    int recievedNumber = 0;    // Current Number
    char Key ='B';
    switch (state)
    {
      case State_None: 
      state = State_L;
      break;

      case State_L:
      Serial.println("Value Not Stored");
      state = State_None;
      break;

      case State_S:
      Serial.println("Value Not Stored");
      state = State_L;
      break;

      case State_T:
      Serial.println("Value Not Stored");
      state = State_L;
      break;

      case State_D:
      Serial.println("Select 'D' to Start");
      Serial.print("Select '*' to Return"); 
      break; 
    }
  }

void gotC (const char key)  // 'C' was Pressed
  {
    char Key = 'C';
    int recievedNumber = 0;    // Current Number
   
    switch (state)
    {
      case State_None: 
      state = State_T;
      break;

      case State_L:
      Serial.println("Value Not Stored");
      state = State_T;
      break;

      case State_S:
      Serial.println("Value Not Stored");
      state = State_T;
      break;

      case State_T:
      Serial.println("Value Not Stored");
      state = State_None;
      break;

      case State_D:
      Serial.println("Select 'D' to Start");
      Serial.print("Select '*' to Return"); 
      break; 
    }
  }


void gotD (const char key)  // 'D' was Pressed
  {
    char Key = 'D';
    switch (state)
    {
      case State_None: 
      Serial.println("Select 'D' to Start");
      Serial.print("Select '*' to Return");
      state = State_D;
      break;

      case State_L:
      Serial.print("Please Input all Variables");
      state = State_None;
      break;

      case State_S:
      Serial.print("Please Input all Variables");
      state = State_None;
      break;

      case State_T:
      Serial.print("Please Input all Variables");
      state = State_None;
      break;

      case State_D:
      {
      if (State_L > 0 && State_S > 0 && State_T > 0)
      Serial.println("Starting Program");
      
      else 
      Serial.println("Not All Variables Set");
      Serial.print("Select '*' to Return"); 
      break; 
      }
    }
  }

void gotp (const char key)  // '#' was Pressed
  {
     char Key = '#';
    switch (state)
    {
      case State_None: 
      Serial.print("Please Select Input");
      break;

      case State_L:
      Serial.print("Load Stored: ");
      Loadval = recievedNumber;
      Serial.print(Loadval);
      state = State_None;
      
      break;

      case State_S:
      Serial.print("Speed Stored: ");
      Speedval = recievedNumber;
      Serial.print(Speedval);
      state = State_None;
      break;

      case State_T:
      Serial.print("Timer Stored: ");
      Timerval = recievedNumber;
      Serial.print(Timerval);
      state = State_None;  
      break;

      case State_D:
      Serial.println("Select 'D' to Start");
      Serial.print("Select '*' to Return"); 
      break; 
    }
  }

  void gote (const char key)  // '*' was Pressed
  {
    char Key = '*';

    switch (state)
    {
      case State_None: 
      Serial.print("Please Select Input");
      break;

      case State_L:
      Serial.print("Value Cleared");
      recievedNumber = 0;
      break;

      case State_S:
      Serial.print("Value Cleared");
      recievedNumber = 0;
      break;

      case State_T:
     Serial.print("Value Cleared");
      recievedNumber = 0;
      break;

      case State_D:
      Serial.print("Returning");
      state =  State_None;
      break; 
    }
  }

  void gotDigit (const char key) // Any Digit is recieved
  {
  int receivedNumber = 0;
 int Key = '0'||'1'||'2'||'3'||'4'||'5'||'6'||'7'||'8'||'9';
 
  switch (state)
    {
    case State_None: 
         Serial.print("Please Select Input");
         break;

    case State_L:
    state = State_L;
    break;
    
    case State_S:
    state = State_S;
    break;
    
    case State_T:
         state = State_T;
         break; 
          
    case State_D:
         Serial.println("Select 'D' to Start");
         Serial.print("Select '*' to Return"); 
         break; 
    }  // end of switch on state
    
  receivedNumber *= 10;
  receivedNumber += key - '0';
    
  }  // end of gotDigit

void ProcessInput ()  // Assigns function depeddning on which key was pressed
{
 switch (key)
 {
  case 'A':
  gotA(key);
  break;

  case 'B':
  gotB(key);
  break;

  case 'C':
  gotC(key);
  break;

  case 'D':
  gotD(key);
  break;

  case '#':
  gotp(key);
  break;

  case '*':
  gote(key);
  break;

  case '0': case '1': case '2': case '3': case '4':
  case '5': case '6': case '7': case '8': case '9': 
  gotDigit(key);
  break;
  
 }
}

void loop ()
{
while (key != 0)  // If a key is pressed, process the input 
ProcessInput ();
}

Any help is greatly appreciated, thank you!

The code you posted does something. You did not way what it actually does.

You expected the code to do something. You did not say what you expected it to do.

"It doesn't work" will get you nowhere.

Thank you for your response, I thought I posted what I expected it to do in the question, but I guess I didn't articulate it correctly. Basically this is the start up sequence for a testing machine which requires the following variables to be set by the user before it can start running: Speed, Load, and Timer. For this forum I just need it to get to the point where it will assign a value each of these variables by using a 4x4 keypad. I decided to make it so that each of the letters (A, B, and C) on the keypad will switch the state to its respective variable, which can then be modified. For example, if the user wants to adjust the speed to 50 mph, he/she would click the 'A' button on the keypad, then 5-0. To enter the value the user will have to select the '#' key. If the user wants to clear the number, he/she can click the '*' key. Once the Speed, Load, and Time variables have been set, the user can press 'D' twice to run the program. For this question i just need to get to the point where the serial will display the numbers that I am pressing to confirm that it is working before I begin on the rest of the project.

What is happening for what I have now is that the keypad doesn't appear to be registering anything. So when I start it up, it'll say "Welcome", but that is all. If I click buttons on the keypad nothing changes

If I click buttons on the keypad nothing changes

So, where do you get data from the keypad?

Here:

char key = kpd.getKey();

But, this happens outside of any function, so, unless you happen to be holding a key down when the Arduino resets, key will be set to NO_KEY.

Nowhere else do you actually read from the keypad.

Go back to the examples that come with the Keypad library. Look at how they read the keypad.

I was able to get the keypad working with a single function, so I guess my confusion comes from the multiple functions with States. I originally tried this:

// Pressed 'A' on the keypad
void gotA (const char key)
  {
    
    char key = kpd.getKey();
    int recievedNumber = 0;    // Current Number
      
    
    switch (state)
    {
      case State_None:
      Serial.print("Enter Desired Speed"); 
      state = State_S;
      break;

      case State_L:
      Serial.println("Value Not Stored");
      state = State_S;
      break;

      case State_S:
      Serial.println("Value Not Stored");
      state = State_None;
      break;

      case State_T:
      Serial.println("Value Not Stored");
      state = State_S;
      break;

      case State_D:
      Serial.println("Select 'D' to Start");
      Serial.print("Select '*' to Return"); 
      break; 
    }
  }

However, I kept getting "declaration of 'char key' shadows parameter, which was caused by the char key = kpd.getKey();

char key = kpd.getKey(); creates a new variable and values it with the value read from the keypad.

key = kpd.getKey(); populates an existing variable with the value read from the keypad.