by using c++ windows form,how to use timer in traffic light that use sensors ,where in each cycle the time deffer between the traffic lights ..as you see here the output always is the last color the code didn't represent each color when its time come ..
while((n1>OSi) && (t!=max))//n1,n2 are number of cars that counted by IS"In sensor". OS "outer sensor.
{
if (button4->BackColor == System::Drawing::Color::Green)
{
button4->BackColor==System::Drawing::Color::Gray;
if ((n1>0) && (n2!=0))
{
button1->BackColor = System::Drawing::Color::Gray;
button2->BackColor=System::Drawing::Color::Yellow;
button2->BackColor=System::Drawing::Color::Gray;
button3->BackColor=System::Drawing::Color::Red;
}
t=1;
if (n2 != 0)
while((n2>osj) &&( t!=max))
{
button3->BackColor=System::Drawing::Color::Red;
button4->BackColor=System::Drawing::Color::Green;
t++;
}
n1=rand();//rand()%number .. this to get random number of the cars.
if (n1>0)
if (button4->BackColor==System::Drawing::Color::Green)
{
button4->BackColor=System::Drawing::Color::Gray;
button5->BackColor=System::Drawing::Color::Yellow;
button5->BackColor=System::Drawing::Color::Gray;
button6->BackColor=System::Drawing::Color::Red;
}
n2=rand();
Not sure what your asking and you have no comments in your code so I'm not sure what it's doing. It's not complete so I can't run it.
At least explain what the variables are with comments.
I would also comment each if statement and calculation so we don't have to guess at your intent.
I used values just to run the traffic light function because my program is not done yet and I want to check how the traffic light function works
but this is my code ..
I declared this in the public
int y,c;
double a,b;
int x1=0;
int x2=0;
this code below I put it in button1 ..
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//the program is about two traffic light on one way road,one in the sowth and other in west
//i start the program by assuming number of cars by using rand function
//i used three buttons on the both sides as traffic light just to change its color to represent red,yellow or green
y=Convert::ToInt16(textBox1->Text);
c=Convert::ToInt16(textBox2->Text);
a=Convert::ToDouble(rand() % y);//a is the number of cars for the west side,i used rand to get randome number
b=Convert::ToDouble(rand() % c);//b is the number of cars for the south side,i used rand to get randome number
textBox3->Text = Convert::ToString(a);
textBox4->Text = Convert::ToString(b);
int i=0;
if (a >= b)//here comparing a,b to see which traffic light would start
do
{
trafficlight(a,b,1,2);//calling function traffic light
i++;
}while (i<10);//here i assumed the program would run less than 10 times.
else
do
{
trafficlight(b,a,2,1);
i++;
}while (i<10);
}
void TrafficLight_1(int& n1, int& n2,int i,int j)
{
int max=60;
int t=1;
//os1,osj are output sensors that calculate the number of cars that pass when the traffic is green
}
t=1;
if (n2 != 0)
while((n2>osj) &&( t!=max))
{
button3->BackColor=System::Drawing::Color::Red;
button4->BackColor=System::Drawing::Color::Green;
t++;
}
n1=calculate();
if (n1>0)
if (button4->BackColor==System::Drawing::Color::Green)
{
button4->BackColor=System::Drawing::Color::Gray;
for(int i=0;i>=2;i++)
button5->BackColor=System::Drawing::Color::Yellow;
button5->BackColor=System::Drawing::Color::Gray;
button6->BackColor=System::Drawing::Color::Red;
}
n2=calculate();
}
//IS is the input sensor that calculates the number of waiting cars.
Calculate(i)
{
n=ISi+(xi-OSi); //ISi is the input sensor (IS1=rand()%y, IS2=rand()%c)
Xi=n; //Xi is the number of cars that didn't pass in the green light
Return n;
}