#include <Keypad.h>
#include <math.h>
#include <Stepper.h>
int AModeTime = 100;
int BModeTime = 100;
int CModeTime = 100;
char curMode = 'A';
int leftLED = 2;
int rightLED = 3; //using this to show direction
int motor1 = 2;
int motor2 = 3;
int motor3 = 4;
int motor4 = 5;
char durationBuffer[6] = {};
int buffIndex = 0;
long duration = 0;
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {10, 11, 12, 13};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad inpKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
Stepper motor = Stepper(100, motor1, motor2, motor3, motor4);
void setup() {
// put your setup code here, to run once:
for (int i = 6; i < 14; i++)
{
pinMode(i, INPUT);
}
Serial.begin(9600);
Serial.println("Working");
}
void modeA()
{
delay(500);
Serial.println("mode A, set dur");
motor.step(-100);
duration = getDuration();
//start spin
delay(duration);
}
void modeB()
{
Serial.println("in mode B, enter duration");
duration = getDuration();
motor.step(-100);
delay(duration);
}
void modeC()
{
Serial.println("in mode C, enter duration");
duration = getDuration();
motor.step(100);
delay(duration);
}
void modeD()
{
modeA();
modeB();
modeC();
modeA();
modeB();
modeC();
}
long getDuration()
{
buffIndex = 0;
duration = 0;
while (1)
{
char customKey = inpKeypad.getKey();
if (customKey == '#')
{
break;
}
durationBuffer[buffIndex] = customKey - '0';
buffIndex++;
}
for (int i = 0; i < buffIndex; i++)
{
duration += durationBuffer[i] * pow(10, buffIndex-i);
}
Serial.println("calculated Duration as: ");
Serial.println(duration);
return duration;
}
void changeMode(char inp)
{
Serial.println("in changeMode()");
delay(500);
switch (inp)
{
case 'A':
modeA();
break;
case 'B':
delay(500);
modeB();
break;
case 'C':
delay(500);
modeC();
break;
case 'D':
delay(500);
modeD();
break;
default:
Serial.println("Something went very very wrong");
}
}
void loop() {
// put your main code here, to run repeatedly:
motor.setSpeed(50);
char customKey = inpKeypad.getKey();
if (customKey){
Serial.println(" Key is:");
Serial.println(customKey);
changeMode('A');
}
}