Traffic Lights at an intersection

Hello!

I am trying to get traffic lights to work on my lego city build. I've written a code with millis instead of delays so I can use a button that gives a fast green light to a bus lane. But my code gets stuck on 2 green lights and 2 red lights constantly...

this here is my code:

int Red1_pin = 12;
int Yellow1_pin = 11;
int Green1_pin = 10;

int Red2_pin = 9;
int Yellow2_pin = 8;
int Green2_pin = 7;

int Bus_pin = 6;

unsigned long starttime;
unsigned long currenttime;

int state = 1; 

void setup()
{
	pinMode(Red1_pin, OUTPUT);
	pinMode(Yellow1_pin, OUTPUT);
	pinMode(Green1_pin, OUTPUT);

	pinMode(Red2_pin, OUTPUT);
	pinMode(Yellow2_pin, OUTPUT);
	pinMode(Green2_pin, OUTPUT);

	pinMode(Bus_pin, INPUT);

	starttime = millis();
}

void loop()
{
  if (state == 1) {
    digitalWrite(Yellow1_pin, LOW);
    digitalWrite(Red1_pin, HIGH);
    digitalWrite(Red2_pin, HIGH);

    currenttime = millis();
    if(currenttime - starttime >= 2000) {
      state = 2;
      starttime = millis();
    }
  }

  if(state == 2) {
    digitalWrite(Green1_pin, HIGH);
    digitalWrite(Red1_pin, LOW);

    currenttime = millis();
    if(currenttime - starttime >= 8000) {
      state = 3;
      starttime = millis();
    }
  }
    
  if (state == 3) {
    digitalWrite(Yellow1_pin , HIGH);
    digitalWrite(Green1_pin , LOW);

    if(digitalRead(Bus_pin) == HIGH) {
       
      state = 4;
      starttime = millis();
    }
    currenttime = millis();
    if(currenttime - starttime >=2000) {
      state = 4;
      starttime = millis();
    }
  }
    
  if (state == 4){
    digitalWrite(Red1_pin, HIGH);
    digitalWrite(Yellow2_pin, LOW);

    if(digitalRead(Bus_pin) == HIGH) {
       
      state = 5;
      starttime = millis();
    }
    currenttime = millis();
    if(currenttime - starttime >=2000) {
      state = 5;
      starttime = millis();
    }
  }
  if(state == 5){
    digitalWrite(Green2_pin, HIGH);
    digitalWrite(Red1_pin, LOW);

    currenttime = millis();
    if(currenttime - starttime >=10000) {
      state = 6;
      starttime = millis();
    }
  }
  if (state == 6){
    digitalWrite(Red1_pin, HIGH);
    digitalWrite(Red2_pin, HIGH);

    if(digitalRead(Bus_pin) == HIGH) {
       
      state = 1;
      starttime = millis();
    }
    currenttime = millis();
    if(currenttime - starttime >=2000) {
      state = 1;
      starttime = millis();
    }
  }  
}

I have no clue what causes the problem there for my question to you guys, can you help me?

sorry for english, not my motherlanguage

I think you might have your LEDs wired "ON" always... because your code is turning RED and GREEN off (as it should).

Hello licolada

Insert a logic analyzer in the sketch calling Serial.println()´s at POI to check the logic of the sequencer.

Have a nice day and enjoy coding in C++.

Hi!

I'm not sure about the complete logic when the button is pressed so play with this code.

#define INTERVAL_2k 0
#define INTERVAL_8k 1
#define INTERVAL_10k 2

#include <Arduino.h>
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/archive/refs/heads/master.zip

const byte Red1_pin = 9;
const byte Yellow1_pin = 7;
const byte Green1_pin = 8;

const byte Red2_pin = 6;
const byte Yellow2_pin = 5;
const byte Green2_pin = 4;

const byte Bus_pin = 3;

