johnwasser,
Thanks a bunch for your replies and code, I have been playing with it here and there and i am having an issue with the code not switching. It appears that after the button is pushed, the machineState does not change? Putting it another way, when i monitor the code below in the serial window, it will repeat "Motor Moving Out" whenever the button is pushed and will do nothing when the button is released. I'm still playing with it, but any advise would be greatly appreciated. Thanks!!
/*
Debugging sketch
ALT - 11Sept12
*/
const int CWPin = 12; //defines Clockwise Pin
const int CCWPin = 11; //defines Counter-clockwise Pin
const int LinputPin = 4; //defines the input pin(for Left switch)
const int RinputPin = 3; //defines the input pin(for Right switch)
const int MotorTime = 1500; //defines the time the Motor is on
const int Dwell = 500; //defines the time until motor changes
void setup(){
Serial.begin(9600); //initialize serial communication at 9600 bps
pinMode(CWPin, OUTPUT); //declare CW Pin as output
pinMode(CCWPin, OUTPUT); //declare CCW Pin as output
pinMode(LinputPin, INPUT); //declare Left switch as input
pinMode(RinputPin, INPUT); //declare Right switch as input
}
void loop() {
enum {WaitingForSwitchOn, MotorMovingOut, DwellPeriod, WaitingForSwitchOff, MotorMovingIn} machineState = WaitingForSwitchOn;
switch (machineState){
unsigned long timerStart;
case WaitingForSwitchOn:
if (digitalRead (LinputPin)) {
timerStart = millis();
digitalWrite(CWPin, HIGH);
Serial.println("Motor Moving Out");
machineState = MotorMovingOut;
}
break;
case MotorMovingOut:
if (millis() - timerStart > MotorTime) {
Serial.println("does it get to here?");
digitalWrite(CWPin, LOW);
timerStart = millis();
machineState = DwellPeriod;
}
break;
case DwellPeriod:
if (millis() - timerStart > Dwell) {
machineState = WaitingForSwitchOff;
}
break;
case WaitingForSwitchOff:
if (digitalRead (!LinputPin)) {
timerStart = millis();
digitalWrite(CCWPin, HIGH);
machineState = MotorMovingIn;
}
break;
case MotorMovingIn:
if (millis() - timerStart > MotorTime) {
digitalWrite(CCWPin, LOW);
machineState = WaitingForSwitchOn;
}
break;
}
}