2404 views|6 replies

3

Posts

0

Resources
The OP
 

I just started learning and recently built a traffic light system. I have some questions about the timing flashing problem. I need help from an expert. [Copy link]

First, the red light (LED1) in the north-south direction lights up for 10 seconds, then the green light (LED2) in the east-west direction lights up for 4 seconds and flashes for 3 seconds, then the yellow light (LED3) in the east-west direction flashes for 3 seconds **********

The current problem is that the program cannot escape when it reaches if((s>=14000)&&(s<17000)) and LED2 keeps flashing.

Dear Gods, how should this be solved?

void deng()
{
Timer0Init();
while(1)
{
if(TF0==1)
{
TF0=0;
TH0=0XFC;
TL0=0X18;
s++;
}
if((s>=0)&&(s<10000)) //10s
{
LED1=~LED1; //Red light in the north-south direction
}
if((s>=10000)&&(s<14000)) //4s
{
LED1=1;
LED2=~LED2; //Green light in the east-west direction stays on for 4s
LED3=1;
}
if((s>=14000)&&(s<17000)) //3s
{
LED1=1;
LED3=1;
while((s>=14000)&&(s<17000))
{
LED2=~LED2; //Green light in the east-west direction flashes for 3s
delay(50000);
}
}
if((s>=17000)&&(s<20000))
{
a=0;
LED1=1;
LED2=1;

while((s>=17000)&&(s<20000))

{
LED3=~LED3; //The yellow light flashes for 3 seconds in the east-west direction

delay(50000);
}

}
if(s==20000) //Return to the initial (41) state, loop
{
LED2=1;
LED3=1;
s=0;
}

}
}

This post is from 51mcu

Latest reply

The problem with delay(),   Details Published on 2020-3-25 12:57
 

424

Posts

8

Resources
2
 

Print out the value of s through the serial port and have a look

This post is from 51mcu
 
 
 

5

Posts

0

Resources
3
 

Your delay is blocking, which slows down the time for your s to increment. Originally, you should have added 1 to 1ms? After encountering your delay, it became 500ms plus 1, which took longer. Turn on the timer interrupt, or change the delay to non-blocking.

This post is from 51mcu

Comments

+1  Details Published on 2019-11-26 18:29
 
 
 

98

Posts

0

Resources
4
 
00.00 Posted on 2019-11-26 17:13 Your delay is blocking, which slows down the time of your s increment. Originally, you should have added 1 to 1ms? After encountering your delay, it became 500ms plus 1. The time...

+1

This post is from 51mcu
 
 
 

2

Posts

0

Resources
5
 

No matter how you write a program, you always like to use delay to idle the time. Why not use interrupts? It will give the same result.

This post is from 51mcu
 
 
 

14

Posts

0

Resources
6
 

Top it

This post is from 51mcu
 
 
 

17

Posts

0

Resources
7
 

The problem with delay(),

This post is from 51mcu
 
 
 

Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list