Traffic lights code help

Beginner here so any help is appreciated.

Looking for advice on how to simulate one of the green lights to blink acting as a green turn signal then solidify to signify regular green light then continue its sequence.

Also trying to add a button to act as a sensor so it reads there is a “car” there and turns any signal green.

Thought I could just add
delay(1000);
digitalWrite(4,0);
delay(500);
digitalWrite(4,1);
delay(500);
digitalWrite(4,0);
delay(500);

because 4 is one of the green lights pins but then it cuts all the other red lights off, so i added the little bit of code below but it was turning all the lights on iffy i just want the green light to blink while red lights remain on that way it sort of signifys a green left turn light.

digitalWrite(8, 1);
digitalWrite(5, 1);
digitalWrite(2, 1);
digitalWrite(13, 1);

2,7,10,13 are red led pins
3,6,9,12 are yellow led pins
4,5,11,8 are the green led pins

I created a sequence that will work for me to signify a left turn signal… only thing I can do to make it better is make that Led flash during its delay…

Main thing I need to do is create a button that shortens the delay of a red light sequence that way it acts as a sensor and tells it a car is there. if that makes sense… basically i need to make it where I can press a button and the delay shortens on the current red light…

Heres the new sequence I also updated it above.
<

void setup() {
  // put your setup code here, to run once:
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT); //defining pins as output
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
//when program starts right green and left green should be ON and up Red and down red should be on so 
digitalWrite(3,0); //yellow LEDs 
digitalWrite(6,0);
digitalWrite(9,0);
digitalWrite(12,0); 
digitalWrite(4,1);
digitalWrite(10,1);
digitalWrite(13,1);
digitalWrite(7,1); //now we have to give some delay
delay(5000);
//now all the yellow lights should be ON and all the previous LED should be off so
digitalWrite(13,0);
digitalWrite(4,0);
digitalWrite(10,0);
digitalWrite(7,0);
digitalWrite(3,1); //yellow LEDs
digitalWrite(6,1);
digitalWrite(9,1);
digitalWrite(12,1); 
//again some delay for yellow LEds
delay(1000);
//now we have to turn off all the yellow leds and we have to turn on the up down green and right left red so
digitalWrite(3,0); //yellow LEDs
digitalWrite(6,0);
digitalWrite(9,0);
digitalWrite(12,0); 
digitalWrite(8,1);
digitalWrite(5,1);
digitalWrite(2,1);
digitalWrite(13,1);
//again delay
delay(5000);
digitalWrite(8,0);
digitalWrite(5,0);
digitalWrite(2,0);
digitalWrite(13,0);//YELLOW LEDS ON
digitalWrite(3,1); //yellow LEDs
digitalWrite(6,1);
digitalWrite(9,1);
digitalWrite(12,1);
delay(5000);
digitalWrite(3,0); //yellow LEDs // new sequence 
digitalWrite(6,0);
digitalWrite(9,0);
digitalWrite(12,0); 
digitalWrite(3,1);
digitalWrite(6,1);
digitalWrite(9,1);
digitalWrite(12,1); //now we have to give some delay
delay(1000);
//now all the yellow lights should be ON and all the previous LED should be off so
digitalWrite(4,0);
digitalWrite(10,0);
digitalWrite(11,0);
digitalWrite(7,0);
digitalWrite(3,1); //yellow LEDs
digitalWrite(6,1);
digitalWrite(9,1);
digitalWrite(12,1); 
//again some delay for yellow LEds
delay(1000);
//now we have to turn off all the yellow leds and we have to turn on the up down green and right left red so
digitalWrite(3,0); //green leds
digitalWrite(6,0);
digitalWrite(9,0);
digitalWrite(12,0); 
digitalWrite(3,1);
digitalWrite(6,1);
digitalWrite(9,1);
digitalWrite(12,1);
//again delay
delay(1000);


}

I created a sequence that will work for me to signify a left turn signal.. only thing I can do to make it better is make that Led flash during its delay..

Main thing I need to do is create a button that shortens the delay of a red light sequence that way it acts as a sensor and tells it a car is there. if that makes sense.. basically i need to make it where I can press a button and the delay shortens on the current red light...

Heres the new sequence I also u

I created a sequence that will work for me to signify a left turn signal.. only thing I can do to make it better is make that Led flash during its delay..

Main thing I need to do is create a button that shortens the delay of a red light sequence that way it acts as a sensor and tells it a car is there. if that makes sense.. basically i need to make it where I can press a button and the delay shortens on the current red light...

Am I able to use pin 1 for a button?

Heres the new sequence I also updated it above.

