Skip to content

Instantly share code, notes, and snippets.

@gjb2048
Last active February 7, 2024 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gjb2048/ec1ac8164ce484db4a75933f11485095 to your computer and use it in GitHub Desktop.
Save gjb2048/ec1ac8164ce484db4a75933f11485095 to your computer and use it in GitHub Desktop.
Dad Rail Route Selector test
int s1 = 2; //entry button 1
int s2 = 3; //entry button 2
int e1 = 4; //exit button 1
int e2 = 5; //exit button 2
int r1 = 6; //route set indicator 1
int r2 = 7; //route set indicator 2
int r3 = 8; //route set indicator 3
int r4 = 10; //route set indicator 4
int timeout = 11;//Route time out indicator light
// Note: bool's use false and true not 0 and 1 -> https://www.arduino.cc/reference/en/language/variables/data-types/bool/.
bool e1a = false; // Has Entry point 1 been called?
bool e2a = false; // Has Entry point 2 been called?
unsigned long entrybuttontimeout = 0; //Space to save the entry button start time
unsigned long currentmillis = 0; //stores current time once on each code loop - Not sure on an embedded device if this is more efficient than declaring in a function. See pinToSet.
void setup() {
pinMode(s1, INPUT_PULLUP);
pinMode(s2, INPUT_PULLUP);
pinMode(e1, INPUT_PULLUP);
pinMode(e2, INPUT_PULLUP);
pinMode(r1, OUTPUT);
pinMode(r2, OUTPUT);
pinMode(r3, OUTPUT);
pinMode(r4, OUTPUT);
pinMode(timeout, OUTPUT);
Serial.begin(9600); // Is this for capturing the output of 'printf' on the USB port?
}
// Sets the given route for one second.
void setRoute(byte routeNumber) {
int pinToSet = 0;
switch(routeNumber) {
case 1:
pinToSet = r1;
break;
case 2:
pinToSet = r2;
break;
case 3:
pinToSet = r3;
break;
case 4:
pinToSet = r4;
break;
}
digitalWrite(pinToSet, HIGH);
delay(1000);
digitalWrite(pinToSet, LOW);
}
void loop() {
currentmillis = millis(); //gets the current time
// Code Below Here Sets The Entry Points of the route.
// If not entry point 1 or 2 set.
if (!(e1a || e2a)) { // Neither of the entry points have been called. Replaces inprogress variable.
// No route call in progress.
if (digitalRead(s1) == LOW) {
// start point 1 is pressed, set the route call to in progress and set Entry point 1 to called. Sets The Route Called Time.
e1a = true;
entrybuttontimeout = currentmillis + 5000;
} else if (digitalRead(s2) == LOW) {
// start point 2 is pressed, set the route call to in progress and set Entry point 2 to called. Sets The Route Called Time.
e2a = true;
entrybuttontimeout = currentmillis + 5000;
}
} else {
// In progress.
if (digitalRead(e1) == LOW) {
if (e1a) {
setRoute(1); // Set route 1.
e1a = false;
} else if (e2a) { // Logically must be true! So could be an else! But shown if enhanced with more buttons.
setRoute(2); // Set route 2.
e2a = false;
}
} else if (digitalRead(e2) == LOW) {
if (e1a) {
setRoute(3); // Set route 3.
e1a = false;
} else if (e2a) { // Logically must be true! So could be an else! But shown if enhanced with more buttons.
setRoute(4); // Set route 4.
e2a = false;
}
}
if (entrybuttontimeout <= currentmillis) {
// Time Out Function After 2.5 Seconds. Sets entry point selectors back to false.
digitalWrite(timeout, HIGH);
e1a = false;
e2a = false;
delay(2500);
digitalWrite(timeout,LOW);
}
}
delay(100); // Slow things down a little.
}
@dadrail
Copy link

dadrail commented Feb 6, 2024

int s1 = 2; //entry button 1
int s2 = 3;//entry button 2
int e1 = 4;//exit button 1
int e2 = 5;//exit button 2
int r1 = 6;//route set indicator 1
int r2 = 7;//route set indicator 2
int r3 = 8;//route set indicator 3
int r4 = 10;//route set indicator 4
int timeout = 11;//Route time out indicator light

bool e1a =0;// Has Entry point 1 been called
bool e2a =0;//Has Entry point 2 been called
bool inprogress =0;// Is a Route Call in Progess?
unsigned long entrybuttontimeout;//Space to save the entry button start time
unsigned long currentmillis;//stores current time once on each code loop

void setup() {
pinMode (s1, INPUT_PULLUP);
pinMode (s2, INPUT_PULLUP);
pinMode (e1, INPUT_PULLUP);
pinMode (e2, INPUT_PULLUP);
pinMode (r1, OUTPUT);
pinMode (r2, OUTPUT);
pinMode (r3, OUTPUT);
pinMode (r4, OUTPUT);
pinMode (timeout, OUTPUT);
Serial.begin(9600);
}

void loop() {

currentmillis=millis();//gets the current time

//Code Below Here Sets The Entry Points of the route

if (inprogress ==0 && digitalRead(s1)==LOW){;inprogress=1; e1a=1; entrybuttontimeout=millis()+5000;};//if no route call is currently in progress, and start point 1 is pressed, set the route call to in progress and set Entry point 1 to called. Sets The Route Called Time
if (inprogress ==0 && digitalRead(s2)==LOW){;inprogress=1; e2a=1; entrybuttontimeout=millis()+5000;};//if no route call is currently in progress, and start point 2 is pressed, set the route call to in progress and set Entry point 2 to called. Sets The Route Called Time

//Code Below Here Sets The Exit Points

if (digitalRead(e1)==LOW && e1a==1){digitalWrite(r1,HIGH);delay(1000);digitalWrite(r1,LOW);e1a=0;inprogress=0;};//This will send the output for route 1 for 1 second and reset all the parameters for the next route to be called.
if (digitalRead(e1)==LOW && e2a==1){digitalWrite(r2,HIGH);delay(1000);digitalWrite(r2,LOW);e2a=0;inprogress=0;};//This will send the output for route 2 for 1 second and reset all the parameters for the next route to be called.
if (digitalRead(e2)==LOW && e1a==1){digitalWrite(r3,HIGH);delay(1000);digitalWrite(r3,LOW);e1a=0;inprogress=0;};//This will send the output for route 3 for 1 second and reset all the parameters for the next route to be called.
if (digitalRead(e2)==LOW && e2a==1){digitalWrite(r4,HIGH);delay(1000);digitalWrite(r4,LOW);e2a=0;inprogress=0;};//This will send the output for route 4 for 1 second and reset all the parameters for the next route to be called.

//Code Below Times out if exit points are not selected

if(entrybuttontimeout<=currentmillis && inprogress==1){digitalWrite (timeout, HIGH);e1a=0 ; e2a=0 ; inprogress=0;}//Time Out Function After 5 Seconds. Sets in progress and entry point selectors back to 0
if(digitalRead (timeout)==HIGH){delay(2500);digitalWrite(timeout,LOW);}// If Time out Was Activated Stop Program for 2.5 Seconds and Reset Time Out Light

}

@gjb2048
Copy link
Author

gjb2048 commented Feb 6, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment