Trafiic Lights - programming

Hi guys, i'm trying to make traffic lights project and i stopped in one moment of programming :confused:
as you can see in loop section i have two "sequences" that are turning on and off light.
Can i make appeal to this sequence in any shorter way in the next lines of code? :wink:

Thanks for all help
(yup am i begginer :v)

Traffic_Lights.ino (987 Bytes)

(deleted)

Please upload the code into a post here and make sure that you select the code in the forum editor and click the code tags icon (</>) top/left of the editor before saving the message. It makes it much easier to deal with when reading and copying it.

I couldn't resist having a look

  digitalWrite(red1, HIGH);   //RED  ON 
  digitalWrite(orange1, HIGH);//ORANGE  ON
  digitalWrite(red1, LOW);    //RED OFF
  digitalWrite(green1, HIGH); //GREEN ON
  digitalWrite(orange1, LOW); //ORANGE OFF

How long do you expect each light to be on ?

Note the use of code tags.

spycatcher2k:
Oh dear - looks like a current school / college / uni project is 'make a set of traffic lights with a pedestrian controlled crossing.'

I for one will not do any one elses homework. Had enough of this doing my college time.

It's not homework... it's just a project to take more good marks :v if you don't want to help in finding and helping me to make it faster please don't comment :wink:

UKHeliBob:
Please upload the code into a post here and make sure that you select the code in the forum editor and click the code tags icon (</>) top/left of the editor before saving the message. It makes it much easier to deal with when reading and copying it.

int red1 = 12;
int orange1 = 11;
int green1= 10;
int arrow1 = 9;
int red2 = 8;
int orange2= 7;
int green2= 6;
int arrow2= 5;
int red3 = 4;
int orange3 = 3;
int green3 = 2;

void setup() {
  pinMode(red1, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(red3, OUTPUT);
  pinMode(orange1, OUTPUT);
  pinMode(orange2, OUTPUT);
  pinMode(orange3, OUTPUT);
  pinMode(green1, OUTPUT);
  pinMode(green2, OUTPUT);
  pinMode(green3, OUTPUT);
  pinMode(arrow1, OUTPUT);
  pinMode(arrow2, OUTPUT);
}

void loop() {
 
// TURNING ON FIRST LIGHT
  
  digitalWrite(red1, HIGH);   //RED  ON 
  digitalWrite(orange1, HIGH);//ORANGE  ON
  digitalWrite(red1, LOW);    //RED OFF
  digitalWrite(green1, HIGH); //GREEN ON
  digitalWrite(orange1, LOW); //ORANGE OFF

// TURNING OFF FIRST LIGHT
  
  digitalWrite(green1, HIGH);  //GREEN ON
  digitalWrite(orange1, HIGH); //ORANGE ON
  digitalWrite(green1, LOW);   //GREEN OFF
  digitalWrite(red1, HIGH);    //RED ON
  digitalWrite(orange1, LOW);  //ORANGE OFF
  
}

UKHeliBob:
I couldn't resist having a look

  digitalWrite(red1, HIGH);   //RED  ON 

digitalWrite(orange1, HIGH);//ORANGE  ON
  digitalWrite(red1, LOW);    //RED OFF
  digitalWrite(green1, HIGH); //GREEN ON
  digitalWrite(orange1, LOW); //ORANGE OFF


How long do you expect each light to be on ?

Note the use of code tags.

i will be adding delays later :wink: now i'm looking to make it faster in coding

(deleted)

szymi2k16:
i will be adding delays later :wink:

and

szymi2k16:
it's just a project to take more good marks

are mutually contradictory.

i will be adding delays later :wink: now i'm looking to make it faster in coding

Wrong approach. Write small but complete sections of code and test them as you go.

You may want to consider whether using delay() is a good idea if there are pedestrian buttons to be read at any time during the light sequence. Using millis(), as in the BlinkWithoutDelay example and Several things at the same time offers a way of keeping the program responsive to inputs.

Even as the OP is a beginner, I'm enjoying this thread...!

Once he gets zero marks in class, I wonder what the next question will be..?
How do I Google for "Arduino traffic lights" ?

I'm not usually this harsh, but everything about this thread is arse-about!

I was just looking for sth like that ;v THANKS for very friendly Help ;3

void zapal1() {
 digitalWrite(redl, HIGH); //Zapala siÄ™ czerwone na 1
 digitalWrite(orangel, HIGH);//Zapala siÄ™ pomaracznowe na 1
 digitalWrite(redl, LOW); //Gaśnie czerwone na 1
 digitalWrite(greenl, HIGH); //Zapala siÄ™ zielone na 1
 digitalWrite(orangel, LOW); //Gaśnie pomarańczowe na 1
}

Looking at your code, and other 'identical' posts - it seems that you didn't get as far as even trying to write any code.

Your example looks a lot like the method instructions copied from your teacher's notes.

think state machine:

this does nothing with lights and uses an 'x' sent over the serial to simulate a button press, but you should be able to get an A+ if you can figure it out!

#define LIGHT_INTERVAL 5000 // 5 seconds

enum TrafficState{
  GREEN,
  YELLOW,
  RED,
  PEDX
};

TrafficState state, lastState = PEDX;
unsigned long lastMillis;
bool buttonPressed = false;

void setup()
{
  Serial.begin(9600);
}

void loop() 
{
  if(state != lastState)
  {
    switch (state)
    {
      case GREEN:
        Serial.println(F("GREEN"));
        break;
      case YELLOW:
        Serial.println(F("YELLOW"));
        break;
      case RED:
        Serial.println(F("RED"));
        break;
      case PEDX:
        Serial.println(F("PEDESTRIAN CROSSING"));
      buttonPressed = false;
        break;
    }
  }
  lastState = state;
  if(millis() - lastMillis > LIGHT_INTERVAL)
  {
    int i = static_cast<int>(state);
    state = static_cast<TrafficState>((i + 1) % 4);
    if(!buttonPressed && state == PEDX)
    {
      state = GREEN;
    }
    lastMillis += LIGHT_INTERVAL;
  }
  if(Serial.available())
  {
    if(Serial.read() == 'x')
    {
      buttonPressed = true;
    }
  }
}