Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
aerobat
Published © GPL3+

Traffic Light Controller with pedestrian crossing

This project was to demonstrate the use of interrupts. A Traffic Light system with crossing control in line with everyday systems.

IntermediateFull instructions provided1,387
Traffic Light Controller with pedestrian crossing

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
LED (generic)
LED (generic)
×7
3 lead 2 colour LED
Red / Green
×1

Hand tools and fabrication machines

Breadboard, 400 Pin
Breadboard, 400 Pin

Story

Read more

Schematics

project_tl_DWB1QXJeYr.JPG

Code

new_traffic_light.ino

Arduino
/* Project to demonstrate use of multiple interrupts in a real world situation.
   Traffic light control with pedestrian crossing
    lights.UK light cycle. Can be changed to USA and EU with no Amber shown on Red to Green
    by // out lines 77,95.
   1 interrupt Peestrian request is latched in and subsquent presses are ignored
    until crossing cycle completed.
    2 interurupt detected traffic sense to prolong green period depending on
    traffic volume but time limited so as not to block one way. Default green time set by "flowTime" and
    modified by "extraDelay" which is limited to one interrupt value set in subroutine call.
    Run on ARDUINO Mega 2560.

  Malcolm Page March 2021. updated January 05 2022.new functions 7/1,Test Func addded 9/1
  Contact malcolm@gbapj.com
  ================================================================================= */
int button0 = 2;
int button1 = 3;
int button2 = 21;
int button3 = 20;
int button4 = 19;
// int button5=18;
const int north_redpin = 5;
const int north_orangpin = 4;
const int north_grnpin = 6;
const int south_redpin = 8;
const int south_orangpin = 9;
const int south_grnpin = 12;
const int pedRedpin = 13;
const int pedGrnpin = 10;
const int pedWaitpin = 7;
const int crossTime = 10000;   // Time pedestrian to cross
const int flowTime = 5000;     // Time for traffic to flow will be modified by traffic sensor interrupt variable "extraDelay"
const int amberTime = 2000;   
const int TrfWait=5000;
const int RedPause=3000;
int pedReq = 0;
int northDelay = 0;
int southDelay = 0;
int NextraDelay = 0;
int SextraDelay = 0;
int CrossCalls = 0;

