1

enter image description hereI have mini project which is Emergency Traffic Light for Ambulance, police etc. What I am try to do is when emergency traffic light go through first sensor 1 (in this code I just using button, “SA”), traffic light will be pause and the traffic light for ambulance will be green. After ambulance go through second sensor (SB), traffic light will be reset and the flow become normal (red → orange → green).

My problem is I don’t know how to make interrupt for this code. I hope anyone can help me.

#define SA 2 //sensor 1
#define SB 3 //sensor 2
#define IND 6 //indicator
/////////////////////////////////////
#define AH 13   // A
#define AK 12
#define AM 11
/////////////////////////////////////////
#define CH 10    //C B
#define CK 9
#define CM 8
#define yellowBlinkTime 500 // 0.5 seconds for yellow light blink

///////////////////////////////////c
#define BH A0   // B
#define BK A1
#define BM A2
///////////////////////////////////////////////

boolean TA = true; // west = true, east = false
int flowTime = 10000; // amount of time to let traffic flow
int changeDelay = 2000; // amount of time between color changes

void setup()
{
pinMode(SA, INPUT); //sensor 1
pinMode(SB, INPUT); //sensor 2
pinMode(IND, OUTPUT);   //indicator

pinMode(AH, OUTPUT);   // A
pinMode(AK, OUTPUT);
pinMode(AM, OUTPUT);
///
pinMode(CH, OUTPUT);   //C
pinMode(CK, OUTPUT);
pinMode(CM, OUTPUT);
///////
pinMode(BH, OUTPUT);   // B
pinMode(BK, OUTPUT);
pinMode(BM, OUTPUT);

// set initial state for lights -
//jalan A
digitalWrite(AM, LOW);
digitalWrite(AK, LOW);
digitalWrite(AH, HIGH);  

//jalan C
digitalWrite(CM, HIGH);    
digitalWrite(CK, LOW);
digitalWrite(CH, LOW);

///// JAlAN B
digitalWrite(BM, HIGH);    
digitalWrite(BK, LOW);
digitalWrite(BH, LOW);

}//-------setup end


void loop()
{
  if(digitalRead(SA) == HIGH )
  { 
  blink();
  //flow();
  //break;
  }

  else{

  flow(); // p void flow normal
} // end else 
} // end void loop

void flow() // flow normal tf
{
//flow1
digitalWrite(AK, LOW);
digitalWrite(AM, LOW);
digitalWrite(CK, LOW);
digitalWrite(AH, HIGH);  
digitalWrite(CM, HIGH);   

digitalWrite(BH, LOW);
digitalWrite(BK, LOW);
digitalWrite(BM, HIGH); 

delay(changeDelay);

//flow2
digitalWrite(AH, LOW);
digitalWrite(AM, LOW);
digitalWrite(AK, HIGH); 

digitalWrite(CM, HIGH);    

digitalWrite(BM, HIGH);
delay(changeDelay);

//flow3
digitalWrite(AK, LOW);
digitalWrite(CM, LOW);
digitalWrite(AM, HIGH);   
digitalWrite(CH, HIGH);  
digitalWrite(BM, HIGH);
delay(changeDelay);


//flow4
digitalWrite(AK, LOW);
digitalWrite(AH, LOW);
digitalWrite(CM, LOW);
digitalWrite(CH, LOW);
digitalWrite(AM, HIGH);    
digitalWrite(CK, HIGH);  
digitalWrite(BM, HIGH);
delay(changeDelay);

//flow 5
digitalWrite(AK, LOW);
digitalWrite(AH, LOW);
digitalWrite(AM, HIGH);    
digitalWrite(BH, HIGH);    
digitalWrite(BM, LOW);
digitalWrite(CK, LOW);
digitalWrite(CH, LOW);
digitalWrite(CM, HIGH);  
delay(changeDelay);

//flow 6
digitalWrite(AK, LOW);
digitalWrite(AH, LOW);
digitalWrite(AM, HIGH);    

digitalWrite(BK, HIGH);    
digitalWrite(BH, LOW);

digitalWrite(CM, HIGH);  
digitalWrite(CK, LOW);
digitalWrite(CH, LOW);

delay(changeDelay);

}//-------flow end

void blink()
{
[enter link description here][2]
    digitalWrite(AH, LOW);
    digitalWrite(AM, LOW);

    digitalWrite(BH, LOW);
    digitalWrite(BM, LOW);

    digitalWrite(CH, LOW);
    digitalWrite(CM, LOW);


    digitalWrite(AK, LOW);
    digitalWrite(CK, LOW);
    digitalWrite(BK, LOW);
    delay(yellowBlinkTime);



    ////////////////////////////////////////////////// BLINK BLINK
    digitalWrite(AK, HIGH); 
    digitalWrite(CK, HIGH); 
    digitalWrite(BK, HIGH); 
    delay(yellowBlinkTime);

}  ///////////////// end void blink
  • 1
    Try using millis() instead of delay(). delay() literally delays the code, thus stopping it from doing it anything else. The loop simply pauses at the delay for the desired amount of time: thus nothing else can be done. millis() does not do this. Instead, it continuously reads out time, compares it to an earlier measurement to see how much time has passed. You can then program what it should do when it crosses a specific threshold (i.e. 20 seconds). You can find tutorials on millis() on the Arduino website. – Len Jul 29 '16 at 8:56
  • I can truly advice you to use millis() from now on, instead of delay() as it will make your code much more efficient. Especially with semi-complicated stuff like this. – Len Jul 29 '16 at 8:57
  • 5
    Also you should look at using a Finite State Machine instead of linear code like that. – Majenko Jul 29 '16 at 9:31
  • The use of two letter variable and constants names make the code RF difficult to read. You could look at using functions to perform repetitive tasks and reduce the volume of code. Using delay statements like this is locking up your code, also it means that your timing will drift particularly if you service an interrupt, try triggering state changes at a certain time, this will also let you monitor sensors without interrupts. – Code Gorilla Jul 29 '16 at 12:21
  • monitor pin changes with github.com/neu-rah/PCINT , still you have cycle your code to allow changes, do not use delay and do not do all work inside interrupt handlers (it wont work) instead use them to set a flag and allow your code to react to that flag change quickly (therefor do not use delay) – neu-rah Jul 30 '16 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.