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...
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
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.
-- 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
Now take that sequence and follow it exactly, but in code not English.
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.