Components and supplies
Arduino Mega 2560
LED (generic)
3 lead 2 colour LED
Tools and machines
Breadboard, 400 Pin
Project description
Code
new_traffic_light.ino
arduino
1/* Project to demonstrate use of multiple interrupts in a real world situation. 2 Traffic light control with pedestrian crossing 3 lights.UK light cycle. Can be changed to USA and EU with no Amber shown on Red to Green 4 by // out lines 77,95. 5 1 interrupt Peestrian request is latched in and subsquent presses are ignored 6 until crossing cycle completed. 7 2 interurupt detected traffic sense to prolong green period depending on 8 traffic volume but time limited so as not to block one way. Default green time set by "flowTime" and 9 modified by "extraDelay" which is limited to one interrupt value set in subroutine call. 10 Run on ARDUINO Mega 2560. 11 12 Malcolm Page March 2021. updated January 05 2022.new functions 7/1,Test Func addded 9/1 13 Contact malcolm@gbapj.com 14 ================================================================================= */ 15int button0 = 2; 16int button1 = 3; 17int button2 = 21; 18int button3 = 20; 19int button4 = 19; 20// int button5=18; 21const int north_redpin = 5; 22const int north_orangpin = 4; 23const int north_grnpin = 6; 24const int south_redpin = 8; 25const int south_orangpin = 9; 26const int south_grnpin = 12; 27const int pedRedpin = 13; 28const int pedGrnpin = 10; 29const int pedWaitpin = 7; 30const int crossTime = 10000; // Time pedestrian to cross 31const int flowTime = 5000; // Time for traffic to flow will be modified by traffic sensor interrupt variable "extraDelay" 32const int amberTime = 2000; 33const int TrfWait=5000; 34const int RedPause=3000; 35int pedReq = 0; 36int northDelay = 0; 37int southDelay = 0; 38int NextraDelay = 0; 39int SextraDelay = 0; 40int CrossCalls = 0; 41 42//================================================================== 43void setup() 44{ 45 Serial.begin(9600); 46 pinMode (pedGrnpin, OUTPUT); 47 pinMode (pedRedpin, OUTPUT); 48 pinMode (north_redpin, OUTPUT); 49 pinMode (north_orangpin, OUTPUT); 50 pinMode (north_grnpin, OUTPUT); 51 pinMode (south_redpin, OUTPUT); 52 pinMode (south_orangpin, OUTPUT); 53 pinMode (south_grnpin, OUTPUT); 54 pinMode(button0, INPUT); 55 pinMode(button1, INPUT); 56 pinMode(button2, INPUT); 57 pinMode(button3, INPUT); 58 pinMode(button4, INPUT); 59 // pinMode(button5,INPUT); 60 pinMode(pedWaitpin, OUTPUT); 61 TestLights(); // Test lights 62 delay(5000); 63 digitalWrite(north_redpin, HIGH); // INITIAL STATE SET TO 64 digitalWrite(south_grnpin, HIGH); // NORTH RED SOUTH GREEN 65 digitalWrite(pedRedpin, HIGH); //initial set to RED 66 attachInterrupt(0, CrossingRequest, RISING); //int 0 on Pin 2 67 attachInterrupt(1, DelayNorthRequest, RISING); //int 1 on Pin 3 68 attachInterrupt(2, DelaySouthRequest, RISING); //int 2 on Pin 21 69 attachInterrupt(3, Funct3, RISING); //int 3 on Pin 20 70 attachInterrupt(4, Funct4, RISING); //int 4 on Pin 19 71 // attachInterrupt(5,Funct5,RISING); //int 5 on Pin 18 72 delay(flowTime); 73} 74 75//================================== Main core of programme ============================== 76void loop() 77{ 78 79 digitalWrite(south_orangpin, HIGH); 80 digitalWrite(south_grnpin, LOW); 81 delay(amberTime); // Changing pause 82 digitalWrite(south_orangpin, LOW); 83 digitalWrite(south_redpin, HIGH); // sets south to stop 84 delay(RedPause); // wait both stop 85 digitalWrite(north_orangpin, HIGH); // disable for USA/EU Sequence 86 delay(amberTime); // Pause amber north 87 digitalWrite(north_redpin, LOW); 88 digitalWrite(north_orangpin, LOW); 89 digitalWrite(north_grnpin, HIGH); // sets north to go 90 delay(flowTime); 91 Serial.println("86"); 92 delay(NextraDelay); 93 Serial.println(NextraDelay); 94 NextraDelay = 0; 95 delay(TrfWait); // wait for more traffic 96 Serial.println("91"); 97 delay(NextraDelay); 98 Serial.println (NextraDelay); 99 NextraDelay = 0; 100 digitalWrite(north_orangpin, HIGH); // set north to stop 101 digitalWrite(north_grnpin, LOW); 102 delay(amberTime); // pause amber 103 digitalWrite(north_redpin, HIGH); 104 digitalWrite(north_orangpin, LOW); 105 delay(RedPause); // wait both stop 106 if (pedReq > 1) setcrossing(); 107 digitalWrite(south_orangpin, HIGH); // disable for USA/EU Sequence 108 delay(amberTime); // pause amber south 109 digitalWrite(south_redpin, LOW); 110 digitalWrite(south_grnpin, HIGH); 111 digitalWrite(south_orangpin, LOW); // set south to go 112 delay(flowTime); 113 Serial.println("104"); 114 delay(SextraDelay); 115 Serial.println(SextraDelay); 116 SextraDelay=0; 117 Serial.println("106"); 118 delay(TrfWait); 119 Serial.println("108"); // wait for more traffic 120 delay(SextraDelay); 121 Serial.println(SextraDelay); 122 SextraDelay = 0; 123 if (pedReq > 0) 124 { 125 pedReq++; // hold off crossing for a further sequence 126 } 127} 128//======================= Subroutines (Functions) ========================== 129void CrossingRequest() // Interrupt 0 here 130{ 131 setwait(); 132} 133//------------------------------------------------- 134void DelayNorthRequest() // Interrupt 2 here 135{ 136 waitnorth(); 137} 138//------------------------------------------------- 139void DelaySouthRequest() // intereupt 1 here 140{ 141 waitsouth(); 142} 143//------------------------------------------------- 144void setwait() 145{ 146 delay(50); 147 (CrossCalls++); 148 if (pedReq > 0) return; 149 digitalWrite(pedWaitpin, HIGH); 150 pedReq = 1; 151} 152//------------------------------------------------- 153void setcrossing() 154{ 155 digitalWrite(south_redpin, HIGH); // sets south to stop 156 delay(2000); 157 digitalWrite(pedWaitpin, LOW); 158 digitalWrite(pedRedpin, LOW); 159 digitalWrite(pedGrnpin, HIGH); 160 delay(crossTime); 161 digitalWrite(pedGrnpin, LOW); 162 flashpedred(); 163 digitalWrite(pedRedpin, HIGH); 164 delay(2000); 165 pedReq = 0; // Re-enable crossing request 166} 167//------------------------------------- 168void flashpedred() 169{ 170 for (int i = 1; i <= 20; i++) { 171 digitalWrite(pedRedpin, HIGH); 172 delay(250); 173 digitalWrite(pedRedpin, LOW); 174 delay(250); 175 } 176} 177//------------------------------------- 178void flashpedgreen() 179{ 180 for (int i = 1; i <= 20; i++) { 181 digitalWrite(pedGrnpin,HIGH); 182 delay(250); 183 digitalWrite(pedGrnpin,LOW); 184 delay(250); 185 } 186} 187//------------------------------------ 188void waitnorth() 189{ 190 NextraDelay = 15000; 191 Serial.println("north"); 192} 193//------------------------------------ 194void waitsouth() 195{ 196 SextraDelay = 15000; 197 Serial.println("south"); 198} 199//-------------------------------------- 200void Funct3() 201{ 202 Serial.println("Funct 3"); 203} 204//+++++++++++++++++++++++++++++++++++ 205void Funct4() 206{ Serial.println("Funct4"); 207} 208//+++++++++++++++++++++++++++++++++ 209void TestLights() 210{ 211 digitalWrite(south_orangpin, HIGH); 212 digitalWrite(south_redpin, HIGH); 213 digitalWrite(south_grnpin, HIGH); 214 digitalWrite(north_grnpin, HIGH); 215 digitalWrite(north_orangpin, HIGH); 216 digitalWrite(north_redpin, HIGH); 217 digitalWrite(pedWaitpin, HIGH); 218 flashpedred(); 219 flashpedgreen(); 220 digitalWrite(south_orangpin, LOW); 221 digitalWrite(south_redpin, LOW); 222 digitalWrite(south_grnpin, LOW); 223 digitalWrite(north_grnpin, LOW); 224 digitalWrite(north_orangpin, LOW); 225 digitalWrite(north_redpin, LOW); 226 digitalWrite(pedWaitpin, LOW); 227} 228//+++++++++++++++++++++++++++++++++++++++++ 229
new_traffic_light.ino
arduino
1/* Project to demonstrate use of multiple interrupts in a real world situation. 2 Traffic light control with pedestrian crossing 3 lights.UK light cycle. Can be changed to USA and EU with no Amber shown on Red to Green 4 by // out lines 77,95. 5 1 interrupt Peestrian request is latched in and subsquent presses are ignored 6 until crossing cycle completed. 7 2 interurupt detected traffic sense to prolong green period depending on 8 traffic volume but time limited so as not to block one way. Default green time set by "flowTime" and 9 modified by "extraDelay" which is limited to one interrupt value set in subroutine call. 10 Run on ARDUINO Mega 2560. 11 12 Malcolm Page March 2021. updated January 05 2022.new functions 7/1,Test Func addded 9/1 13 Contact malcolm@gbapj.com 14 ================================================================================= */ 15int button0 = 2; 16int button1 = 3; 17int button2 = 21; 18int button3 = 20; 19int button4 = 19; 20// int button5=18; 21const int north_redpin = 5; 22const int north_orangpin = 4; 23const int north_grnpin = 6; 24const int south_redpin = 8; 25const int south_orangpin = 9; 26const int south_grnpin = 12; 27const int pedRedpin = 13; 28const int pedGrnpin = 10; 29const int pedWaitpin = 7; 30const int crossTime = 10000; // Time pedestrian to cross 31const int flowTime = 5000; // Time for traffic to flow will be modified by traffic sensor interrupt variable "extraDelay" 32const int amberTime = 2000; 33const int TrfWait=5000; 34const int RedPause=3000; 35int pedReq = 0; 36int northDelay = 0; 37int southDelay = 0; 38int NextraDelay = 0; 39int SextraDelay = 0; 40int CrossCalls = 0; 41 42//================================================================== 43void setup() 44{ 45 Serial.begin(9600); 46 pinMode (pedGrnpin, OUTPUT); 47 pinMode (pedRedpin, OUTPUT); 48 pinMode (north_redpin, OUTPUT); 49 pinMode (north_orangpin, OUTPUT); 50 pinMode (north_grnpin, OUTPUT); 51 pinMode (south_redpin, OUTPUT); 52 pinMode (south_orangpin, OUTPUT); 53 pinMode (south_grnpin, OUTPUT); 54 pinMode(button0, INPUT); 55 pinMode(button1, INPUT); 56 pinMode(button2, INPUT); 57 pinMode(button3, INPUT); 58 pinMode(button4, INPUT); 59 // pinMode(button5,INPUT); 60 pinMode(pedWaitpin, OUTPUT); 61 TestLights(); // Test lights 62 delay(5000); 63 digitalWrite(north_redpin, HIGH); // INITIAL STATE SET TO 64 digitalWrite(south_grnpin, HIGH); // NORTH RED SOUTH GREEN 65 digitalWrite(pedRedpin, HIGH); //initial set to RED 66 attachInterrupt(0, CrossingRequest, RISING); //int 0 on Pin 2 67 attachInterrupt(1, DelayNorthRequest, RISING); //int 1 on Pin 3 68 attachInterrupt(2, DelaySouthRequest, RISING); //int 2 on Pin 21 69 attachInterrupt(3, Funct3, RISING); //int 3 on Pin 20 70 attachInterrupt(4, Funct4, RISING); //int 4 on Pin 19 71 // attachInterrupt(5,Funct5,RISING); //int 5 on Pin 18 72 delay(flowTime); 73} 74 75//================================== Main core of programme ============================== 76void loop() 77{ 78 79 digitalWrite(south_orangpin, HIGH); 80 digitalWrite(south_grnpin, LOW); 81 delay(amberTime); // Changing pause 82 digitalWrite(south_orangpin, LOW); 83 digitalWrite(south_redpin, HIGH); // sets south to stop 84 delay(RedPause); // wait both stop 85 digitalWrite(north_orangpin, HIGH); // disable for USA/EU Sequence 86 delay(amberTime); // Pause amber north 87 digitalWrite(north_redpin, LOW); 88 digitalWrite(north_orangpin, LOW); 89 digitalWrite(north_grnpin, HIGH); // sets north to go 90 delay(flowTime); 91 Serial.println("86"); 92 delay(NextraDelay); 93 Serial.println(NextraDelay); 94 NextraDelay = 0; 95 delay(TrfWait); // wait for more traffic 96 Serial.println("91"); 97 delay(NextraDelay); 98 Serial.println (NextraDelay); 99 NextraDelay = 0; 100 digitalWrite(north_orangpin, HIGH); // set north to stop 101 digitalWrite(north_grnpin, LOW); 102 delay(amberTime); // pause amber 103 digitalWrite(north_redpin, HIGH); 104 digitalWrite(north_orangpin, LOW); 105 delay(RedPause); // wait both stop 106 if (pedReq > 1) setcrossing(); 107 digitalWrite(south_orangpin, HIGH); // disable for USA/EU Sequence 108 delay(amberTime); // pause amber south 109 digitalWrite(south_redpin, LOW); 110 digitalWrite(south_grnpin, HIGH); 111 digitalWrite(south_orangpin, LOW); // set south to go 112 delay(flowTime); 113 Serial.println("104"); 114 delay(SextraDelay); 115 Serial.println(SextraDelay); 116 SextraDelay=0; 117 Serial.println("106"); 118 delay(TrfWait); 119 Serial.println("108"); // wait for more traffic 120 delay(SextraDelay); 121 Serial.println(SextraDelay); 122 SextraDelay = 0; 123 if (pedReq > 0) 124 { 125 pedReq++; // hold off crossing for a further sequence 126 } 127} 128//======================= Subroutines (Functions) ========================== 129void CrossingRequest() // Interrupt 0 here 130{ 131 setwait(); 132} 133//------------------------------------------------- 134void DelayNorthRequest() // Interrupt 2 here 135{ 136 waitnorth(); 137} 138//------------------------------------------------- 139void DelaySouthRequest() // intereupt 1 here 140{ 141 waitsouth(); 142} 143//------------------------------------------------- 144void setwait() 145{ 146 delay(50); 147 (CrossCalls++); 148 if (pedReq > 0) return; 149 digitalWrite(pedWaitpin, HIGH); 150 pedReq = 1; 151} 152//------------------------------------------------- 153void setcrossing() 154{ 155 digitalWrite(south_redpin, HIGH); // sets south to stop 156 delay(2000); 157 digitalWrite(pedWaitpin, LOW); 158 digitalWrite(pedRedpin, LOW); 159 digitalWrite(pedGrnpin, HIGH); 160 delay(crossTime); 161 digitalWrite(pedGrnpin, LOW); 162 flashpedred(); 163 digitalWrite(pedRedpin, HIGH); 164 delay(2000); 165 pedReq = 0; // Re-enable crossing request 166} 167//------------------------------------- 168void flashpedred() 169{ 170 for (int i = 1; i <= 20; i++) { 171 digitalWrite(pedRedpin, HIGH); 172 delay(250); 173 digitalWrite(pedRedpin, LOW); 174 delay(250); 175 } 176} 177//------------------------------------- 178void flashpedgreen() 179{ 180 for (int i = 1; i <= 20; i++) { 181 digitalWrite(pedGrnpin,HIGH); 182 delay(250); 183 digitalWrite(pedGrnpin,LOW); 184 delay(250); 185 } 186} 187//------------------------------------ 188void waitnorth() 189{ 190 NextraDelay = 15000; 191 Serial.println("north"); 192} 193//------------------------------------ 194void waitsouth() 195{ 196 SextraDelay = 15000; 197 Serial.println("south"); 198} 199//-------------------------------------- 200void Funct3() 201{ 202 Serial.println("Funct 3"); 203} 204//+++++++++++++++++++++++++++++++++++ 205void Funct4() 206{ Serial.println("Funct4"); 207} 208//+++++++++++++++++++++++++++++++++ 209void TestLights() 210{ 211 digitalWrite(south_orangpin, HIGH); 212 digitalWrite(south_redpin, HIGH); 213 digitalWrite(south_grnpin, HIGH); 214 digitalWrite(north_grnpin, HIGH); 215 digitalWrite(north_orangpin, HIGH); 216 digitalWrite(north_redpin, HIGH); 217 digitalWrite(pedWaitpin, HIGH); 218 flashpedred(); 219 flashpedgreen(); 220 digitalWrite(south_orangpin, LOW); 221 digitalWrite(south_redpin, LOW); 222 digitalWrite(south_grnpin, LOW); 223 digitalWrite(north_grnpin, LOW); 224 digitalWrite(north_orangpin, LOW); 225 digitalWrite(north_redpin, LOW); 226 digitalWrite(pedWaitpin, LOW); 227} 228//+++++++++++++++++++++++++++++++++++++++++ 229
Downloadable files
project_tl_DWB1QXJeYr.JPG
project_tl_DWB1QXJeYr.JPG
Comments
Only logged in users can leave comments