Keypad Stepper

The pins used are just an example, please change them to fit your needs.

/*
 || @version 1.0
 || @author Andrew Mascolo
 || @date July 3, 2013
 ||
 || @description
 || Simple use of keypad and Stepper
*/

#include <Keypad.h>
#include <Stepper.h>
Stepper stepper(100, 10, 11, 12, 13);

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

char customKey;
int  Value = 0, StepVal = 0;
bool Allow_to_step = false;
//=========================================================

/*
S = Change SetSpeed
D = Change Direction
A = Advance 1 step
R = Retreat 1 step
B = Break / Stop, also sets Value and StepVal to zero
G = Go
*/
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'S'},
  {'4', '5', '6', 'D'},
  {'7', '8', '9', 'A'},
  {'B', '0', 'G', 'R'}
};
//=========================================================

byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup()
{
  Serial.begin(115200);
  stepper.setSpeed(10);
}

void loop()
{
  customKey = customKeypad.getKey();

  if (customKey >= '0' && customKey <= '9') // This keeps collecting the Value until customKey is not a number
  {
    Value = Value * 10 + (customKey - '0'); // concatnate key presses into one value
    Serial.print(Value);
  }
  else
  {
    Serial.println();
    switch (customKey)
    {
      case 'S': // Change set speed
        Allow_to_step = false;
        stepper.setSpeed(Value); 
        Serial.print(F("Speed: "));
        Serial.println(Value);
        break;

      case 'D':
        StepVal *= -1;
        Serial.print(F("Direction: "));
        Serial.println(StepVal > 0 ? "ClockWise" : "Counter Clockwise");
        break;

      case 'A':
        Allow_to_step = false;
        StepVal++;
        stepper.step(1);
        Serial.println(F("Advance"));
        break;

      case 'R':
        Allow_to_step = false;
        StepVal--;
        stepper.step(-1);
        Serial.println(F("Retreat"));
        break;

      case 'B':
        StepVal = 0;
        Value = 0;
        Serial.println("Break / Clear Value");
        Allow_to_step = false;
        break;

      case 'G':
        StepVal = Value;
        Allow_to_step = true;
        Serial.println(F("Go"));
        break;

      default: break; // numbers not allowed
    }
  }
  if (Allow_to_step == true)
    stepper.step(StepVal);
}

Share