Go Back  Xtreme Visual Basic Talk > Other Languages > Miscellaneous Languages > C# Code for making a traffic Light System ???


Reply
 
Thread Tools Display Modes
  #1  
Old 06-28-2012, 05:47 AM
syedwaqasali95 syedwaqasali95 is offline
Freshman
 
Join Date: Nov 2010
Posts: 26
Default C# Code for making a traffic Light System ???


Hello Everyone,
I am new to C# so am having trouble making a Traffic Lights System.
I have used three timers and a button.
When I press the button the RedTimer starts but the program just stops working after that, the red light remains lit and the other one's don't start( it does respond ) .
This is the code for my three timers and each timer has an interval of 1000 ms.
Can anyone tell me why this is happening ???

Code:
        private void buttonStart_Click(object sender, EventArgs e)
        {
            timerRed.Start();
        } 


       private void timerRed_Tick(object sender, EventArgs e)
       {
           timerGreen.Stop();
           redLight.FillColor = Color.Red;            
           greenLight.FillColor = Color.Black;
            timerRed.Stop();
            timerYellow.Start();
        }
        private void timerYellow_Tick(object sender, EventArgs e)
        {
            timerRed.Stop();
            yellowLight.FillColor = Color.Yellow;
            redLight.FillColor = Color.Black;
            timerYellow.Stop();
            timerGreen.Start();
        }
        private void timerGreen_Tick(object sender, EventArgs e)
        {
            timerGreen.Stop();
            greenLight.FillColor = Color.Green;
            yellowLight.FillColor = Color.Black;
            timerGreen.Stop();
            timerRed.Start();
        }
Reply With Quote
  #2  
Old 06-28-2012, 09:39 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

In the IDE, Properties window, events subsection for your timer controls, for the "Tick" event, are you referencing the appropriate subroutines you have defined for each Tick event, or are the Tick event fields blank for the yellow and green?

Perhaps you had the IDE create the Red event for you (and it updated the Tick field to tie the event to your function), but then you just copied the code and changed the function names and operations, but didn't update the tick event field for those controls to have the IDE create the behind the scenes code that ties the events to your functions.

Also, normally you would want to only use one timer in a program anyway. You would use a variable to track the state of things and take appropriate action based on that state in the Tick event of the one timer, rather than use up a number of timer resources for one program.

What type of control are your Lights, by the way.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 06-28-2012 at 09:46 AM.
Reply With Quote
  #3  
Old 06-28-2012, 09:50 AM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is online now
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Wigan, UK
Posts: 1,676
Default

Rather than have three timers each of which start and stop the other timers could you not just have a single timer and track the current state of the lights, every time the timer ticks move to the next state or go to the original state if we have gone through all possible states.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #4  
Old 07-20-2012, 06:19 AM
ElderKnight ElderKnight is offline
Senior Contributor

Forum Leader
 
Join Date: Oct 2003
Location: Central Florida
Posts: 1,205
Default

Be sure to have a fail-safe routine to assure that you never, ever get greens at right angles to one another.
__________________
-- D.J.

I do not endorse any items advertised within this frame, and regret that the viewer is subjected to such.
Reply With Quote
  #5  
Old 07-20-2012, 12:47 PM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default A traffic light sample using single timer and (maybe) a state machine

Quote:
Originally Posted by passel
Also, normally you would want to only use one timer in a program anyway.
Quote:
Originally Posted by PlausiblyDamp
Rather than have three timers each of which start and stop the other timers could you not just have a single timer
I agree with passel and PlausiblyDamp, three timers is two timers too many.

Also do you know there is such a thing as a Timer class built in .Net?

So it's really not even necessary to use a Timer control if you don't want to...

I'm not a C# programmer but I quickly found this code
(inside "TrafficLight.zip (62.17K)" on port #3 of this page):
Code:
private Timer lightTimer;
private TrafficLight trafficLight;
public TrafficLightController()
   {
       lightTimer = new Timer();
       lightTimer.Interval = 1000;
       lightTimer.Elapsed += lightTimer_Elapsed;
       trafficLight = new TrafficLight();
    }
In the "TrafficLight.cs" module for the above cited zip you may also
notice code like this:
Code:
namespace TrafficLightSim
{
    public class TrafficLight
    {
        public ITrafficLight State
        { get; set; }

        public Image Display
        {
            get
            {
                return State.Display;
            }
        }

        public TrafficLight()
        {
            State = new TrafficLightOff();
        }

        public void Change()
        {
            State.Change(this);
        }
    }
}
In programming there is a concept know as a state machine,
(or sometimes referred to as a finite state machine).

Even though passel suggestion is a valid one:
Quote:
You would use a variable to track the state of things..
..the other option, instead of using a byte variable like "yState",
is to explore code for developing a state machine class
that creates States for each of the colors,
as shown in the C# code of this off forum post.
If you really what to explore State Machines in C# I would also recommend
Part II of this CodeProject series.

Quote:
Originally Posted by passel
What type of control are your Lights, by the way.
I was curious about this also.

It's easy enough to just draw the traffic lights using FillEllipse.

To see this C# traffic light drawing code in a more complete example you can download
"CSharpGraphics[Modified].zip" from the fourth post down on this off forum page.

Last edited by surfR2911; 07-20-2012 at 01:26 PM.
Reply With Quote
Reply

Tags
c#language, lights, timers, traffic


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->