#include <Stepper.h>
#include <Keypad.h>
const int stepsPerRevolution = 200; // Change this to fit the number of steps per revolution for your motor
const int motorSpeedSlow = 10; // Speed for slow movement
const int motorSpeedNormal = 60; // Speed for normal movement
const int motorSpeedFast = 120; // Speed for fast movement
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
const byte ROW_NUM = 4; // four rows
const byte COLUMN_NUM = 4; // four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
void setup() {
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Emergency brake key
emergencyBrake();
} else {
handleKeypadInput(key);
}
}
// No delay to allow continuous operation
}
void handleKeypadInput(char key) {
int motorSpeed;
switch (key) {
case '1':
motorSpeed = motorSpeedSlow;
break;
case '2':
motorSpeed = motorSpeedNormal;
break;
case '3':
motorSpeed = motorSpeedFast;
break;
case 'A':
Serial.println("Clockwise - Speed: " + String(motorSpeed));
fakeIndicator(5); // Simulate motor working with 5 dots
myStepper.setSpeed(motorSpeed);
myStepper.step(stepsPerRevolution);
return;
case 'B':
Serial.println("Counterclockwise - Speed: " + String(motorSpeed));
fakeIndicator(5); // Simulate motor working with 5 dots
myStepper.setSpeed(motorSpeed);
myStepper.step(-stepsPerRevolution);
return;
// Add more cases for additional keys if needed
}
// Move the stepper motor based on the selected speed
Serial.println("Speed: " + String(motorSpeed));
fakeIndicator(5); // Simulate motor working with 5 dots
myStepper.setSpeed(motorSpeed);
myStepper.step(stepsPerRevolution);
}
void emergencyBrake() {
Serial.println("Emergency Brake!");
myStepper.setSpeed(0); // Set speed to 0 for an emergency brake
myStepper.step(0); // Stop the motor immediately
}
void fakeIndicator(int dots) {
for (int i = 0; i < dots; ++i) {
Serial.print(".");
delay(500); // Adjust delay as needed
}
Serial.println();
}