Variation of Traffic Light Controller

Hi all, first time poster. Looks like a ton of useful information and helpful people here. I'm a newby to the Ardino, but did some BASIC programming back in the 80's, so some of the code makes a little sense to this old fart.

I have an Elegoo Uno R3 starter kit and have a mock up of my project on a breadboard and the code copied below.

Here is my objective: I want to build a sort of traffic light (red, yellow, green) to use at autocrosses to stage drivers. The "normal" status will be red light on, green & yellow off. When a car activates a photo cell, red light goes off and yellow light comes on. When the starter presses a momentary button, the yellow and red light are off and the green light goes on and stays on for a short time (maybe 5 seconds), then returns to "normal" status of red light on, green and yellow off.

My mock up hardware and code kind of work. I hold down the miniature button to simulate a car staged. I then touch the "go" button and everything works as it's supposed to.

Here's my problem: I have two different photo cells. One is manufactured by ALGE and is normally open and goes to closed when the beam is blocked. If I replace the miniature button with this photocell, all is well. The other photocell is manufactured by TagHeuer and is normally open and only gives a momentary close when the beam is blocked. I would prefer to use the TagHeuer light for this project and save my ALGE light for my timing equipment.

I assume I need to code in a toggle function. I have played with that and watched a couple tutorials several times, but I just can't seem to figure out how to make all this work together.

My schematic is pretty simple - I'll draw a little neater one and post.

Thanks in advance for any help and please go gentle.

// constants won't change. They're used here to set pin numbers:
const int stagebuttonPin = 4;                  // the number of the pushbutton pins
const int gobuttonPin = 2;
const int redledPin =  12;                    // the number of the LED pins
const int yellowPin = 13;
const int grnPin = 11;

int stagebuttonState;
int gobuttonState;

void setup() {

  pinMode(redledPin, OUTPUT);                 // initialize the LEDs pin as an output:
  pinMode(yellowPin, OUTPUT);
  pinMode(grnPin, OUTPUT);

  pinMode(stagebuttonPin, INPUT);              // initialize the pushbutton pin as an input:
  pinMode(gobuttonPin, INPUT);

}

void loop() {

  stagebuttonState = digitalRead(stagebuttonPin);                      // read the state of the pushbutton value:
  gobuttonState = digitalRead(gobuttonPin);

  if (stagebuttonState == 0 && gobuttonState == 0) {                  // normal state, red led on,

    digitalWrite(redledPin, HIGH);
    digitalWrite(yellowPin, LOW);
    digitalWrite(grnPin, LOW);
  }


  if (stagebuttonState == 1 && gobuttonState == 0) {                  // staged state, yellow led on,

    digitalWrite(redledPin, LOW);
    digitalWrite(yellowPin, HIGH);
    digitalWrite(grnPin, LOW);
  }


  if (stagebuttonState == 1 && gobuttonState == 1) {                // go button pushed, grn led on for 5 secs, then back to normal state

    digitalWrite(redledPin, LOW);
    digitalWrite(yellowPin, LOW);
    digitalWrite(grnPin, HIGH);
    delay (5000);
    digitalWrite(redledPin, HIGH);
    digitalWrite(yellowPin, LOW);
    digitalWrite(grnPin, LOW);
  }
}

How long does the sensor give you to detect it's high? If it was super fast, an interrupt might be called for, but I doubt it.

You need to have loop doing nothing but look for the pulse when you're waiting for a car.

To that end, have a boolean variable called CarDetected or some such. False to start. Have an if statement that checks that variable in loop. if it's false, just digitalRead the pin and set the variable when you see something. If it's true, do all your other stuff and set it false at the end.

Here's a quick schematic:

20210120_155537[1].jpg

Thanks WildBill,

I get the concept - just haven't been able to get it to work yet. I'm not 100% sure I'm getting the loop correct. I'm going to keep trying and put some time in on this today. I won't let it beat me!

Maybe start with something that just detects the pulse and tells you about it over serial.

Thank you so much WIldBill - I think I got it!!!

I did just what you said - I started from scratch and worked just with my staging button and printed the status to the serial monitor. Once I got that working and understood why, I just added the rest.

Again, thank you.

If the photocell goes ON momentary when it sees something and you want it to stay ON, you can use a SET RESET using interrupt input and program two ISR, one for leading edge to SET and one for falling edge to RESET.

I'll bet those light bulbs are dim, with 330k resistors in series with them.

330K resistors - you know, I read your reply and thought: No, they worked just fine. Then it finally sunk in that I had written 330K and not 330 ohms. I think I need another beer.

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