//IR Single Track_Class.ino APPLICATION FILE/*************************************************************************************************** * The following code will implement either a controller for a single section of single track * or a controller for two (dual) sections of single track. * For a dual controller all the code should be present. For a single track not all code is required. * It is reccomended that the code be left intact. The timing and memory overheads are minimal. * However each track uses 4 Arduino pins at each end so there are 8 pins available for other * applications if the redundant code is omitted. * The critical sections of code are 1. creating an instance of IR_Single_Track_Class. * For example:"IR_Single_Track_Class Entry1 (A3,4,3,2);" creates instance Entry1 (to single track) * that has the sensor wired to pin A3, the RED signal to pin 4, AMBER to 3 and GREEN to pin 2. * and 2. in the loop( ) method "track_status1 = one_track(Entry1,Entry4,track_status1);" where * in this example Entry1 is one end of the single track and Entry4 the other. It is this * statement that defines the architecture of the train layout.**************************************************************************************************/#include "IR_Single_Track_Class.h"
//Wiring is sensor, red signal, amber signal and green signal. Change as requiredIR_Single_Track_Class Entry1 (A3,4,3,2);IR_Single_Track_Class Entry2 (A2,7,6,5);IR_Single_Track_Class Entry3 (A1,10,9,8);IR_Single_Track_Class Entry4 (A0,13,12,11);
//single track will be vacant or train entering from one end or the otherenum track_activity {vacant,train1,train2} track_status1,track_status2;
#define POLARITY 0 //0 = negative logic signals
void setup() { Entry1.begin(POLARITY); //set up light pins as outputs Entry2.begin(POLARITY); Entry3.begin(POLARITY); Entry4.begin(POLARITY); //Set signals to RED. Main code will set to GREEN via RED-AMBER_GREEN sequence Entry1.signal_control(1); //both signals RED - Entry2.signal_control(1); //both signals RED - Entry3.signal_control(1); //both signals RED - Entry4.signal_control(1); //main code will go AMBER-GREEN track_status1= vacant; track_status2= vacant;}
//one_track determines which train enters section of single track first.//same code (different arguements) used by code for 2 single tracks.//when train enters lights at other end go RED.//lights at entry follow sequence given by my_track(sen1,sen2) track_activity one_track(IR_Single_Track_Class &test_pt1, IR_Single_Track_Class &test_pt2, track_activity track_status){ int sen1 = test_pt1.train_over_sensor(); int sen2 = test_pt2.train_over_sensor(); switch (track_status){ case vacant : test_pt1.signal_control(0); //not red test_pt2.signal_control(0); if (sen1) track_status = train1; else if(sen2) track_status = train2; break; case train1 : test_pt2.signal_control(1); //force red at exit if (test_pt1.my_track(sen1,sen2)) track_status = vacant; break; case train2 : test_pt1.signal_control(1); //force red if (test_pt2.my_track(sen2,sen1)) track_status = vacant; break; } return track_status; }
void loop() {
//code continually loops keeping track of status //First two arguments give entry points to single track. //These should be modified to match the layout track_status1 = one_track(Entry1,Entry2,track_status1); track_status2 = one_track(Entry3,Entry4,track_status2); } ----------------------------------------------------
//IR_Single_Track_Class.cpp IMPLEMENTATION FILE#include "IR_SINGLE_Track_Class.h"
long reset_time = 300000L; //design resets after 5 minutes
IR_Single_Track_Class:: IR_Single_Track_Class (int sensor,int red,int amber,int green) : IR_Sensor_Class(sensor), Signals3_Class(red,amber,green){}
void IR_Single_Track_Class :: begin ( int polarity){ Signals3_Class::begin(polarity);}
int IR_Single_Track_Class::my_track(int entry_sensor, int exit_sensor){ switch (train_location) { case waiting : train_location = at_entry; return 0; case at_entry: if (!entry_sensor) { train_location = in_transit; _restart_time=millis(); }return 0; case in_transit: signal_control(1); if (millis( ) > (_restart_time + reset_time )) {train_location = waiting; return 1;} if (exit_sensor) train_location = at_exit; return 0; case at_exit: if (exit_sensor) return 0; else {train_location = waiting; return 1; } }};-----------------------------------------
//IR_Single_Track_Class.h HEADER FILE#ifndef IR_SINGLE_TRACK_CLASS_H#define IR_SINGLE_TRACK_CLASS_H#include "Arduino.h"#include <Signals3_Class.h>#include <IR_Sensor_Class.h>
class IR_Single_Track_Class : public IR_Sensor_Class, public Signals3_Class { public: IR_Single_Track_Class(int sensor,int red,int amber,int green); void begin(int polarity); int my_track(int my_sensor, int your_sensor); private: long _restart_time; enum train_status {waiting, at_entry, in_transit, at_exit} train_location; };#endif