Click to See Complete Forum and Search --> : Traffic Lights c#


johnboy14
October 4th, 2008, 08:08 AM
I am trying to create a set of traffic lights using visual c#, my problem is how to implement a timer between each light. I have 3 traffic lights and 2 pedestrian lights. Any help on the timer folks.

TheCPUWizard
October 4th, 2008, 01:26 PM
Look at the Timer class..... :rolleyes: :rolleyes:

johnboy14
October 4th, 2008, 04:51 PM
where's that Mr Vague

TheCPUWizard
October 4th, 2008, 05:15 PM
where's that Mr Vague

Did you ever consider a really radical approach of going to the MSDN site and typing in "Timer"...The very first hit is what you need.

In case that is too difficult:http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

funnyusername
October 5th, 2008, 11:34 AM
LOL :D You guys crack me up! :D

johnboy14
October 5th, 2008, 11:41 AM
very useful, I am new to c# so all and visual c# so I ain't quite up todate on all the sites and best places for different stuff. Thanks anyway.

eclipsed4utoo
October 5th, 2008, 11:51 AM
very useful, I am new to c# so all and visual c# so I ain't quite up todate on all the sites and best places for different stuff. Thanks anyway.

maybe you should also try to use the little known search site called Google. a simple search for "c# timers" would have netted you plenty of results.

TheCPUWizard
October 5th, 2008, 12:03 PM
msdn.microsoft.com is the official site for any Microsoft product related questions.

google.com is also a useful resource, but one needs to be careful since the results can come from "anyone". There is alot of incorrect material on the internet, and much of the .Net postings contain incorrect or sub-optimal information.

I usually recommmend starting with msdn then google if necessary. If you find something that looks good on google, you will have specific keywords that you can use to confirm the information back on msdn.

johnboy14
October 5th, 2008, 12:06 PM
Would I declare the timer before the main or as soon as I enter the main.

You see I have a Button that activates the lights, I want it to wait 5 seconds then give me an amber light.

TheCPUWizard
October 5th, 2008, 12:08 PM
You "Declare" the timer by dragging it from the toolbox onto your form. You can initialize the duration in the properties window, or in code. You can start and stop the timer with the enabled property. You write an event handler which is triggered when the timer expires.

Did you download and step through the available samples?

johnboy14
October 5th, 2008, 12:10 PM
bits of it, I am familar with declaring variables and methods etc. but I programmed a set of traffic lights with an AVR board once in C but low level stuff to be honest. I'll read through them,

p.s I was unaware that there was a timer on visual c#, so I could drag and drop. I can't seem to find it on my toobar though.

johnboy14
October 5th, 2008, 12:43 PM
In all honesty I'm not sure how to start.

this below is my code for my start button, I set the timer to intervals of 5 seconds so am I free to place what what I want my lights to do now.

private void button1_Click(object sender, System.EventArgs e)
{
timer1.Enabled = true

TheCPUWizard
October 5th, 2008, 01:03 PM
Go back to design mode on your form. Select the timer. Select the Properties Windows. Switch to events view (looks like a lightning bolt). Double click on the event. This will add an empty routine to your code where you put the logic of what to do when the timer expires...

I will ask one more time...
Did you download and step through the available samples?

johnboy14
October 5th, 2008, 01:09 PM
No I Did Not.

johnboy14
October 5th, 2008, 01:36 PM
I want it to just run in a loop. until the button is pressed

TheCPUWizard
October 5th, 2008, 02:15 PM
No I Did Not.

Then do so. If you have specific problems understanding someting in one of the turorials, plase post the URL you got the tutorial from and your question.

Until then....

johnboy14
October 5th, 2008, 03:59 PM
http://msdn.microsoft.com/en-us/library/3tszykws.aspx

I'm using this tutorial, not I can't figure out what to put in my timer event.

I want to press button1

wait 5 seconds

and the world "ON" should be placed in the text box of my choice

wait another 5

and the word "ON" is allocated as sequence of traffic lights.

TheCPUWizard
October 5th, 2008, 04:15 PM
Press button. In click even enable the timer. In the timer_tick method look to see if the light is on, if so turn it off, else turn it in.

Thats it

johnboy14
October 6th, 2008, 03:07 AM
[QUOTE=In the timer_tick method look to see if the light is on, if so turn it off, else turn it in.

Thats it[/QUOTE]

I don't know what your talking about here mate.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeTimer()
{
//' Run this procedure in an appropriate event.
// Set to 5 second.
timer1.Interval = 5000;
// Enable timer.
timer1.Enabled = true;
StartButton.Text = "Stop";
}

private void timer1_Tick(object sender, EventArgs e)
{

}

private void StartButton_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
red.Text = ("OFF");
amber.Text = ("ON");
green.Text = ("OFF");







}
}
}

TheCPUWizard
October 6th, 2008, 06:31 AM
1) Please use [ code ] tags. and use preview, so you dont have messed up posts.


private void timer1_Tick(object sender, EventArgs e)
{
if (red.Text == "ON")
{
red.Text=="OFF"
green.Text = "ON"
}
else if (green.Text == "ON")
{
green.Text = "OFF";
amber.Text = "ON";
}
else
{
amber.Text = "OFF";
red.Text = "ON";
}
}


There are more efficient way using enums, and arrays, but this will work (sucject to typos, but you should get the ides....

johnboy14
October 6th, 2008, 06:39 AM
that makes perfect sense, using bool expressions. Thanks again, I don't mean to be a dump *** but its all part of my learning methods. http://www.codeguru.com/forum/images/icons/icon10.gif