Stop Light Controller

I am very new to arduino and i was wondering if i could get help with a project i have a uno and a 4 relay board. I want to hook it up to a stop light so the green will light for about 2.5 seconds then turn off the the yellow right after then red. The only problem is that i am awful at the coding and dont really know what i am doing if someone could maybe come up with some code i could tweak to fit my parameters it would be very appreciated.

Try PMing @bluefin They’re often writing code for beginners in this section.

But even bluefin [sic], I guess that should be Blackfin?, will need to know the exact model of relay board, since there are many different ones and they don't all work the same. So one thing you should do is provide a link to the board's data sheet or at least its vendor's site.

21aholden: if i could get help with a project

Presumably each of the lights is on one of the relays, so a greenRelay, a yellowRelay and a redRelay kind of thing? Is it literally a case that you want it to cycle green-yellow-red and back to green?

Did you look at the blink example? I'd start there with just an led not the relay, and add two more leds... maybe you did start there, and if so, if you post the code you did so far and explain what it's doing right and wrong, folk will assist.

Since you don't tell us how the relays are turned on or off I assumed that pins on the Arduino would switch them through transistors of whatever. I included a couple of defines:

//you may need to swap these depending on how you're driving
//the light relays
#define LIGHT_ON    HIGH
#define LIGHT_OFF   LOW

that allows you to define what pin level turns the relay on and off. I also guessed at what your pin assignments are.

//define the pins on the Arduino controlling the relays
const byte pinRed = 2;
const byte pinYel = 3;
const byte pinGrn = 4;

//an array of the light pin numbers that makes it
//easy to access them
byte grTrafficLights[] = 
{
    pinRed,
    pinYel,
    pinGrn
};

//you may need to swap these depending on how you're driving
//the light relays
#define LIGHT_ON    HIGH
#define LIGHT_OFF   LOW

//a holder for the millis() count when timing the lights
unsigned long
    timeTrafficLight;

//keeps track of which light is on
//0=red, 1=yellow, 2=green
//after green is done, the count is set back to 0 (red)
byte
    activeLight = 0;

void setup() 
{
    //set up the pins and set the RED light on
    pinMode( pinRed, OUTPUT );
    digitalWrite( pinRed, LIGHT_ON );
    pinMode( pinYel, OUTPUT );
    digitalWrite( pinYel, LIGHT_OFF );
    pinMode( pinGrn, OUTPUT );
    digitalWrite( pinGrn, LIGHT_OFF );

    //grab the millis() count to start timing the red
    timeTrafficLight = millis();
    
}//setup

void loop() 
{
    //has 2.5-seconds (2500mS) passed?
    if( millis() - timeTrafficLight > 2500 )
    {
        //yes turn off the current light
        digitalWrite( grTrafficLights[activeLight], LIGHT_OFF );

        //bump the active light counter. If it goes over
        //2 (green) set it back to 0 (red)
        activeLight++;
        if( activeLight == 3 )
            activeLight = 0;

        //now turn on that light
        digitalWrite( grTrafficLights[activeLight], LIGHT_ON );
        //save this time for the next 2.5-sec period
        timeTrafficLight = millis();
                    
    }//if
    
}//loop

Next lesson will be a state-machine, which can accommodate cross streets, pedestrians and other variables.

(Actually a complex, but logically simple problem)

lastchancename: Next lesson ...

There hasn't been a lesson yet...

@OP you should still provide details of the relay hardware; the connections are not always intuitive, and it would be wise to get some advice on how to hook it up, or at least check your understanding. (Unless of course you know already for a fact how to do that.)

I have two lines of code i want to combine for a stoplight display the first code is

int relayredPin = 2;
int relayyelPin = 3;
int relaygrePin = 4;
void setup() {
pinMode(relayredPin,OUTPUT);  
pinMode(relayyelPin,OUTPUT);  
pinMode(relaygrePin,OUTPUT);  
}
void loop() {
  digitalWrite(relayredPin, LOW);
  digitalWrite(relayyelPin, LOW);
  digitalWrite(relaygrePin, LOW);
}

Then the second code

#define LIGHT_ON    LOW
#define LIGHT_OFF   HIGH
//define the pins on the Arduino controlling the relays
const byte pinRed = 2;
const byte pinYel = 3;
const byte pinGrn = 4;

