/* * Arduino program to control a model train running a point to point layout with the help of feedback sensors. * * Made by Tech Build: https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g?sub_confirmation=1 * * Feel free to tinker with the code and customize it for your own layout. :) */ int s; int ta = 2;//Ineger variable to store time delay of the train between crossing the 'sensored' track and stopping at point A. int tb = 2;//Ineger variable to store time delay of the train between crossing the 'sensored' track and stopping at point B. int sa = 5;//Integer variable to store how long will the train stop at station A(in seconds). int sb = 5;//Integer variable to store how long will the train stop at station B(in seconds). int MidSpeed = 70;//Integer variable to store the train's initial speed(when leaving or arriving at the station). int MaxSpeed = 110;//Integer variable to store the train's maximum speed(after leaving the station). void motor_go(){ if(s>=1&&s<=255){ digitalWrite(9,LOW); digitalWrite(8,HIGH); analogWrite(10,s); } if(s<=-1&&s>=-255){ digitalWrite(8,LOW); digitalWrite(9,HIGH); analogWrite(10,-s); } if(s==0){ digitalWrite(9,LOW); digitalWrite(8,LOW); analogWrite(10,s); } } void setup() { // put your setup code here, to run once: pinMode(10,OUTPUT); pinMode(9,OUTPUT); pinMode(8,OUTPUT); pinMode(A0, INPUT); pinMode(A1, INPUT); } void loop() { /* * The train will start from point A to proceed towards point B. */ for(s=0;s<=30;s++){//Applying low-voltage across the track rails to make the train's lights turn on(This might not work for all trains!). motor_go(); delay(60); } delay(1000);//Waiting for a second. for(s=s;s<=MidSpeed;s++){//Starting the train from the station at point A. motor_go(); delay(125); } while(digitalRead(A0)!=HIGH);//The train will keep moving at the set 'MidSpeed' until crossing the 'sensored' track. for(s=s;s<=MaxSpeed;s++){//Increasing the speed of the train. motor_go(); delay(125); } while(digitalRead(A1)!=HIGH);//The train will keep moving at the set 'MaxSpeed' until crossing the next 'sensored' track. for(s=s;s!=MidSpeed;s--){//Reducing the speed of the train. motor_go(); delay(125); } delay(tb*1000);//Wait for set amount of time(in seconds) before stopping the train at the station at point B. for(s=s;s!=30;s--){//Bring it to a halt, keeping the lights turned on(This might not work for all trains!). motor_go(); delay(125); } delay(1000);//Wait for a second. for(s=s;s!=0;s--){ motor_go(); delay(60); } delay(5000);//Wait for set amount of time at the station before startinf again. /* * The train will now start to go back to point A the same way it went from ppint A to B. */ for(s=s;s!=-30;s--){ motor_go(); delay(60); } delay(1000); for(s=s;s!=-MidSpeed;s--){ motor_go(); delay(125); } while(digitalRead(A1)!=HIGH); for(s=s;s!=-MaxSpeed;s--){ motor_go(); delay(125); } while(digitalRead(A0)!=HIGH); for(s=s;s<=-MidSpeed;s++){ motor_go(); delay(125); } delay(ta*1000); for(s=s;s<=-30;s++){ motor_go(); delay(125); } delay(1000); for(s=s;s<=0;s++){ motor_go(); delay(60); } delay(5000); }