Here's a rough prototype of some code that should make a start.
#define SECTION_TIME 1000 // # of mS to travel a section
#define FOWARDS 1
#define BACKWARDS 0
volatile boolean stop;
void startMotors (int direction) {}; // you write these
void stopMotors () {};
void move (int sections, int direction) {
long time_to_travel = sections * SECTION_TIME;
long start_time = millis();
long stop_time = start_time + time_to_travel;
startMotors (direction);
while (millis() < stop_time && !stop) {}
stopMotors();
stop = false;
}
void fontSensor_ISR() {
stop = true;
}
void backSensor_ISR() {
stop = true;
}
void setup () {
attachInterrupt (0, fontSensor_ISR, RISING);
attachInterrupt (1, backSensor_ISR, RISING);
}
void loop () {
move (1, FOWARDS); // move to A
move (1, BACKWARDS);
move (2, FOWARDS); // move to B
move (2, BACKWARDS);
move (3, FOWARDS); // move to C
move (3, BACKWARDS);
move (4, FOWARDS); // move to D
move (4, BACKWARDS);
move (6, FOWARDS); // move to E (stopped by sensor)
move (1, BACKWARDS); // move to D
move (1, FOWARDS);
move (2, BACKWARDS); // move to C
move (2, FOWARDS);
move (3, BACKWARDS); // move to B
move (3, FOWARDS);
move (6, BACKWARDS); // move to A (stopped by sensor)
delay (1000);
}
It assumes that you get an interrupt from the sensors if you get too close, but now that I think about it that probably is not the case so that should be changed.
Can you use that as a start and build it from there?
I could not get onto the forum most of yesterday so didn't see the new requirements.
______
Rob