//an array of the light pin numbers that makes it
//easy to access them
byte grTrafficLights[] = 
{
    pinRed,
    pinYel,
    pinGrn
};

//you may need to swap these depending on how you're driving
//the light relays
#define LIGHT_ON    LOW
#define LIGHT_OFF   HIGH

//a holder for the millis() count when timing the lights
unsigned long
    timeTrafficLight;

//keeps track of which light is on
//0=red, 1=yellow, 2=green
//after green is done, the count is set back to 0 (red)
byte
    activeLight = 0;

void setup() 
{
    //set up the pins and set the RED light on
    pinMode( pinRed, OUTPUT );
    digitalWrite( pinRed, LIGHT_ON );
    pinMode( pinYel, OUTPUT );
    digitalWrite( pinYel, LIGHT_OFF );
    pinMode( pinGrn, OUTPUT );
    digitalWrite( pinGrn, LIGHT_OFF );

    //grab the millis() count to start timing the red
    timeTrafficLight = millis();
    
}//setup

void loop() 
{
    //has 2.5-seconds (2500mS) passed?
    if( millis() - timeTrafficLight > 2500 )
    {
        //yes turn off the current light
        digitalWrite( grTrafficLights[activeLight], LIGHT_OFF );

        //bump the active light counter. If it goes over
        //2 (green) set it back to 0 (red)
        activeLight++;
        if( activeLight == 3 )
            activeLight = 0;

        //now turn on that light
        digitalWrite( grTrafficLights[activeLight], LIGHT_ON );
        //save this time for the next 2.5-sec period
        timeTrafficLight = millis();
                    
    }//if
    
}//loop

I would like to combine the two codes and have a button to switch between the two codes, the first code turns all 3 relays on then the second one makes one go on for 2.5 seconds then 2 right after then 3 doing the same. The problem is that i don't know how to combine the codes and make the button switch code could someone help me out thanks.

So i have two codes to run a traffic light for display. I want to combine these codes and use a button to switch between the two codes. This is the first code it has all 3 relays on.

int relayredPin = 2;
int relayyelPin = 3;
int relaygrePin = 4;
void setup() {
pinMode(relayredPin,OUTPUT);  
pinMode(relayyelPin,OUTPUT);  
pinMode(relaygrePin,OUTPUT);  
}
void loop() {
  digitalWrite(relayredPin, LOW);
  digitalWrite(relayyelPin, LOW);
  digitalWrite(relaygrePin, LOW);
}

This is the second code It runs the green on for 2.5 seconds then off then yellow the same then Red the same.

#define LIGHT_ON    LOW
#define LIGHT_OFF   HIGH
//define the pins on the Arduino controlling the relays
const byte pinRed = 2;
const byte pinYel = 3;
const byte pinGrn = 4;

//an array of the light pin numbers that makes it
//easy to access them
byte grTrafficLights[] = 
{
    pinRed,
    pinYel,
    pinGrn
};

//you may need to swap these depending on how you're driving
//the light relays
#define LIGHT_ON    LOW
#define LIGHT_OFF   HIGH

//a holder for the millis() count when timing the lights
unsigned long
    timeTrafficLight;

//keeps track of which light is on
//0=red, 1=yellow, 2=green
//after green is done, the count is set back to 0 (red)
byte
    activeLight = 0;

void setup() 
{
    //set up the pins and set the RED light on
    pinMode( pinRed, OUTPUT );
    digitalWrite( pinRed, LIGHT_ON );
    pinMode( pinYel, OUTPUT );
    digitalWrite( pinYel, LIGHT_OFF );
    pinMode( pinGrn, OUTPUT );
    digitalWrite( pinGrn, LIGHT_OFF );

    //grab the millis() count to start timing the red
    timeTrafficLight = millis();
    
}//setup

void loop() 
{
    //has 2.5-seconds (2500mS) passed?
    if( millis() - timeTrafficLight > 2500 )
    {
        //yes turn off the current light
        digitalWrite( grTrafficLights[activeLight], LIGHT_OFF );

        //bump the active light counter. If it goes over
        //2 (green) set it back to 0 (red)
        activeLight++;
        if( activeLight == 3 )
            activeLight = 0;

        //now turn on that light
        digitalWrite( grTrafficLights[activeLight], LIGHT_ON );
        //save this time for the next 2.5-sec period
        timeTrafficLight = millis();
                    
    }//if
    
}//loop

