Background timer

Hello,
carrying on with the model railway theme,

I Have just purchased a Arduino Motor Shield,With a bit of research it wasn't too hard to get the train going
backwards and forwards with the points switching etc.

The problem is The train is slightly faster going forward than backwards , so timers are not really an option as the train will eventually hit the buffers.

On the test track I have allowed 6 seconds for the train to travel from A to B, it may only take 5

The idea is to put 4 number IR sensors at the end of each siding ,once the train hits the sensor an - if statement - should cut the power to the train for 8 seconds.This would stop the train hitting the buffers.
after 8 seconds the loop would run again

any Help would be much Appreciated
Thanks

// Arduino Motor shield
// small test track



int Sensor = 10;

void setup() {

 //set up chanel A


pinMode(12, OUTPUT); //initiates motor chanel A pin
pinMode(9, OUTPUT); //initiates Brake chanel A pin
pinMode(10, INPUT); //sensor input

}
void loop() {

  //Foreward at full speed
  
 digitalWrite(12, HIGH);    //establishes forward direction of chanel A
 digitalWrite(9, LOW);      //disengage the brake for chanel A
 analogWrite(3, 65);       //spins the motor to set speed from 0 -255

 delay (6000);
 
 digitalWrite(9, HIGH);      //disengage the brake for chanel A
 
 delay(1000);

// Backward at half speed

  digitalWrite(12, LOW);    //establishes backward direction of chanel A
  digitalWrite(9, LOW);      //engages the brake for chanel A
  analogWrite(3, 65);       //spins the motor to set speed
  
  delay (6000);
  
  digitalWrite(9, HIGH);      //disengage the brake for chanel A

delay (1000);

 // This part should cut power to the train for 8 seconds
 //then carry on.

 // if (Sensor == HIGH);
 // analogWrite(3, 0);           // sets the speed to zero
 //digitalWrite(9, LOW);      //engages the brake for chanel A
 //delay (8000);              //pause for 8 seconds
   
  }

IR sensors are good for detecting.... IR (heat for exemple). Does your train have enough heat to be detected ? Also it's not very precise, the angle of detection is pretty wide usually

Typically one would go for end course switch detection for example to check if something is approaching a dangerous area

PS: in your commented out codeif (Sensor == HIGH);does nothing at all because of the semi-colon... and the fact that you defined int Sensor = 10;so it won't be HIGH (1) anyway...

Thanks for your quick reply

The collision detectors actually work quite well as the underside of the train is painted white.

any other pointers why this is not working

2020laurence:
any other pointers why this is not working

what's not working? the part you commented out?

  // This part should cut power to the train for 8 seconds
  //then carry on.

  // if (Sensor == HIGH);
  // analogWrite(3, 0);           // sets the speed to zero
  //digitalWrite(9, LOW);      //engages the brake for chanel A
  //delay (8000);              //pause for 8 seconds

I made a couple comments above for that...

Correct
The train goes back and fourth with this commented out

once re instated

The train stays still

Edit - please describe completely, the sequence of actions that the train is supposed to follow. It's not obvious from the code or your description.

That was the general idea of what I wanted to do
but to tell the truth ,
Im not the best at coding ( getting on a bit ) and lost the will to live after searching google for a day
All the examples seem to use timers of some description

remember the code is executed sequentially. So if you were to write the test correctly but only execute it at the end of the loop, after all the delays, I'm not sure it will be of much help...

seems you need to go back to basics and get acquainted with the environment, the language, using millis() etc.. there are plenty of tutorials to choose from

Sorry, that won't do. You can't just copy features because someone used it. They likely had other reasons for doing it, than you would.

Try fixing this error:

 // if (Sensor == HIGH);

The semicolon makes this statement mean, "do absolutely nothing". You have to follow it with curly braces {} around the code that the if applies to.

A problem that you need to think about: you can't check the sensor during the time that any delay() is running. That could send the train off the end of the track.

For testing, why not make a sketch that only starts the train towards the end, and stops safely. When that works perfectly, add all the other stuff. One feature at a time.

Sorry about that.

siding 1 siding 2

-- train. -------- --------------------
x sensor 1 - - sensor 2. x


x sensor 3 sensor 4. x

main 1 main 2

sequence of events
train starts off in siding 1
train then moves to main 2 and stops when it hits sensor 4. wait 8 seconds
train moves from main 2 to main 1and stops when it hits sensor 3 wait 8 seconds
train moves from main 1 to siding 2 and stops when it hits sensor 2

loop starts again

Thanks AARG

Thought it might come to that

Now take that sequence and follow it exactly, but in code not English. :slight_smile:
I think it would initially look like:

repeat
{
if sensor1 and dir = west
{
throttle = 0
wait 8
dir = east
throttle = on
}

if sensor4 and dir = east
{
throttle = 0
wait 8
dir = west
throttle = on
}
}

That would make it shuttle back and forth between siding 1 and siding 4.

I will give it a go
Thanks for your efforts

Rewire the Shard easy as Pie

make a train stop at a sensor ( impossible )

2020laurence:
make a train stop at a sensor ( impossible )

I seriously doubt that. :slight_smile:

You will probably need a variable to remember the direction... it's not enough to just set the motor that way.

I Actually do that for a living
but the art of coding is somewhat quite different.
Not Giving up though.