I need some help with my program. I am trying to call the motor function whenever the timer is entered by the user. The user enters the timer through the keypad and the time displays on the LCD display. I have the LED lights working correctly, but the motors are not working with the timer. Can someone help me?
Here is my code.
#include <Wire.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
int Blue_LED = 2;
int stepper1_DIR = 17;
int stepper1_STEP = 18;
int stepper2_DIR = 19;
int stepper2_STEP = 20;
int Distance = 0; // Record the number of steps we've taken
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);
pinMode(stepper1_DIR, OUTPUT);
pinMode(stepper1_STEP, OUTPUT);
digitalWrite(stepper1_DIR, LOW);
digitalWrite(stepper1_STEP, LOW);
pinMode(stepper2_DIR, OUTPUT);
pinMode(stepper2_STEP, OUTPUT);
digitalWrite(stepper2_DIR, HIGH);
digitalWrite(stepper2_STEP, 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();
motorStatus(false);
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);
motorStatus(true);
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 motorStatus(bool state)
{
digitalWrite(stepper1_STEP, HIGH);
delayMicroseconds(400);
digitalWrite(stepper1_STEP, LOW);
delayMicroseconds(400);
digitalWrite(stepper2_STEP, HIGH);
delayMicroseconds(400);
digitalWrite(stepper2_STEP, LOW);
delayMicroseconds(400);
Distance = Distance + 1; // record this step
// Check to see if we are at the end of our move
if (Distance == 3600)
{
// We are! Reverse direction (invert DIR signal)
if (digitalRead(stepper1_DIR) == LOW and digitalRead(stepper2_DIR) == HIGH)
{
digitalWrite(stepper1_DIR, HIGH);
digitalWrite(stepper2_DIR, LOW);
}
else
{
digitalWrite(stepper1_DIR, LOW);
digitalWrite(stepper2_DIR, HIGH);
}
// Reset our distance back to zero since we're
// starting a new move
Distance = 0;
// Now pause for half a second
delay(1000);
}
}
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(" ");
}