void setup() {
  // put your setup code here, to run once:
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT); //defining pins as output
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
//when program starts right green and left green should be ON and up Red and down red should be on so 
digitalWrite(3,0); //yellow LEDs 
digitalWrite(6,0);
digitalWrite(9,0);
digitalWrite(12,0); 
digitalWrite(4,1);
digitalWrite(10,1);
digitalWrite(13,1);
digitalWrite(7,1); //now we have to give some delay
delay(5000);
//now all the yellow lights should be ON and all the previous LED should be off so
digitalWrite(13,0);
digitalWrite(4,0);
digitalWrite(10,0);
digitalWrite(7,0);
digitalWrite(3,1); //yellow LEDs
digitalWrite(6,1);
digitalWrite(9,1);
digitalWrite(12,1); 
//again some delay for yellow LEds
delay(1000);
//now we have to turn off all the yellow leds and we have to turn on the up down green and right left red so
digitalWrite(3,0); //yellow LEDs
digitalWrite(6,0);
digitalWrite(9,0);
digitalWrite(12,0); 
digitalWrite(8,1);
digitalWrite(5,1);
digitalWrite(2,1);
digitalWrite(13,1);
//again delay
delay(5000);
digitalWrite(8,0);
digitalWrite(5,0);
digitalWrite(2,0);
digitalWrite(13,0);//YELLOW LEDS ON
digitalWrite(3,1); //yellow LEDs
digitalWrite(6,1);
digitalWrite(9,1);
digitalWrite(12,1);
delay(5000);
digitalWrite(3,0); //yellow LEDs // new sequence 
digitalWrite(6,0);
digitalWrite(9,0);
digitalWrite(12,0); 
digitalWrite(3,1);
digitalWrite(6,1);
digitalWrite(9,1);
digitalWrite(12,1); //now we have to give some delay
delay(1000);
//now all the yellow lights should be ON and all the previous LED should be off so
digitalWrite(4,0);
digitalWrite(10,0);
digitalWrite(11,0);
digitalWrite(7,0);
digitalWrite(3,1); //yellow LEDs
digitalWrite(6,1);
digitalWrite(9,1);
digitalWrite(12,1); 
//again some delay for yellow LEds
delay(1000);
//now we have to turn off all the yellow leds and we have to turn on the up down green and right left red so
digitalWrite(3,0); //green leds
digitalWrite(6,0);
digitalWrite(9,0);
digitalWrite(12,0); 
digitalWrite(3,1);
digitalWrite(6,1);
digitalWrite(9,1);
digitalWrite(12,1);
//again delay
delay(1000);


}

OK, you need to read up on this reference and this one to understand how to write multi-functional code. :sunglasses:

It is not appropriate to do this sort of thing in “straight line” code, you need for example to be able to have the blinking happen independently of the check for traffic to make the lights change.

Have a think about this code for a Pedestrian crossing:

// Pedestrian crossing lights

const int led13Pin =  13;    // LED pin number
const int PgrnPin =  6;
const int PredPin =  5;
const int RgrnPin =  4;
const int RyelPin =  3;
const int RredPin =  2;
const int button1 =  10;

int led13State = LOW;        // initialise the LED
int PgrnState = LOW;
int PredState = LOW;
int RgrnState = LOW;
int RyelState = LOW;
int RredState = LOW;
char bstate1 = 0;
boolean press = false;

unsigned long count1 = 0;   // will store last time LED was updated
unsigned long count2 = 0;
unsigned long count3 = 0;
unsigned long count4 = 0;
unsigned long count5 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.

// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check 
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

