Old thread here(videos of V1.1 on page 2):
http://arduino.cc/forum/index.php/topic,8491.0.html
The current version of this project is V1.2 which is a revamped cleaner code of V1.1. Both versions only allow for 1 train to go around the track in either manual or automatic mode.
Recently I decided to start coding a new version which allows for multiple train on the track. 2 Would be enough but the way I want to code is so it doesn’t matter how many trains are on the track as long as there are enough block sections to accommodate for the trains.
The way I want to set up this whole project is to make each section (block) work standalone. The old version used a pre-programmed ride cycle that would just start reading the next sensor when the previous one was touched. Very basic. The new version cycles through the block sections and checks to see if it meets the 2 or 3 requirements for it to let the train through. If it does the train will head for the next section. Otherwise it will be stopped dead in it’s tracks (lol pun) to prevent collisions.
Here is the code up until now which is far from finished and close to working at all:
#include <Servo.h>
// Servo Motors
Servo gatesServo1;
Servo gatesServo2;
//Pins
const int modeSwitch = 22; //Switch to select Auto/Manual mode
const int gateSwitch = 26; //Switch to open/close the station gates.
const int dispatchButton = 28; //Button to dispatch the train
const int trainSwitch = 30; //Amount of trains
const int ledG = 34; //Green LED, blink = ready to dispatch
const int ledR = 36; //Indicates the status from the station gates (Open/Closed)
const int motorSet1 = 42; //Motors 1 and 2
const int motorSet2 = 44; //Motors 3 and 4
const int motorSet3 = 46; //Motors 5 and 6
const int sensorLift = 49; //Sensor on the lift
const int sensorBrakes = 51; //Sensor on the brakes
const int sensorStation = 53; //Sensor in the station
// Booleans
stationState = true;
brakeState = false;
liftState = false;
//Variables
//Others
void setup() {
Serial.begin(9600);
//Inputs
pinMode(modeSwitch, INPUT);
pinMode(gateSwitch, INPUT);
pinMode(dispatchButton, INPUT);
pinMode(sensorLift, INPUT);
pinMode(sensorBrakes, INPUT);
pinMode(sensorStation, INPUT);
//Outputs
pinMode(ledG, OUTPUT);
pinMode(ledR, OUTPUT);
pinMode(motorSet1, OUTPUT);
pinMode(motorSet2, OUTPUT);
pinMode(motorSet3, OUTPUT);
gatesServo1.attach(2);
gatesServo2.attach(3);
//Initialize
}
void loop() {
//Manual 1 train
if(modeSwitch == HIGH && trainSwitch == HIGH) {
//Gates
if(gateSwitch == LOW && stationState == true) {
gateServo1.write(1);
gateServo2.write(1);
}
else {
gateServo1.write(90);
gateServo2.write(90);
}
if(gateSwitch == HIGH && stationState == true && dispatchButton == LOW) {
Serial.println("DISPATCHED");
//Motors Station On
liftState = HIGH;
}
//The Lift
if(liftState == true && brakeState == false){
if(sensorLift == HIGH){
digitalWrite(motorSet2, HIGH);
}
else {
//wacht tot top gepasseerd (delay vervanger!!)
digitalWrite(motorSet2, LOW);
liftState = false;
}
}
else {
digitalWrite(motorSet2, LOW);
}
//The Brakes
if(brakeState == true && stationState && false){
}
//Manual 2 trains
if(modeSwitch == HIGH && trainSwitch == LOW) {
}
//Automatic 1 train
if(modeSwitch == LOW && trainSwitch == HIGH) {
}
//Automatic 2 trains
if(modeSwitch == LOW && trainSwitch == LOW) {
}
}
THIS CODE IS FAR FROM FINISHED ADN HAS A LOT OF BUGS AND MESS-UPS. (Ugh, I haven’t coded in a while, takes some time to get back into it.)
The only thing that bugs me right now is that I don’t know how to initialize the coaster when it first boots up. I’m thinking about adding more sensors but that will be very expensive. When the coaster boots up there will be a train in the station and maybe (depending on the amount of trains which is interchangeable) on the brake section. I need it to somehow read the brake section on the first boot to determine the amount of trains on the track. Any help on how to do this is appreciated
The current block sections are: Station, Lift, Brakes. Currently I have 3 sensors but I have a spare 4th laying around which can be added if necessary.