If anyone has any help to give me it would be very appreciated.

#define LIGHT_ON    LOW
#define LIGHT_OFF   HIGH

//define the pins on the Arduino controlling the relays
const byte pinRed = 2;
const byte pinYel = 3;
const byte pinGrn = 4;

//what the switch will do
#define FLASHING_SEQUENCE   HIGH
#define STATIC_ON           LOW
const byte pinSelSwitch = 5;    //select between flashing and static-on states

//an array of the light pin numbers that makes it
//easy to access them
byte grTrafficLights[] = 
{
    pinRed,
    pinYel,
    pinGrn
};

//you may need to swap these depending on how you're driving
//the light relays
#define LIGHT_ON    LOW
#define LIGHT_OFF   HIGH

//a holder for the millis() count when timing the lights
unsigned long
    timeTrafficLight;

//keeps track of which light is on
//0=red, 1=yellow, 2=green
//after green is done, the count is set back to 0 (red)
byte
    activeLight = 0;

byte
    lastSwitch;
bool
    bSelect;            //if true, lights flash; if false, they're on static
    
void setup() 
{
    //set up the pins and set the RED light on
    pinMode( pinRed, OUTPUT );
    pinMode( pinYel, OUTPUT );
    pinMode( pinGrn, OUTPUT );
    InitLamps();
    
    pinMode( pinSelSwitch, INPUT_PULLUP );
    lastSwitch = digitalRead( pinSelSwitch );
    
    //enable the flashing out of reset
    bSelect = true;
    
}//setup

void loop() 
{
    byte
        currSw;
    
    currSw = digitalRead( pinSelSwitch );
    if( currSw != lastSwitch )
    {
        lastSwitch = currSw;
        if( currSw == STATIC_ON )
        {
            LampsOn();
            bSelect = false;
            
        }//if
        else
        {
            //FLASHING_SEQUENCE
            InitLamps();       
            bSelect = true;
            
        }//else
        
    }//if

    //if flashing is enabled, do it
    if( bSelect )
        DoTrafficLights();
    
}//loop

void InitLamps( void )
{
    //init lights and timer for sequenced blinking
    digitalWrite( pinRed, LIGHT_ON );
    digitalWrite( pinYel, LIGHT_OFF );
    digitalWrite( pinGrn, LIGHT_OFF );
    timeTrafficLight = millis();
    
}//InitLamps

void LampsOn( void )
{
    //turn all the lights on
    digitalWrite( pinRed, LIGHT_ON );
    digitalWrite( pinYel, LIGHT_ON );
    digitalWrite( pinGrn, LIGHT_ON );
    
}//LampsOn

void DoTrafficLights( void )
{
    //has 2.5-seconds (2500mS) passed?
    if( millis() - timeTrafficLight > 2500 )
    {
        //yes turn off the current light
        digitalWrite( grTrafficLights[activeLight], LIGHT_OFF );

        //bump the active light counter. If it goes over
        //2 (green) set it back to 0 (red)
        activeLight++;
        if( activeLight == 3 )
            activeLight = 0;

        //now turn on that light
        digitalWrite( grTrafficLights[activeLight], LIGHT_ON );
        //save this time for the next 2.5-sec period
        timeTrafficLight = millis();
                    
    }//if
    
}

Thanks a lot blackfin one last question what pins will the button be connect to?

21aholden: Thanks a lot blackfin one last question what pins will the button be connect to?

Check the code:

//what the switch will do
#define FLASHING_SEQUENCE   HIGH
#define STATIC_ON           LOW
const byte pinSelSwitch = 5;    //select between flashing and static-on states

I assigned it arbitrarily to pin 5.

Also note that the code expects the switch to be open when released; the internal pull-up will pull the pin high. When the switch is closed, it will ground the pin. If you do something different you'll have to update the code to match.

@21aholden, do not cross-post. Threads merged. Twice.

One last thing i want to use an ir remote and receiver as the button and i can’t seem to properly combine the codes. The ir code works well for just a led right now. !Thanks for any help!

