Components and supplies
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
BLOCK DETECTION 3 ASPECT LIGHT CONTROL
arduino
This is my BLOCK DETECTION 3 ASPECT LIGHT CONTROL program. It detecets the train entering and leaving a block of track and changes the lights accordingly.
1// initilize the Digital input pins 2int sensePin1 = 7; 3int sensePin2 = 4; 4//Initilize the OUTPUT pins for the RED,GREEN and YELLOW lights. 5int ledPinGRN = 10; 6int ledPinYEL = 11; 7int ledPinRED = 12; 8 9 10 11void setup() { 12 analogReference(DEFAULT); //isn't necessary 13 Serial.begin(9600); 14 pinMode(ledPinGRN, OUTPUT);//Set pin MODES for lights 15 pinMode(ledPinYEL, OUTPUT);//Set pin MODES for lights 16 pinMode(ledPinRED, OUTPUT);//Set pin MODES for lights 17 18} 19 20enum LIGHTSTATES 21{ 22 ST_GREEN, // light green 23 ST_YELLOWFLASH, // light yellow flash 24 ST_YELLOW, // yellow on for given time 25 ST_RED, // light red 26 ST_RED1, // light red 27}; 28 29// state of statemachine for block 1; initial value green 30LIGHTSTATES block1State = ST_GREEN; 31 32// current time 33static unsigned long currentTime; 34 35void loop() 36{ 37 // get current time 38 currentTime = millis(); 39 40 // read sensors 41 42 int valA1 = digitalRead(sensePin1); 43 int valA2 = digitalRead(sensePin2); 44 45 46 //Serial.print('valA1'); 47 //Serial.println(""); 48 //Serial.print('valA2'); 49 //Serial.println(""); 50 //Serial.println(""); 51 //delay(1500); 52 switch (block1State) 53 { 54 // show green light 55 // we will switch to red if sensors are triggered 56 case ST_GREEN: 57 block1Green(valA1, valA2); 58 break; 59 case ST_YELLOWFLASH: 60 block1YellowFlash(valA1, valA2); 61 break; 62 case ST_YELLOW: 63 block1Yellow(valA1, valA2); 64 break; 65 case ST_RED: 66 block1Red(valA1, valA2); 67 break; 68 case ST_RED1: 69 block1Red1(valA1, valA2); 70 break; 71 } 72} 73 74/* 75 switch green light on for block 1 76 'wait' for sensors to trigger and switches to red 77 input: values of sensors A1 and A2 78*/ 79void block1Green(int valA1, int valA2) 80{ 81 digitalWrite(ledPinGRN, LOW); 82 digitalWrite(ledPinYEL, HIGH); 83 digitalWrite(ledPinRED, HIGH); 84 85 if (valA1 == LOW && valA2 == HIGH) 86 { 87 // change state to red 88 block1State = ST_RED;} 89 else if (valA1 == HIGH && valA2 == LOW) 90 { 91 // change state to red 92 block1State = ST_RED1; 93 } 94 } 95 96 97/* 98 flash yellow led 10 times for block 1 99 after that, switch to steady yellow 100 input: values of sensors A1 and A2; both are not used but added for consistency in the calls to the light functions 101*/ 102void block1YellowFlash(int valA1, int valA2) 103{ 104 // on and off duration 105 const unsigned long duration = 500; 106 // number of flashes 107 const byte numFlashes = 10; 108 109 // remember last time that we changed from 'yellow on' to 'yellow off' or vice versa 110 static unsigned long startTime = 0; 111 // keep a counter for the number of flashes 112 static byte counter = 0; 113 114 // if called after coming from red 115 if (startTime == 0) 116 { 117 // switch yellow on 118 digitalWrite(ledPinGRN, HIGH); 119 digitalWrite(ledPinYEL, LOW); 120 digitalWrite(ledPinRED, HIGH); 121 122 // start timing 123 startTime = currentTime; 124 125 // nothing else to do 126 return; 127 } 128 129 // if it's time to toggle the yellow led 130 // based on blink without delay 131 if (currentTime - startTime >= duration) 132 { 133 // set start time 134 startTime += duration; 135 136 // toggle the yellow led 137 digitalWrite(ledPinYEL, !digitalRead(ledPinYEL)); 138 139 // increment the counter 140 counter++; 141 } 142 143 if (counter > numFlashes * 2) 144 { 145 // reset variables 146 // as a result, the next time this function is called, the sequence is started from scratch 147 startTime = 0; 148 counter = 0; 149 150 // change state to steady yellow 151 block1State = ST_YELLOW; 152 } 153} 154 155/* 156 switch yellow light on for N seconds for block 1 157 after that, switch to green 158 input: values of sensors A1 and A2; both are not used but added for consistency in the calls to the light functions 159*/ 160void block1Yellow(int valA1, int valA2) 161{ 162 // duration for yellow on 163 const unsigned long duration = 2000; 164 // start time of delay 165 static unsigned long startTime = 0; 166 167 if (startTime == 0) 168 { 169 // switch yellow on 170 digitalWrite(ledPinGRN, HIGH); 171 digitalWrite(ledPinYEL, LOW); 172 digitalWrite(ledPinRED, HIGH); 173 174 // set start time of delay 175 startTime = currentTime; 176 177 // nothing else to do 178 return; 179 } 180 181 // if N milliseconds passed 182 if (currentTime - startTime >= duration) 183 { 184 // reset variables so next call to this sequence will start sequence from scratch 185 startTime = 0; 186 // change state to green 187 block1State = ST_GREEN; 188 } 189} 190 191/* 192 switch red light on for block1 193 'wait' for sensors to trigger and switches to yellow flash 194 input: values of sensors A1 and A2 195*/ 196void block1Red(int valA1, int valA2) 197{ 198 digitalWrite(ledPinGRN, HIGH); 199 digitalWrite(ledPinYEL, HIGH); 200 digitalWrite(ledPinRED, LOW); 201 if (valA1 == HIGH && valA2 == LOW) 202 { 203 // change state to yellow flash 204 block1State = ST_YELLOWFLASH; 205 } 206} 207void block1Red1(int valA1, int valA2) 208{ 209 digitalWrite(ledPinGRN, HIGH); 210 digitalWrite(ledPinYEL, HIGH); 211 digitalWrite(ledPinRED, LOW); 212 if (valA1 == LOW && valA2 == HIGH) 213 { 214 // change state to yellow flash 215 block1State = ST_YELLOWFLASH; 216 } 217 218} 219 220
IR Block Detector
arduino
Block detection for model trains.
1// initilize the Digital input pins 2int sensePin1 = 7; 3int sensePin2 = 4; 4//Initilize the OUTPUT pins for the RED,GREEN and YELLOW lights. 5int ledPinGRN = 10; 6int ledPinYEL = 11; 7int ledPinRED = 12; 8 9 10 11void setup() { 12 analogReference(DEFAULT); //isn't necessary 13 Serial.begin(9600); 14 pinMode(ledPinGRN, OUTPUT);//Set pin MODES for lights 15 pinMode(ledPinYEL, OUTPUT);//Set pin MODES for lights 16 pinMode(ledPinRED, OUTPUT);//Set pin MODES for lights 17 18} 19 20enum LIGHTSTATES 21{ 22 ST_GREEN, // light green 23 ST_YELLOWFLASH, // light yellow flash 24 ST_YELLOW, // yellow on for given time 25 ST_RED, // light red 26 ST_RED1, // light red 27}; 28 29// state of statemachine for block 1; initial value green 30LIGHTSTATES block1State = ST_GREEN; 31 32// current time 33static unsigned long currentTime; 34 35void loop() 36{ 37 // get current time 38 currentTime = millis(); 39 40 // read sensors 41 42 int valA1 = digitalRead(sensePin1); 43 int valA2 = digitalRead(sensePin2); 44 45 46 //Serial.print('valA1'); 47 //Serial.println(""); 48 //Serial.print('valA2'); 49 //Serial.println(""); 50 //Serial.println(""); 51 //delay(1500); 52 switch (block1State) 53 { 54 // show green light 55 // we will switch to red if sensors are triggered 56 case ST_GREEN: 57 block1Green(valA1, valA2); 58 break; 59 case ST_YELLOWFLASH: 60 block1YellowFlash(valA1, valA2); 61 break; 62 case ST_YELLOW: 63 block1Yellow(valA1, valA2); 64 break; 65 case ST_RED: 66 block1Red(valA1, valA2); 67 break; 68 case ST_RED1: 69 block1Red1(valA1, valA2); 70 break; 71 } 72} 73 74/* 75 switch green light on for block 1 76 'wait' for sensors to trigger and switches to red 77 input: values of sensors A1 and A2 78*/ 79void block1Green(int valA1, int valA2) 80{ 81 digitalWrite(ledPinGRN, LOW); 82 digitalWrite(ledPinYEL, HIGH); 83 digitalWrite(ledPinRED, HIGH); 84 85 if (valA1 == LOW && valA2 == HIGH) 86 { 87 // change state to red 88 block1State = ST_RED;} 89 else if (valA1 == HIGH && valA2 == LOW) 90 { 91 // change state to red 92 block1State = ST_RED1; 93 } 94 } 95 96 97/* 98 flash yellow led 10 times for block 1 99 after that, switch to steady yellow 100 input: values of sensors A1 and A2; both are not used but added for consistency in the calls to the light functions 101*/ 102void block1YellowFlash(int valA1, int valA2) 103{ 104 // on and off duration 105 const unsigned long duration = 500; 106 // number of flashes 107 const byte numFlashes = 10; 108 109 // remember last time that we changed from 'yellow on' to 'yellow off' or vice versa 110 static unsigned long startTime = 0; 111 // keep a counter for the number of flashes 112 static byte counter = 0; 113 114 // if called after coming from red 115 if (startTime == 0) 116 { 117 // switch yellow on 118 digitalWrite(ledPinGRN, HIGH); 119 digitalWrite(ledPinYEL, LOW); 120 digitalWrite(ledPinRED, HIGH); 121 122 // start timing 123 startTime = currentTime; 124 125 // nothing else to do 126 return; 127 } 128 129 // if it's time to toggle the yellow led 130 // based on blink without delay 131 if (currentTime - startTime >= duration) 132 { 133 // set start time 134 startTime += duration; 135 136 // toggle the yellow led 137 digitalWrite(ledPinYEL, !digitalRead(ledPinYEL)); 138 139 // increment the counter 140 counter++; 141 } 142 143 if (counter > numFlashes * 2) 144 { 145 // reset variables 146 // as a result, the next time this function is called, the sequence is started from scratch 147 startTime = 0; 148 counter = 0; 149 150 // change state to steady yellow 151 block1State = ST_YELLOW; 152 } 153} 154 155/* 156 switch yellow light on for N seconds for block 1 157 after that, switch to green 158 input: values of sensors A1 and A2; both are not used but added for consistency in the calls to the light functions 159*/ 160void block1Yellow(int valA1, int valA2) 161{ 162 // duration for yellow on 163 const unsigned long duration = 2000; 164 // start time of delay 165 static unsigned long startTime = 0; 166 167 if (startTime == 0) 168 { 169 // switch yellow on 170 digitalWrite(ledPinGRN, HIGH); 171 digitalWrite(ledPinYEL, LOW); 172 digitalWrite(ledPinRED, HIGH); 173 174 // set start time of delay 175 startTime = currentTime; 176 177 // nothing else to do 178 return; 179 } 180 181 // if N milliseconds passed 182 if (currentTime - startTime >= duration) 183 { 184 // reset variables so next call to this sequence will start sequence from scratch 185 startTime = 0; 186 // change state to green 187 block1State = ST_GREEN; 188 } 189} 190 191/* 192 switch red light on for block1 193 'wait' for sensors to trigger and switches to yellow flash 194 input: values of sensors A1 and A2 195*/ 196void block1Red(int valA1, int valA2) 197{ 198 digitalWrite(ledPinGRN, HIGH); 199 digitalWrite(ledPinYEL, HIGH); 200 digitalWrite(ledPinRED, LOW); 201 if (valA1 == HIGH && valA2 == LOW) 202 { 203 // change state to yellow flash 204 block1State = ST_YELLOWFLASH; 205 } 206} 207void block1Red1(int valA1, int valA2) 208{ 209 digitalWrite(ledPinGRN, HIGH); 210 digitalWrite(ledPinYEL, HIGH); 211 digitalWrite(ledPinRED, LOW); 212 if (valA1 == LOW && valA2 == HIGH) 213 { 214 // change state to yellow flash 215 block1State = ST_YELLOWFLASH; 216 } 217 218} 219 220 221 222 223
BLOCK DETECTION 3 ASPECT LIGHT CONTROL
arduino
This is my BLOCK DETECTION 3 ASPECT LIGHT CONTROL program. It detecets the train entering and leaving a block of track and changes the lights accordingly.
1// initilize the Digital input pins 2int sensePin1 = 7; 3int sensePin2 4 = 4; 5//Initilize the OUTPUT pins for the RED,GREEN and YELLOW lights. 6int 7 ledPinGRN = 10; 8int ledPinYEL = 11; 9int ledPinRED = 12; 10 11 12 13void 14 setup() { 15 analogReference(DEFAULT); //isn't necessary 16 Serial.begin(9600); 17 18 pinMode(ledPinGRN, OUTPUT);//Set pin MODES for lights 19 pinMode(ledPinYEL, 20 OUTPUT);//Set pin MODES for lights 21 pinMode(ledPinRED, OUTPUT);//Set pin MODES 22 for lights 23 24} 25 26enum LIGHTSTATES 27{ 28 ST_GREEN, // light green 29 30 ST_YELLOWFLASH, // light yellow flash 31 ST_YELLOW, // yellow on for given 32 time 33 ST_RED, // light red 34 ST_RED1, // light red 35}; 36 37// 38 state of statemachine for block 1; initial value green 39LIGHTSTATES block1State 40 = ST_GREEN; 41 42// current time 43static unsigned long currentTime; 44 45void 46 loop() 47{ 48 // get current time 49 currentTime = millis(); 50 51 // read 52 sensors 53 54 int valA1 = digitalRead(sensePin1); 55 int valA2 = digitalRead(sensePin2); 56 57 58 59 //Serial.print('valA1'); 60 //Serial.println(""); 61 //Serial.print('valA2'); 62 63 //Serial.println(""); 64 //Serial.println(""); 65 //delay(1500); 66 67 switch (block1State) 68 { 69 // show green light 70 // we will switch 71 to red if sensors are triggered 72 case ST_GREEN: 73 block1Green(valA1, 74 valA2); 75 break; 76 case ST_YELLOWFLASH: 77 block1YellowFlash(valA1, 78 valA2); 79 break; 80 case ST_YELLOW: 81 block1Yellow(valA1, valA2); 82 83 break; 84 case ST_RED: 85 block1Red(valA1, valA2); 86 break; 87 88 case ST_RED1: 89 block1Red1(valA1, valA2); 90 break; 91 } 92} 93 94/* 95 96 switch green light on for block 1 97 'wait' for sensors to trigger and switches 98 to red 99 input: values of sensors A1 and A2 100*/ 101void block1Green(int valA1, 102 int valA2) 103{ 104 digitalWrite(ledPinGRN, LOW); 105 digitalWrite(ledPinYEL, 106 HIGH); 107 digitalWrite(ledPinRED, HIGH); 108 109 if (valA1 == LOW && valA2 == 110 HIGH) 111 { 112 // change state to red 113 block1State = ST_RED;} 114 else 115 if (valA1 == HIGH && valA2 == LOW) 116 { 117 // change state to red 118 block1State 119 = ST_RED1; 120 } 121 } 122 123 124/* 125 flash yellow led 10 times for block 126 1 127 after that, switch to steady yellow 128 input: values of sensors A1 and 129 A2; both are not used but added for consistency in the calls to the light functions 130*/ 131void 132 block1YellowFlash(int valA1, int valA2) 133{ 134 // on and off duration 135 const 136 unsigned long duration = 500; 137 // number of flashes 138 const byte numFlashes 139 = 10; 140 141 // remember last time that we changed from 'yellow on' to 'yellow 142 off' or vice versa 143 static unsigned long startTime = 0; 144 // keep a counter 145 for the number of flashes 146 static byte counter = 0; 147 148 // if called after 149 coming from red 150 if (startTime == 0) 151 { 152 // switch yellow on 153 digitalWrite(ledPinGRN, 154 HIGH); 155 digitalWrite(ledPinYEL, LOW); 156 digitalWrite(ledPinRED, HIGH); 157 158 159 // start timing 160 startTime = currentTime; 161 162 // nothing else to 163 do 164 return; 165 } 166 167 // if it's time to toggle the yellow led 168 // 169 based on blink without delay 170 if (currentTime - startTime >= duration) 171 { 172 173 // set start time 174 startTime += duration; 175 176 // toggle the yellow 177 led 178 digitalWrite(ledPinYEL, !digitalRead(ledPinYEL)); 179 180 // increment 181 the counter 182 counter++; 183 } 184 185 if (counter > numFlashes * 2) 186 187 { 188 // reset variables 189 // as a result, the next time this function 190 is called, the sequence is started from scratch 191 startTime = 0; 192 counter 193 = 0; 194 195 // change state to steady yellow 196 block1State = ST_YELLOW; 197 198 } 199} 200 201/* 202 switch yellow light on for N seconds for block 1 203 after 204 that, switch to green 205 input: values of sensors A1 and A2; both are not used 206 but added for consistency in the calls to the light functions 207*/ 208void block1Yellow(int 209 valA1, int valA2) 210{ 211 // duration for yellow on 212 const unsigned long duration 213 = 2000; 214 // start time of delay 215 static unsigned long startTime = 0; 216 217 218 if (startTime == 0) 219 { 220 // switch yellow on 221 digitalWrite(ledPinGRN, 222 HIGH); 223 digitalWrite(ledPinYEL, LOW); 224 digitalWrite(ledPinRED, HIGH); 225 226 227 // set start time of delay 228 startTime = currentTime; 229 230 // nothing 231 else to do 232 return; 233 } 234 235 // if N milliseconds passed 236 if (currentTime 237 - startTime >= duration) 238 { 239 // reset variables so next call to this sequence 240 will start sequence from scratch 241 startTime = 0; 242 // change state to 243 green 244 block1State = ST_GREEN; 245 } 246} 247 248/* 249 switch red light 250 on for block1 251 'wait' for sensors to trigger and switches to yellow flash 252 253 input: values of sensors A1 and A2 254*/ 255void block1Red(int valA1, int valA2) 256{ 257 258 digitalWrite(ledPinGRN, HIGH); 259 digitalWrite(ledPinYEL, HIGH); 260 digitalWrite(ledPinRED, 261 LOW); 262 if (valA1 == HIGH && valA2 == LOW) 263 { 264 // change state to yellow 265 flash 266 block1State = ST_YELLOWFLASH; 267 } 268} 269void block1Red1(int valA1, 270 int valA2) 271{ 272 digitalWrite(ledPinGRN, HIGH); 273 digitalWrite(ledPinYEL, 274 HIGH); 275 digitalWrite(ledPinRED, LOW); 276 if (valA1 == LOW && valA2 == HIGH) 277 278 { 279 // change state to yellow flash 280 block1State = ST_YELLOWFLASH; 281 282 } 283 284} 285 286
Comments
Only logged in users can leave comments
lecurtis
0 Followers
•0 Projects
Table of contents
Intro
18
0