Traffic Light Controller

So I'm building a traffic light controller for a traffic light I just happen to have laying around my house. I have a code that does various patterns on the lights which works fine on LEDs but I'm having trouble on the large scale design. The patterns are cycled by pressing a switch that calls an interrupt and increments a counter. I've tried connecting the UNO to a darlington array ( ULN2803APG) that in turn enables a 12 volt supply to close relays and turn on certain lights but I've been running into issues. The main problem is that when I connect my circuit to 120 AC, the switch is pulled high and low without a toggle. Any suggestions? Once again, the circuitry and code works when used on a small scale model.

I'm trying to figure out a sturdy solution so if you have any suggestions I would greatly appreciate them. I have attached a crude schematic for reference.

Your schematic shows all the relay coils connected together but not connected to Ground.

The switch is wired wrong. You connect the pull-down resistor from Ground to the input pin. Not from Ground to a third terminal on the switch.

Also you don't need those 6 470 ohm resistors wired between the arduino output pins and the driver input pins, the driver chip already has built in series resistors and is designed for direct TTL logic level input voltage.

Lefty

The patterns are cycled by pressing a switch that calls an interrupt and increments a counter.

Let me guess. Lots of delay()s, so polling the switch is too slow. Time to rewrite the code to eliminate the delay()s and the interrupt.

Thanks for all the input, sorry my diagram sucks so much but the relay coils are connected to ground in the actual circuit.

And yeah, its a bunch of delay()s controlled by variable resister input so i can change the speed of each pattern. I'm not too picky as long as it works. Although I am curious how I would do it without using delay. here is one of the patterns

     if(buttonCount==0)//light pattern 1: scrolling down
     {
       sensorValue = analogRead(analogInPin);
       if(cnt1==0)
       {
         allOff(digitalG,digitalG2,digitalG3,digitalY,digitalY2,digitalY3,digitalR,digitalR2,digitalR3);
         digitalWrite(digitalG,HIGH);
         digitalWrite(digitalG2,HIGH);
         digitalWrite(digitalG3,HIGH);
         cnt1++;
       }
       else if(cnt1==1)
       {
         delay(20*sensorValue);
         allOff(digitalG,digitalG2,digitalG3,digitalY,digitalY2,digitalY3,digitalR,digitalR2,digitalR3);
         digitalWrite(digitalY,HIGH);
         digitalWrite(digitalY2,HIGH);
         digitalWrite(digitalY3,HIGH);
         cnt1++;
       }
       else if(cnt1==2)
       {
         delay(20*sensorValue);
         allOff(digitalG,digitalG2,digitalG3,digitalY,digitalY2,digitalY3,digitalR,digitalR2,digitalR3);
         digitalWrite(digitalR,HIGH);
         digitalWrite(digitalR2,HIGH);
         digitalWrite(digitalR3,HIGH);
         cnt1++;
       }
       else if (cnt1 ==3)
       {
         delay(20*sensorValue);
         allOff(digitalG,digitalG2,digitalG3,digitalY,digitalY2,digitalY3,digitalR,digitalR2,digitalR3);
         cnt1++;
       }
       else
       {
         cnt1=0;//reset counter
       }
        
     }

Although I am curious how I would do it without using delay.

Let us all intone the mantra - " look at the blink without delay example ". Ommmmm.

Let us all intone the mantra - " look at the blink without delay example ". Ommmmm.

Ommmmm.

OK, now that we have that out of the way, toss the Arduino in the corner for a while. YOU are going to take its place. You have a bunch of switches in front of you, a stopwatch, with the stop button broken off, and a pad of paper. How would YOU cycle the lights in the proper order?

The Arduino knows how to toggles pins, turning the lights on and off. The millis() function acts like the broken stopwatch, incrementing continuously, until it rolls around after 49+ days. The variables that the Arduino reads from/writes to take the place of paper.

The code that you write replaces you.

When YOU can define the steps you follow, you have a set of requirements that is easy to translate into code.

If you get stuck, think in terms of a state machine. There are a number of states - "Light is red", "Light is yellow", "Light is red", "Traffic waiting", "Pedestrian waiting", etc.

There are things that happen when a transition from one state to another occurs. Changing from "Light is yellow" to "Light is red" should cause certain (obvious) things to happen.

When to perform each transition is typically defined in terms of time (the light stays green for n seconds) or external activity (a pedestrian pushed a switch, a car triggered a sensor, etc.).

Having the Arduino implement that state machine is pretty easy, once you know all the possible states, which states can transition to other states (yellow to red is allowed; green to red is not), when to perform the transition, and what to do when the transition occurs.

Thanks again, I do know how the millis() function works though. I meant specifically with timing being controlled by the potentiometer input. But I think it would work just fine if I just used what is the delay in my code as the interval in a similar code using millis().

But I think it would work just fine if I just used what is the delay in my code as the interval in a similar code using millis().

Yes, it would.