Hi all, I know there's an easier way to do this but I'm just not sure how. I come from a ladder logic/PLC background so using functions makes the most sense to me.
Here's a video of everything in action. Arduino stepper positions set using keypad passwords and a momentary button. - YouTube
The video description lays everything out.
The thing I've had the most difficulty with so far is having the stepper wait for a button press to confirm it's ok to move. My fix was to make each function loop till the button is pressed. But I think this will create problems in the future if I want a timeout function or something.
Anyway here's the code tell me what you think? I'm linking on my phone so excuse any formatting errors.
/*Project Title: Multiple Keypad Passwords for Multiple Stepper Positions
Keypad Connection
* ROW0 to digital pin 22
* ROW1 to digital pin 28
* ROW2 to digital pin 26
* ROW3 to digital pin 24
* COL0 to digital pin 25
* COL1 to digital pin 23
* COL2 to digital pin 27
Operation:
'#' = OK -> checks if the password is correct or not
'*' = CLEAR -> clears the password typed
*/
#include <AccelStepper.h>
#include <Password.h>
#include <Keypad.h>
Password redPass = Password( "789" ); //This is red password
Password bluPass = Password( "456" ); //This is blue password
Password grnPass = Password( "123" ); //This is green password
// Define a stepper and the pins it will use
AccelStepper stepper(1,2,3); //1 = EasyDriver, 2 = step, 3 = direction
//LED pins
#define grnLED 42
#define redLED 41
#define bluLED 40
#define waitLED 52
#define goLED 51
int buttonPin = 53; // the pin that the pushbutton is attached to
// Variables will change:
const byte ROWS = 4; // 4 rows
const byte COLS = 3; // 3 columns
char keys[ROWS][COLS] = { // Define the Keymap
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {22, 28, 26, 24};// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = {25, 23, 27};// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
pinMode(grnLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(bluLED, OUTPUT);
pinMode(waitLED, OUTPUT);
pinMode(goLED, OUTPUT);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, LOW);
Serial.begin(9600);
Serial.print("Enter Password: ");
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
stepper.setMaxSpeed(5000.0);
stepper.setAcceleration(2000.0);
}
void loop()
{
keypad.getKey();
}
//check the keypad events
void keypadEvent(KeypadEvent keyPress)
{
switch (keypad.getState())
{
case PRESSED:
Serial.print(keyPress); //print the keypress
switch (keyPress)
{
case '#':
checkPassword();
break;
case '*':
grnPass.reset();
digitalWrite(goLED, LOW);
Pos_Home();
digitalWrite(grnLED, LOW);
Serial.print(" ");
redPass.reset();
digitalWrite(goLED, LOW);
Pos_Home();
digitalWrite(redLED, LOW);
Serial.print(" ");
bluPass.reset();
digitalWrite(goLED, LOW);
Pos_Home();
digitalWrite(bluLED, LOW);
Serial.print(" ");
break;
default:
grnPass.append(keyPress);
redPass.append(keyPress);
bluPass.append(keyPress);
}
}
}
//check the entered password
void checkPassword(){
if (grnPass.evaluate()) {//if green password is correct
Serial.println(" Correct Password"); //print a message on serial monitor
digitalWrite(grnLED, HIGH); //turn ON green LED
Pos_1(); //add function for stepper position 1
grnPass.reset(); //reset password
}
else if (bluPass.evaluate()) { //if blue password is correct
Serial.println(" Correct Password"); //print a message on serial monitor
digitalWrite(redLED, HIGH); //turn ON red LED
Pos_2(); //add function for stepper position 2
bluPass.reset(); //reset password
}
else if (redPass.evaluate()) { //if red password is correct
Serial.println(" Correct Password"); //print a message on serial monitor
digitalWrite(bluLED, HIGH); //turn ON yellow LED
Pos_3(); //add function for stepper position 3
redPass.reset();//reset password
}
else //if wrong password
{
Serial.println(" Wrong Password"); //if password is wrong
digitalWrite(grnLED, HIGH); //cascade LEDs
delay(100);
digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(bluLED, HIGH);
delay(500);
digitalWrite(grnLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(bluLED, LOW);
grnPass.reset(); //reset passwords
redPass.reset();
bluPass.reset();
}
}
void Pos_Home() { //stepper home position
digitalWrite(waitLED, HIGH);
stepper.runToNewPosition(0);
digitalWrite(waitLED, LOW);
}
void Pos_1() { //stepper position 1
digitalWrite(waitLED, HIGH);
if(digitalRead(buttonPin) == HIGH) { //if button is presed
/////would like to add flashing redLED while stepper is moving/////
stepper.runToNewPosition(200);
digitalWrite(waitLED, LOW);
digitalWrite(goLED, HIGH);
}
else { //loop until button pressed
Pos_1();
}
}
void Pos_2() { //stepper position 2
digitalWrite(waitLED, HIGH);
if(digitalRead(buttonPin) == HIGH) { //if button is presed
/////would like to add flashing redLED while stepper is moving/////
stepper.runToNewPosition(400);
digitalWrite(waitLED, LOW);
digitalWrite(goLED, HIGH);
}
else { //loop until button pressed
Pos_2();
}
}
void Pos_3() { //stepper position 3
digitalWrite(waitLED, HIGH);
if(digitalRead(buttonPin) == HIGH) { //if button is presed
/////would like to add flashing redLED while stepper is moving/////
stepper.runToNewPosition(800);
digitalWrite(waitLED, LOW);
digitalWrite(goLED, HIGH);
}
else { //loop until button pressed
Pos_3();
}
}