//==================================================================
void setup()
{
  Serial.begin(9600);
  pinMode (pedGrnpin, OUTPUT);
  pinMode (pedRedpin, OUTPUT);
  pinMode (north_redpin, OUTPUT);
  pinMode (north_orangpin, OUTPUT);
  pinMode (north_grnpin, OUTPUT);
  pinMode (south_redpin, OUTPUT);
  pinMode (south_orangpin, OUTPUT);
  pinMode (south_grnpin, OUTPUT);
  pinMode(button0, INPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  // pinMode(button5,INPUT);
  pinMode(pedWaitpin, OUTPUT);
  TestLights();                             // Test lights
  delay(5000);
  digitalWrite(north_redpin, HIGH);         // INITIAL STATE SET TO
  digitalWrite(south_grnpin, HIGH);         // NORTH RED SOUTH GREEN
  digitalWrite(pedRedpin, HIGH);            //initial set to RED
  attachInterrupt(0, CrossingRequest, RISING);   //int 0 on Pin 2
  attachInterrupt(1, DelayNorthRequest, RISING); //int 1 on Pin 3
  attachInterrupt(2, DelaySouthRequest, RISING); //int 2 on Pin 21
  attachInterrupt(3, Funct3, RISING);            //int 3 on Pin 20
  attachInterrupt(4, Funct4, RISING);            //int 4 on Pin 19
  // attachInterrupt(5,Funct5,RISING);           //int 5 on Pin 18
  delay(flowTime);
}

//================================== Main core of programme ==============================
void loop()
{

  digitalWrite(south_orangpin, HIGH);
  digitalWrite(south_grnpin, LOW);
  delay(amberTime);                         // Changing pause
  digitalWrite(south_orangpin, LOW);
  digitalWrite(south_redpin, HIGH);         // sets south to stop
  delay(RedPause);                          // wait both stop
  digitalWrite(north_orangpin, HIGH);       // disable for USA/EU Sequence
  delay(amberTime);                         // Pause amber north
  digitalWrite(north_redpin, LOW);
  digitalWrite(north_orangpin, LOW);
  digitalWrite(north_grnpin, HIGH);         // sets north to go
  delay(flowTime);
  Serial.println("86");
  delay(NextraDelay);
  Serial.println(NextraDelay);
  NextraDelay = 0;
  delay(TrfWait);                           // wait for more traffic
  Serial.println("91");
  delay(NextraDelay);
  Serial.println (NextraDelay);
  NextraDelay = 0;
  digitalWrite(north_orangpin, HIGH);       // set north to stop
  digitalWrite(north_grnpin, LOW);
  delay(amberTime);                         // pause amber
  digitalWrite(north_redpin, HIGH);
  digitalWrite(north_orangpin, LOW);
  delay(RedPause);                          // wait both stop
  if (pedReq > 1) setcrossing();
  digitalWrite(south_orangpin, HIGH);       // disable for USA/EU Sequence
  delay(amberTime);                         // pause amber south
  digitalWrite(south_redpin, LOW);
  digitalWrite(south_grnpin, HIGH);
  digitalWrite(south_orangpin, LOW);        // set south to go
  delay(flowTime);
  Serial.println("104");
  delay(SextraDelay);
  Serial.println(SextraDelay);
  SextraDelay=0;
  Serial.println("106");
  delay(TrfWait);
  Serial.println("108");                    // wait for more traffic
  delay(SextraDelay);
  Serial.println(SextraDelay);
  SextraDelay = 0;
  if (pedReq > 0)
  {
    pedReq++;                               // hold off crossing for a further sequence
  }
}
//======================= Subroutines (Functions) ==========================
void CrossingRequest()   // Interrupt 0 here
{
  setwait();
}
//-------------------------------------------------
void DelayNorthRequest()  // Interrupt 2 here
{
  waitnorth();
}
//-------------------------------------------------
void DelaySouthRequest()  // intereupt 1 here
{
  waitsouth();
}
//-------------------------------------------------
void setwait()
{
  delay(50);
  (CrossCalls++);
  if (pedReq > 0) return;
  digitalWrite(pedWaitpin, HIGH);
  pedReq = 1;
}
//-------------------------------------------------
void setcrossing()
{
  digitalWrite(south_redpin, HIGH);     // sets south to stop
  delay(2000);
  digitalWrite(pedWaitpin, LOW);
  digitalWrite(pedRedpin, LOW);
  digitalWrite(pedGrnpin, HIGH);
  delay(crossTime);
  digitalWrite(pedGrnpin, LOW);
  flashpedred();
  digitalWrite(pedRedpin, HIGH);
  delay(2000);
  pedReq = 0;                      // Re-enable crossing request
}
//-------------------------------------
void flashpedred()
{
  for (int i = 1; i <= 20; i++) {
    digitalWrite(pedRedpin, HIGH);
    delay(250);
    digitalWrite(pedRedpin, LOW);
    delay(250);
  }
}
//-------------------------------------
void flashpedgreen()
{
  for (int i = 1; i <= 20; i++) {
    digitalWrite(pedGrnpin,HIGH);
    delay(250);
    digitalWrite(pedGrnpin,LOW);
    delay(250);
  }
}
//------------------------------------
void waitnorth()
{
  NextraDelay = 15000;
  Serial.println("north");
}
//------------------------------------
void waitsouth()
{
  SextraDelay = 15000;
  Serial.println("south");
}
//--------------------------------------
void Funct3()
{
  Serial.println("Funct 3");
}
//+++++++++++++++++++++++++++++++++++
void Funct4()
{ Serial.println("Funct4");
}
//+++++++++++++++++++++++++++++++++
void TestLights()
{
  digitalWrite(south_orangpin, HIGH);
  digitalWrite(south_redpin, HIGH);
  digitalWrite(south_grnpin, HIGH);
  digitalWrite(north_grnpin, HIGH);
  digitalWrite(north_orangpin, HIGH);
  digitalWrite(north_redpin, HIGH);
  digitalWrite(pedWaitpin, HIGH);
  flashpedred();
  flashpedgreen();
  digitalWrite(south_orangpin, LOW);
  digitalWrite(south_redpin, LOW);
  digitalWrite(south_grnpin, LOW);
  digitalWrite(north_grnpin, LOW);
  digitalWrite(north_orangpin, LOW);
  digitalWrite(north_redpin, LOW);
  digitalWrite(pedWaitpin, LOW);
}
//+++++++++++++++++++++++++++++++++++++++++

Credits

aerobat

aerobat

0 projects • 0 followers

Comments