I updated my code with your suggestions and it works....almost.
I am having a problem with the ending. I cannot making go back to the beginning of the loop ( and for it to detect the emergency press button).
Would you have a suggestion?
I tried to have my "END" state go to "IDLE" state, which made my start button being recognized, but then the Emergency is not detected. If I remove the go to "IDLE" state, it spins in the "END" state.
thanks
#include <Railuino.h>
enum State {
IDLE,
STARTING,
GOINGFORWARD,
PAUSING,
GOINGBACK,
END
};
// create a variable holding the current state
State state = IDLE;
const word LOCO = ADDR_MM2 + 78;
const boolean DEBUG = true;
TrackController ctrl(0xdf24, DEBUG);
const int SENSORFORWARDPin = A0;
const int SENSORBACKWARDPin = A1;
const int STARTPin = 12;
const int EmergencyPin = 4;
void setup()
{
Serial.begin(9600);
while (!Serial);
pinMode(10, OUTPUT);
digitalWrite(12, LOW);
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(4, LOW);
ctrl.begin();
Serial.println("Power on");
ctrl.setPower(true);
}
// the loop just does a time slice without delay
void loop() {
// read the button.
if ( digitalRead(EmergencyPin) == HIGH) { /* the button has just been pressed */
if ( digitalRead(SENSORBACKWARDPin) == HIGH) {
Serial.println("emergency stop");
ctrl.setLocoSpeed(LOCO, 0);
delay(500);
digitalWrite(EmergencyPin, LOW);
digitalWrite(SENSORBACKWARDPin, LOW);
state = END;
}
else {
Serial.println("emergency return");
ctrl.setLocoDirection(LOCO, DIR_REVERSE);
ctrl.setLocoSpeed(LOCO, 1000);
delay(800);
// do the emergency stop stuff;
}
}
else {
switch (state) {
case IDLE:
if ( digitalRead(STARTPin) == HIGH) {
Serial.println("idle ");
ctrl.setPower(true);
state = STARTING;
}
break;
case STARTING:
Serial.println("starting ");
delay(1000);
digitalWrite(10, HIGH); // turn the Sound on
delay(1000);
Serial.println("HORN ");
digitalWrite(10, LOW); // deactivate sound
state = GOINGFORWARD;
break;
case GOINGFORWARD:
if ( digitalRead(SENSORFORWARDPin) == HIGH) {
Serial.println("sensor forward detection");
state = PAUSING;
}
else {
Serial.println("forward ");
ctrl.setLocoDirection(LOCO, DIR_FORWARD);
ctrl.setLocoSpeed(LOCO, 1000);
delay(1100);
}
break;
case PAUSING:
digitalWrite(SENSORFORWARDPin, LOW);
ctrl.setLocoSpeed(LOCO, 0);
Serial.println("pause");
delay(2000);
state = GOINGBACK;
break;
case GOINGBACK:
if ( digitalRead(SENSORBACKWARDPin) == HIGH) {
Serial.println("sensor backward detection");
ctrl.setLocoSpeed(LOCO, 0);
delay(2000);
state = END;
}
else {
/* LOCO goes backward */
Serial.println("reverse ");
ctrl.setLocoDirection(LOCO, DIR_REVERSE);
ctrl.setLocoSpeed(LOCO, 1000);
delay(1100);
}
break;
case END:
ctrl.setPower(false);
Serial.println("end ");
break;
}
}
}
just to clarify my use of delay in my code,
without it my train would not be moving ( actually moving in very very small increments, not smoothly at all).
I would rather do away with delay as it does not always detect my PIR sensor at both ends. I might change to an IR sensor, as in the example here
the PIR detects my train ok, just not when it goes too fast within the delay given. if I had a delay(50) it'd be no problem.
it's a model train and I have about a 1cm diameter hole in the tracks to sense any movement.
it cannot be seen by the public.
beninflux:
the PIR detects my train ok, just not when it goes too fast within the delay given. if I had a delay(50) it'd be no problem.
it's a model train and I have about a 1cm diameter hole in the tracks to sense any movement.
it cannot be seen by the public.