void setout(unsigned long *marker) {
  *marker = millis();             // initialise
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far, 
    if (button == HIGH) return false; // Nothing happening!
    else { 
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far, 
    if (button == LOW) return false; // Nothing happening!
    else { 
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else 
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else 
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    {  
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}


void setleds() {
  digitalWrite(led13Pin, led13State);
  digitalWrite(PredPin, PredState);
  digitalWrite(PgrnPin, PgrnState);
  digitalWrite(RredPin, RredState);
  digitalWrite(RyelPin, RyelState);
  digitalWrite(RgrnPin, RgrnState);
}

boolean ispress() { // One-directional read of button - sets but does not clear!
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    press = true;
  } 
  return(press);
}

void setup() {
  Serial.begin(9600);
  pinMode(led13Pin, OUTPUT);      
  pinMode(PgrnPin, OUTPUT);      
  pinMode(PredPin, OUTPUT);      
  pinMode(RgrnPin, OUTPUT);      
  pinMode(RyelPin, OUTPUT);      
  pinMode(RredPin, OUTPUT);      
  pinMode(button1, INPUT);      
  digitalWrite(button1,HIGH);        // internal pullup all versions
  press = false;
  Serial.println("Starting ...");
}

void loop() {
  // All red phase
  RredState = HIGH;
  RyelState = LOW; 
  RgrnState = LOW; 
  PredState = HIGH;
  PgrnState = LOW; 
  setleds();
  Serial.println("Red phase");
  setout(&count3);  
  while (!timeout(&count3, 3000UL )) {
    ispress();  // Check on the button
  }

  // Road Green
  RredState = LOW;
  RyelState = LOW; 
  RgrnState = HIGH; 
  PredState = HIGH;
  PgrnState = LOW; 
  setleds();
  Serial.println("Road green");
  setout(&count3);  
  while (!timeout(&count3, 8000UL )) { // Reasonable time on green
    ispress();  // Check on the button
  }
  Serial.println("Green stale, wait on button");

  while ( press == false )  // Now wait for the button 
  {
    if (timeout(&count2, 300UL )) {
      if (led13State == LOW) {
        led13State = HIGH;
      }
      else {
        led13State = LOW; 
      } 
      digitalWrite(led13Pin, led13State);
    }
    ispress();   
  }
  led13State = LOW; 
  digitalWrite(led13Pin, led13State);

  Serial.println("Button sensed");
  setout(&count3);  
  while (!timeout(&count3, 4000UL )) { // Do not respond immediately!
  }

  // Road Yellow
  RredState = LOW;
  RyelState = HIGH; 
  RgrnState = LOW; 
  PredState = HIGH;
  PgrnState = LOW; 
  setleds();
  Serial.println("Road yellow");
  setout(&count3);  
  while (!timeout(&count3, 5000UL )) {
  }

  // Road Red
  RredState = HIGH;
  RyelState = LOW; 
  RgrnState = LOW; 
  PredState = HIGH;
  PgrnState = LOW; 
  setleds();
  Serial.println("Road red");
  setout(&count3);  
  while (!timeout(&count3, 3000UL )) {
  }

  // Walk Green
  RredState = HIGH;
  RyelState = LOW; 
  RgrnState = LOW; 
  PredState = LOW;
  PgrnState = HIGH; 
  setleds();
  press = false;  
  Serial.println("Walk");
  setout(&count3);  
  while (!timeout(&count3, 6000UL )) {
  }

  // Flash Don't Walk
  RredState = HIGH;
  RyelState = LOW; 
  RgrnState = LOW; 
  PgrnState = LOW; 
  Serial.println("Flash Don't Walk");
  setout(&count3);  
  while (!timeout(&count3, 7000UL )) {
    if (timeout(&count2, 500UL )) {
      if (PredState == LOW) {
        PredState = HIGH;
      }
      else {
        PredState = LOW; 
      } 
      setleds();
    }
    ispress();  // Check on the button
  }

}

Am I able to use pin 1 for a button?

yes, but don't. Keep 0 and 1 for serial.

You can use A0 - A5 also for digitialWrite

noiasca:
Yes, but don't.

Beautifully put! :sunglasses:

Paul__B:
OK, you need to read up on this reference and this one to understand how to write multi-functional code. :sunglasses:

It is not appropriate to do this sort of thing in "straight line" code, you need for example to be able to have the blinking happen independently of the check for traffic to make the lights change.

Have a think about this code for a Pedestrian crossing:

im not too worried about the blinking as much as I am about being able to press a button to just represent that the sensor "detects a car".... so basically just change the side the side sensor is suppose to be on to green light.. which I assumed was possible with this type of code.. I have it where my project looks like traffic sequence like I'd like already now I just need to add a button to change pin 11 to high and10,7, and 2 to high when this button is clicked... but not just override it .. need the action its going through to complete first... is that not possible with this type of code?

sorry im extremely new and all the bs going on already havent been able to read and look things up as much as id like too..
appreciate all the answers so far.

Connect buttons from input to ground, use pinMode of INPUT_PULLUP and add an external pullup from the pin to 5 V if that becomes unreliable and you have actually implemented de-bouncing as in my code.

Paul__B:
Connect buttons from input to ground, use pinMode of INPUT_PULLUP and add an external pullup from the pin to 5 V if that becomes unreliable and you have actually implemented de-bouncing as in my code.

so if i connect my button from input pullup and to ground how do I go about making this Leds transition to the new light sequence? I havent implemented de-bouncing ... im not sure how too

Well, that is where it depends on the way you have implemented the code.

For your "straight line" code, you need to determine where in the sequence it needs to make the decision based on the state of the button and branch with an "if - else" statement to different sequences. I don't know how you can actually do that. The problem is the delay() statements which prevent anything else happening while they wait.

I have given you an example of partial "state machine" code which never uses "delay()" so the "while" loops enable the button to be monitored even as flashing lights are executed.

This is not the same as full "state machine" code, but is one step in the right direction. :grinning:

And while I include de-bouncing of the button, this is not strictly necessary here as the actual sequence is so much slower than contact bounce, but it happens to neatly incorporate the "state change" algorithm whereby if the button is held, it will not be acknowledged again until it has first been released.

is there a way I can pay someone to help me / or just do it?

I can take out a majority of this code it’s just a duplicate to somewhat change the sequence of the lighting to look like it’s left turn signal is on..

If you see where it says // new sequence
I can take it out from there...

Click "report to moderator" and ask that the topic be moved to "gigs and collaberations" . Then add a post saying how much you can pay. If no-one replies, double your offer. And repeat.