This is the first code for the traffic light.

#define LIGHT_ON    LOW
#define LIGHT_OFF   HIGH

//define the pins on the Arduino controlling the relays
const byte pinRed = 2;
const byte pinYel = 3;
const byte pinGrn = 4;

//what the switch will do
#define FLASHING_SEQUENCE   HIGH
#define STATIC_ON           LOW
const byte pinSelSwitch = 5;    //select between flashing and static-on states

//an array of the light pin numbers that makes it
//easy to access them
byte grTrafficLights[] = 
{
    pinRed,
    pinYel,
    pinGrn
};

//you may need to swap these depending on how you're driving
//the light relays
#define LIGHT_ON    LOW
#define LIGHT_OFF   HIGH

//a holder for the millis() count when timing the lights
unsigned long
    timeTrafficLight;

//keeps track of which light is on
//0=red, 1=yellow, 2=green
//after green is done, the count is set back to 0 (red)
byte
    activeLight = 0;

byte
    lastSwitch;
bool
    bSelect;            //if true, lights flash; if false, they're on static
    
void setup() 
{
    //set up the pins and set the RED light on
    pinMode( pinRed, OUTPUT );
    pinMode( pinYel, OUTPUT );
    pinMode( pinGrn, OUTPUT );
    InitLamps();
    
    pinMode( pinSelSwitch, INPUT_PULLUP );
    lastSwitch = digitalRead( pinSelSwitch );
    
    //enable the flashing out of reset
    bSelect = true;
    
}//setup

void loop() 
{
    byte
        currSw;
    
    currSw = digitalRead( pinSelSwitch );
    if( currSw != lastSwitch )
    {
        lastSwitch = currSw;
        if( currSw == STATIC_ON )
        {
            LampsOn();
            bSelect = false;
            
        }//if
        else
        {
            //FLASHING_SEQUENCE
            InitLamps();       
            bSelect = true;
            
        }//else
        
    }//if

    //if flashing is enabled, do it
    if( bSelect )
        DoTrafficLights();
    
}//loop

void InitLamps( void )
{
    //init lights and timer for sequenced blinking
    digitalWrite( pinRed, LIGHT_ON );
    digitalWrite( pinYel, LIGHT_OFF );
    digitalWrite( pinGrn, LIGHT_OFF );
    timeTrafficLight = millis();
    
}//InitLamps

void LampsOn( void )
{
    //turn all the lights on
    digitalWrite( pinRed, LIGHT_ON );
    digitalWrite( pinYel, LIGHT_ON );
    digitalWrite( pinGrn, LIGHT_ON );
    
}//LampsOn

void DoTrafficLights( void )
{
    //has 2.5-seconds (2500mS) passed?
    if( millis() - timeTrafficLight > 2500 )
    {
        //yes turn off the current light
        digitalWrite( grTrafficLights[activeLight], LIGHT_OFF );

        //bump the active light counter. If it goes over
        //2 (green) set it back to 0 (red)
        activeLight++;
        if( activeLight == 3 )
            activeLight = 0;

        //now turn on that light
        digitalWrite( grTrafficLights[activeLight], LIGHT_ON );
        //save this time for the next 2.5-sec period
        timeTrafficLight = millis();
                    
    }//if
    
}

This is the ir receiver code.

#include <IRremote.h> //include the library
#define Button_1 0xFF18E7
#define Button_2 0xFF4AB5
int receiver = 13; //initialize pin 13 as recevier pin.
uint32_t Previous; 
IRrecv irrecv(receiver); //create a new instance of receiver
decode_results results;


void setup() {
 Serial.begin(9600);
 irrecv.enableIRIn(); //start the receiver
pinMode(12, OUTPUT);
}
void loop() {
 if (irrecv.decode(&results)) { //if we have received an IR signal
  if (results.value==0xFFFFFFFF) { 
      results.value=Previous;
      }

    switch(results.value) {
           case Button_1 : digitalWrite(12, HIGH); break;
           case Button_2 : digitalWrite(12, LOW); break;
           
      } 
 Serial.println (results.value, HEX); //display HEX results 
 irrecv.resume(); //next value
 }
 Previous=results.value;
 }

Compiles, not tested.

