Traffic Lights- Project

I'm a newbie to coding

i need help on coding

I'm currently on a project on traffic lights system using LEDs

I have done the pattern and its successfully looping, but there's a function i need to do,

an OVERRIDE FUNCTION, the goal is to stop the current pattern and then changing it to only red lights on both sets with a push of a button (for as long as the button is held)

copy of the code below

//light 1
int red1= 13;
int yellow1= 12;
int green1= 11;

//light 2
int red2= 10;
int yellow2= 9;
int green2= 8;
//buttons
int button1 = 7;



void setup()
{ // light 1
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  
  //light 2
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  //buttons
  pinMode(button1, INPUT);
  
}

void loop() 
{
  digitalWrite(red1,1); 
  digitalWrite(yellow1,0);
  digitalWrite(green1,0);
  digitalWrite(red2,0); 
  digitalWrite(yellow2,0);
  digitalWrite(green2,1);
  delay(5000);
  
  digitalWrite(red1,1); 
  digitalWrite(yellow1,0);
  digitalWrite(green1,0);
  digitalWrite(red2,1); 
  digitalWrite(yellow2,0);
  digitalWrite(green2,0);
  delay(3000);
  
   digitalWrite(red1,1); 
  digitalWrite(yellow1,0);
  digitalWrite(green1,0);
  digitalWrite(red2,1); 
  digitalWrite(yellow2,1);
  digitalWrite(green2,0);
  delay(5000);
}

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

You've been given this assignment, so that you can discover how to write non-blocking code. It's a "boilerplate" school assignment. Was it covered in class? If not, you can find links about how to do that, in the sticky help threads at the top of the forum. It's all about using millis() instead of delay().

We only had one coding class and it was a pretty basic one where we made some leds flash, our teacher caught covid and couldnt teach us, even remotely. Most of my knowledge is from watching videos and following other people's work

Is it not possible to code it with only delays? from what i've gathered all i need to do is add an if function for pressing the button, but idk

No, it's impossible. That's the whole point of the exercise. It looks like you didn't even try the 'if' statement you mentioned... you've had some time to check out the links I mentioned, have you followed them at all?

Can you at least point me in the right direction? the threads you were talking about

BTW do you know of any good videos on the matter?

Thanks

ramble:
Can you at least point me in the right direction? the threads you were talking about

BTW do you know of any good videos on the matter?

Thanks

Kind of like these but they change from time to time:

Forget about videos! Most of them are pure garbage! Here is one that is not too bad...

Can you at least point me in the right direction?

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

So its just a cross street right? North/South [pattern 1] goes for awhile.
Then, East/West [pattern 2] goes for awhile..

Repeat?

Now they want to you add [pattern 3] when a button is pressed?

-jim lee

It does look like the traffic lights with a 1 suffix ( red1, yellow1 and green1) never change in the loop. Is that what is intended ?

an OVERRIDE FUNCTION, the goal is to stop the current pattern and then changing it to only red lights on both sets with a push of a button (for as long as the button is held)

That sounds like a rather simplified representation of real traffic lights. Normally, for example, a light on green would not go instantly to red, but would first go to yellow, then red.

To illustrate what you want to achieve, if may be good to draw a horizontal time line. Above it, draw 6 rows, one for each traffic light colour and start colouring them in to represent a complete, unbroken cycle. Do the same again, this time with the cycle broken by the temporary press of the override button. I've started it for you, based on your code, but it is clear that your code does not event attempt a full cycle of both sets of lights:

trafficLightCycle.JPG

trafficLightCycle.JPG

So i've updated the code and did the full pattern for both,

only thing now is the button isn't working

the button only works when i make it as long as the 1st part of the pattern,

why is that?

i know i shouldnt be using the delay function but im dumb and stubborn,

int SWITCH = 7;
int red1 = 13;
int yellow1 = 12;
int green1 = 11;

int buttonstate = 0;

void setup()
{ // light 1
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  
  //light 2
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  //buttons
  pinMode(7, INPUT);
  
}

void loop()
{
  buttonstate = digitalRead(7);
  if (buttonstate == HIGH) {
    digitalWrite(13, HIGH);
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    
  } else {
    digitalWrite(13, HIGH);  //1 button works if i cut the code just after this part
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    delay(1000);
      
    digitalWrite(13, HIGH);  //2
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    delay(5000);
      
    digitalWrite(13,HIGH);  //3
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    delay(5000); 
    
    digitalWrite(13,HIGH);  //4
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    delay(5000); 
    
    digitalWrite(13,HIGH);  //5
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    delay(5000); 
    
    
    digitalWrite(13,LOW);  //6
    digitalWrite(12, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    delay(5000); 
    
    digitalWrite(13,HIGH);  //7
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    delay(5000); 
    
  }
}

Nothing else can happen while a delay() runs. It shouldn't, really... I sympathize somewhat but the computer doesn't care. It just does what you tell it to do. You will never get this working without the techniques linked to in reply #7.

Would the 31 second delay periods during which time the input is not read have anything to do with it ?

Just an idea, to posssibly make your life easier..

Write 3 functions.

void eastWest(void) {

}

void northSouth(void) {

}

void allRed(void) {

}

Now you have a one line call to whatever pattern you want. Instead of all them digitalOut() calls cluttering up your loop() function that you are working on.

-jim lee

there is really just one pattern for the 6 LEDs so yeah i guess this would clean things up

This was offered some time ago:

https://forum.arduino.cc/index.php?action=dlattach;topic=685038.0;attach=365656

Should give you some ideas.

I'm a complete noob coder

lets say i've coded some LEDs to display a pattern and looped
a simple pattern using delay

how do i code it so a button push forces the led to display a different pattern (for as long as the button is held

int SWITCH = 7;
int red1 = 13;
int yellow1 = 12;
int green1 = 11;

int buttonstate = 0;

void setup()
{ // light 1
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  
  //light 2
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  //buttons
  pinMode(7, INPUT);
  
}

void loop()
{
  buttonstate = digitalRead(7);
  if (buttonstate == HIGH) {
    digitalWrite(13, HIGH);
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    
  } else {
    digitalWrite(13, HIGH);  //1
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    delay(1000);
      
    digitalWrite(13, HIGH);  //2
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    delay(5000);
      
    digitalWrite(13,HIGH);  //3
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    delay(5000); 
    
    digitalWrite(13,HIGH);  //4
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    delay(5000); 
    
    digitalWrite(13,HIGH);  //5
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    delay(5000); 
    
    
    digitalWrite(13,LOW);  //6
    digitalWrite(12, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    delay(5000); 
    
    digitalWrite(13,HIGH);  //7
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    delay(5000); 
    
  }
}

)

You can't. During a delay() there is nothing you can do.

You can start by reading the Blink Without Delay page: https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay.

There are a number of ways to program a certain sequence. Suppose the Blink-Without-Delay can activate something every second or every 5 seconds. Can you think of a way to go through a certain sequence ?
Since the delay() will be gone and the loop() will run repeatedly very fast, you need a variable that tells the code what to do.

O, I see. Hmmm, now I don't know what to think of it.
https://forum.arduino.cc/index.php?topic=721886.0

DUPLICATE TOPICS MERGED

Why did you start a new topic for the same problem ?