const byte numIntervals = 3;
const unsigned int interval[numIntervals] = {2000, 8000, 10000};
byte intervalIndex = 0;
byte state = 1;
unsigned long starttime;
bool print = false;
bool pressedOneTime = false;

Bounce2::Button button = Bounce2::Button();

void printDebug(bool start = false)
{
  if (start == true)
  {
    Serial.println("Starting...");
  }

  Serial.print("\nState: ");
  Serial.print(state);
  Serial.print(", Interval: ");
  Serial.println(interval[intervalIndex]);
}

void controlLightsOfSectionOne(bool red1, bool yellow1, bool green1)
{
  digitalWrite(Red1_pin, red1);
  digitalWrite(Yellow1_pin, yellow1);
  digitalWrite(Green1_pin, green1);
}

void controlLightsOfSectionTwo(bool red2, bool yellow2, bool green2)
{
  digitalWrite(Red2_pin, red2);
  digitalWrite(Yellow2_pin, yellow2);
  digitalWrite(Green2_pin, green2);
}

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

  button.attach(Bus_pin, INPUT_PULLUP);
  button.interval(5);
  button.setPressedState(LOW);

  pinMode(Red1_pin, OUTPUT);
  pinMode(Yellow1_pin, OUTPUT);
  pinMode(Green1_pin, OUTPUT);

  pinMode(Red2_pin, OUTPUT);
  pinMode(Yellow2_pin, OUTPUT);
  pinMode(Green2_pin, OUTPUT);

  controlLightsOfSectionOne(LOW, LOW, LOW); // All lights OFF
  controlLightsOfSectionTwo(LOW, LOW, LOW); // All lights OFF

  printDebug(true);

  starttime = millis();
}

void loop()
{
  if ((millis() - starttime) >= interval[intervalIndex])
  {
    state++;

    if (state > 5)
    {
      state = 2;
    }
    starttime = millis();

    print = true;
  }

  button.update();

  if (button.pressed())
  {
    Serial.println("Button Pressed!");

    if ((pressedOneTime == false) && (state < 3))
    {
      pressedOneTime = true;
      state = 3;
      print = true;
      starttime = millis();
    }
  }

  switch (state)
  {
    case 1:                                        // Idle state
      {
        intervalIndex = INTERVAL_2k;
        controlLightsOfSectionOne(HIGH, LOW, LOW); // Red ON, yellow and green OFF
        controlLightsOfSectionTwo(HIGH, LOW, LOW); // Red ON, yellow and green OFF
        break;
      }
    case 2: // Green for section 1 and red for section 2
      {
        intervalIndex = INTERVAL_8k;
        controlLightsOfSectionOne(LOW, LOW, HIGH);
        controlLightsOfSectionTwo(HIGH, LOW, LOW);
        pressedOneTime = false;
        break;
      }

    case 3: // yellow for section 1 and red for section 2
      {
        if(pressedOneTime == true)
        {
          Serial.println("Fast green enabled");
        }
        intervalIndex = INTERVAL_2k;
        controlLightsOfSectionOne(LOW, HIGH, LOW);
        controlLightsOfSectionTwo(HIGH, LOW, LOW);
        break;
      }

    case 4: // Red for section 1 and green for section 2
      {
        if(pressedOneTime == true)
        {
          intervalIndex = INTERVAL_2k;
        }
        else
        {
          intervalIndex = INTERVAL_10k;
        }
        controlLightsOfSectionOne(HIGH, LOW, LOW);
        controlLightsOfSectionTwo(LOW, LOW, HIGH);
        break;
      }
    case 5: // Yellow for section 2 and red for section 1
      {
        intervalIndex = INTERVAL_2k;
        controlLightsOfSectionOne(HIGH, LOW, LOW);
        controlLightsOfSectionTwo(LOW, HIGH, LOW);
        break;
      }
    default:
      {
        break;
      }
  }

  if(print == true)
  {
    print = false;
    printDebug();
  }
}

Best regards.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.