Button 1 should set the static display while button 2 should start the blinking pattern.

#include <IRremote.h>
       
//IR stuff
const int receiver = 13;          
IRrecv irrecv(receiver);            
decode_results results;
#define Button_1 0xFF18E7
#define Button_2 0xFF4AB5

#define LIGHT_ON    LOW
#define LIGHT_OFF   HIGH

//define the pins on the Arduino controlling the relays
const byte pinRed = 2;
const byte pinYel = 3;
const byte pinGrn = 4;

//what the switch will do
#define FLASHING_SEQUENCE   HIGH
#define STATIC_ON           LOW
const byte pinSelSwitch = 5;    //select between flashing and static-on states

//an array of the light pin numbers that makes it
//easy to access them
byte grTrafficLights[] = 
{
    pinRed,
    pinYel,
    pinGrn
};

//you may need to swap these depending on how you're driving
//the light relays
#define LIGHT_ON    LOW
#define LIGHT_OFF   HIGH

//a holder for the millis() count when timing the lights
unsigned long
    timeTrafficLight;

//keeps track of which light is on
//0=red, 1=yellow, 2=green
//after green is done, the count is set back to 0 (red)
byte
    activeLight = 0;

byte
    lastSwitch;
bool
    bSelect;            //if true, lights flash; if false, they're on static
    
void setup() 
{
    irrecv.enableIRIn(); //start the receiver
    
    //set up the pins and set the RED light on
    pinMode( pinRed, OUTPUT );
    pinMode( pinYel, OUTPUT );
    pinMode( pinGrn, OUTPUT );
    InitLamps();
    
    pinMode( pinSelSwitch, INPUT_PULLUP );
    lastSwitch = digitalRead( pinSelSwitch );
    
    //enable the flashing out of reset
    bSelect = true;
    
}//setup

void loop() 
{
    byte
        bChangeFlag; 
    byte
        currSw;

    bChangeFlag = 0;
    if( irrecv.decode(&results) ) 
    {
        irrecv.resume(); 
        
        //if we have received an IR signal
        //is it button 1?
        if( results.value == Button_1 )
            bChangeFlag = 0x01;
        else if(  results.value == Button_2 )
            bChangeFlag = 0x02;
            
    }//if

    currSw = digitalRead( pinSelSwitch );
    if( currSw != lastSwitch )
    {
        lastSwitch == currSw;
        bChangeFlag = 0x03;
        
    }//if

    if( bChangeFlag == 0x01 || ((bChangeFlag == 0x03) && (currSw == STATIC_ON)) ) 
    {
        //IR button 1
        LampsOn();
        bSelect = false;
        
    }//if
    else if ( bChangeFlag == 0x02 || ((bChangeFlag == 0x03) && (currSw == FLASHING_SEQUENCE)) ) 
    {
        //IR button 2
        //FLASHING_SEQUENCE
        InitLamps();       
        bSelect = true;
        
    }//if
    
    //if flashing is enabled, do it
    if( bSelect )
        DoTrafficLights();
    
}//loop

void InitLamps( void )
{
    //init lights and timer for sequenced blinking
    digitalWrite( pinRed, LIGHT_ON );
    digitalWrite( pinYel, LIGHT_OFF );
    digitalWrite( pinGrn, LIGHT_OFF );
    timeTrafficLight = millis();
    
}//InitLamps

void LampsOn( void )
{
    //turn all the lights on
    digitalWrite( pinRed, LIGHT_ON );
    digitalWrite( pinYel, LIGHT_ON );
    digitalWrite( pinGrn, LIGHT_ON );
    
}//LampsOn

void DoTrafficLights( void )
{
    //has 2.5-seconds (2500mS) passed?
    if( millis() - timeTrafficLight > 2500 )
    {
        //yes turn off the current light
        digitalWrite( grTrafficLights[activeLight], LIGHT_OFF );

        //bump the active light counter. If it goes over
        //2 (green) set it back to 0 (red)
        activeLight++;
        if( activeLight == 3 )
            activeLight = 0;

        //now turn on that light
        digitalWrite( grTrafficLights[activeLight], LIGHT_ON );
        //save this time for the next 2.5-sec period
        timeTrafficLight = millis();
                    
    }//if
    
}//loop