Hello, I am working on a project that uses a timer to control a system. The system has two NEMA 17 stepper motors and a LED strip. The system is controlled by the timer entered through the keypad and I have trouble figuring out how to turn the motors on when the timer starts. Here is my code that only controls how long the LED strip turns on.
Here is my code.
#include <Wire.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
int Blue_LED = 2;
char currentTimeValue[4];
int currentState = 1;
int timerSeconds = 0;
int lpcnt = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {6,7,8,9};
byte colPins[cols] = {3,4,5};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
LiquidCrystal lcd(15,14,10,11,12,13);
void setup()
{
lcd.begin(16,2);
timeEntryScreen();
pinMode(Blue_LED, OUTPUT);
digitalWrite(Blue_LED, LOW);
currentTimeValue[0]='0';
currentTimeValue[1]='0';
currentTimeValue[2]='0';
currentTimeValue[3]='0';
showEnteredTime();
}
void loop()
{
int l;
char tempVal[3];
char key = keypad.getKey();
if(int(key) != 0 and currentState == 1) {
switch (key) {
case '*':
lightStatus(false);
currentTimeValue[0]='0';
currentTimeValue[1]='0';
currentTimeValue[2]='0';
currentTimeValue[3]='0';
showEnteredTime();
currentState = 1;
lpcnt = 0;
timerSeconds = 0;
break;
case '#':
tempVal[0] = currentTimeValue[0];
tempVal[1] = currentTimeValue[1];
tempVal[2] = 0;
timerSeconds = atol(tempVal) * 60;
tempVal[0] = currentTimeValue[2];
tempVal[1] = currentTimeValue[3];
tempVal[2] = 0;
timerSeconds = timerSeconds + atol(tempVal);
currentState = 2;
break;
default:
currentTimeValue[0]= currentTimeValue[1];
currentTimeValue[1]= currentTimeValue[2];
currentTimeValue[2]= currentTimeValue[3];
currentTimeValue[3]= key;
showEnteredTime();
break;
}
}
if(currentState == 2){
if(int(key) != 0) {
if(key == '*'){
lightStatus(false);
timeEntryScreen();
currentTimeValue[0]='0';
currentTimeValue[1]='0';
currentTimeValue[2]='0';
currentTimeValue[3]='0';
showEnteredTime();
currentState = 1;
lpcnt = 0;
timerSeconds = 0;
}
} else {
if(lpcnt > 9) {
lpcnt = 0;
--timerSeconds;
showCountdown();
if(timerSeconds <= 0) {
currentState = 1;
lightStatus(false);
timeEntryScreen();
showEnteredTime();
} else {
lightStatus(true);
}
}
++lpcnt;
delay(100);
}
}
}
void showEnteredTime()
{
lcd.setCursor(5,1);
lcd.print(currentTimeValue[0]);
lcd.print(currentTimeValue[1]);
lcd.print(":");
lcd.print(currentTimeValue[2]);
lcd.print(currentTimeValue[3]);
}
void lightStatus(bool state)
{
if(state)
digitalWrite(Blue_LED, HIGH);
else
digitalWrite(Blue_LED, LOW);
}
void showCountdown()
{
char timest[6];
lcd.setCursor(0,0);
lcd.print("** TIME LEFT: **");
lcd.setCursor(0,1);
lcd.print("** ");
sprintf(timest, "%d:%.2d",(timerSeconds/60),(timerSeconds - ((timerSeconds/60*60))));
lcd.print(timest);
lcd.print(" **");
}
void timeEntryScreen()
{
clearScreen();
lcd.setCursor(0,0);
lcd.print("** ENTER TIME **");
}
void clearScreen()
{
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}