Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

C program for traffic light control

LiltLilt Posts: 7Member
Hi,
I'm trying to write a code for a traffic light control where red would delay for 10secs, Amber for 4 secs and Green for 15secs and the cycle starts over again. Please Does any one have a code for traffic lights control that I can work on. My program will be working in VxWorks platform (Tornado - Real time operating system)

Thank you ;-)

Comments

  • tom_swtom_sw Posts: 828Member
    [b][red]This message was edited by tom_sw at 2006-1-23 12:9:14[/red][/b][hr]
    : Hi,
    : I'm trying to write a code for a traffic light control where red would delay for 10secs, Amber for 4 secs and Green for 15secs and the cycle starts over again. Please Does any one have a code for traffic lights control that I can work on. My program will be working in VxWorks platform (Tornado - Real time operating system)
    :
    : Thank you ;-)
    :
    :
    Just cause I'm a little bored - though you'll have to figure the environment specific stuff: (this works in Win32)
    [code]
    // TrafficLight.h file
    #ifndef _TRAFFIC_LIGHT_CLASS_DEFINED_
    #define _TRAFFIC_LIGHT_CLASS_DEFINED_

    class TrafficLight
    {
    public:
    enum LightState {RED = 0, YELLOW, GREEN};

    public:
    TrafficLight();
    void Advance();
    void Wait();
    char* CurrentState();

    private:
    LightState m_state;
    int m_Delay[3];
    };
    #endif


    // TrafficLight.cpp file
    #include "Windows.h"
    #include "TrafficLight.h"

    TrafficLight::TrafficLight()
    {
    m_state = RED;
    m_Delay[ RED ] = 10 * 1000; // msec value
    m_Delay[ YELLOW ] = 4 * 1000; // msec value
    m_Delay[ GREEN ] = 15 * 1000; // msec value
    };

    void TrafficLight::Advance()
    {
    switch (m_state)
    {
    case RED: m_state = GREEN; break;
    case YELLOW: m_state = RED; break;
    case GREEN: m_state = YELLOW; break;
    }
    };

    void TrafficLight::Wait()
    {
    Sleep(m_Delay[m_state]);
    };

    char* TrafficLight::CurrentState()
    {
    switch (m_state)
    {
    case RED: return "Red"; break;
    case YELLOW: return "Yellow"; break;
    case GREEN: return "Green"; break;
    }

    return "Error";
    }
    // LightSim.cpp file
    // LightSim.cpp : Defines the entry point for the console application.
    //

    #include
    #include "TrafficLight.h"

    int main(int argc, char* argv[])
    {
    TrafficLight light1;

    while (1)
    {
    printf("Light is currently %s
    ", light1.CurrentState());
    light1.Wait();
    light1.Advance();
    };

    return 0;
    }
    [/code]


  • zeroscspotzeroscspot Posts: 40Member
    you may want to consider using adj

    adj = -4
    adj = -3

    the teacher showed me this because of a project i was doing where it dealt with admissions of different groups of people, one of the groups was infants. In the program it said that the first infant of any group was free

    in the program she started off that infant group with

    adj = -1

    and then proceeded to put in other calculations to figure out the total price for admissions of the group

    im not sure if adj will help you in time situations.
  • tahminatahmina Posts: 1Member
    Edit: Post removed. Code begging is against the C/C++ board posting policies. Also, this thread is 3 years old, so I'm locking it.

    Lundin, C/C++ moderator
This discussion has been closed.