本网页已闲置超过3分钟,按键盘任意键或点击空白处,即可回到网页

基于Arduino 的智能交通灯系统
发布时间:2021-05-15
分享到:
基于Arduino 的智能交通灯系统
发布时间:2021-05-15
分享到:

该项目是创建一个交通灯系统,模拟现实生活中的交通灯系统,如下图,一个方向是一条主要的双车道道路,另一个方向是一个掉头,包括一个人行横道。

硬件组件:

  • Arduino Mega 2560
  • 跳线
  • 发光二极管(5mm) 红色、绿色、黄色
  • 发光二极管(3mm) 红色、绿色
  • 高亮度发光二极管  白色
  • LDR 5mΩ
  • 按钮开关
  • 7段发光二极管显示屏

设计步骤:

步骤一:塑造街道的形状

1)人行道 

2)掉头

步骤二:焊接交通灯LEDS,路灯杆,准备其余的部件

1)交通灯(一个带有行人附加按钮)

2)路灯ploes

3)伺服sg90、4个红外传感器、7段显示器

项目演示:

 代码:

交通灯系统代码

//#include <VarSpeedServo.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define F_CPU 16000000

int pinA = 8;
int pinB = 9;
int pinC = 4;
int pinD = 3;
int pinE = 2;
int pinF = 7;
int pinG = 6;
int pinP = 5;
#define buzz PB6                   // 蜂鸣器
#define ledR1 PB0                  // 主路红灯   //交通
#define ledY1 PB1                  // 主干道黄灯 //交通
#define ledG1 PB2                  // 主路绿灯 //交通
#define ledR2 PB3                   // 掉头红灯 //交通
#define ledY2 PB4                 // 掉头黄灯 //交通
#define ledG2 PB5                 // 掉头绿灯 //交通
#define ledtr PF0                 // 人行横道红色LED
#define ledtg PF1                  // 人行横道绿色LED
#define IR1 PD3                     //红外传感器
#define IR2 PJ1                      //红外传感器
#define IR3 PA0                        //红外传感器
#define IR4 PA1                         //红外传感器
#define ledoff PA4                      //主开关LED红色
#define ledon PA5                       //主开关LED绿色 
#define ledflash PA6                    //白色LED闪光灯(雷达)

#define button1 PC3                        //行人按钮1
#define button2 PK0                        //按钮2(主交通开关)

volatile unsigned char elec=0;           //其中一些可变变量不再使用
volatile unsigned int e=0;
    
volatile unsigned int ldrStatus=0;

 volatile unsigned int pinchange = 0;
 volatile unsigned int x = 0;
 volatile unsigned int S = 0;
 int o=0;
 int sens1 =22;
 int sens2 =23;
      
ISR(TIMER3_OVF_vect){        // 定时器3触发,一段时间后,它检查灯是否低于一定值,以打开灯或不打开(LDR传感器)

int ldrStatus = analogRead(A11);  /* 另外,在代码的另一部分中添加了该块,以确保即使代码在另一个Inturret中,路灯也在夜间亮起*/

if (ldrStatus <= 200) {

digitalWrite(41, HIGH);


Serial.println(ldrStatus);

} else {

digitalWrite(41, LOW);


}

}
volatile unsigned int count=0;
volatile unsigned long start =0;
volatile unsigned long t1 =0;
volatile unsigned long t2 =0;
volatile  float t3=0;


ISR(TIMER2_OVF_vect){         // 如果乌图恩的交通是绿色的,行人被允许过马路,而一辆车从主干道前没有停过白色的
                            // 线路或刚刚中断的交通,为了安全起见,蜂鸣器会提醒行人,7段显示屏也会叮当响,伺服(门)
S++;                         // 将在u形转弯处缓慢关闭(演示后修改),以避免任何事故(计时器检查是否夹伤(白线处的红外传感器(主要道路))
if(S>=100){                   //那是从下面的夹钳插入的
if(pinchange==1){
pinchange=0;
S=0;


int ldrStatus = analogRead(A11);   //再次检查LDR

if (ldrStatus <= 200) {

digitalWrite(41, HIGH);
Serial.println(ldrStatus);

} else {

digitalWrite(41, LOW);


}

  PORTF &= ~(1<<ledtg);
 TCNT2=99;
 OCR5A=350;                              // 伺服控制
    _delay_ms(100);
 OCR5A=340;  
    _delay_ms(100);
 OCR5A=320;  
    _delay_ms(100);
OCR5A=300;  
    _delay_ms(100);
    OCR5A=280;    
    _delay_ms(100);
    OCR5A=260;  
    _delay_ms(100);
    OCR5A=220;  
    _delay_ms(100);
    OCR5A=190;  
    _delay_ms(500);
       OCR5A=150;  
    _delay_ms(500);


 ldrStatus = analogRead(A11);  //再次检查LDR

if (ldrStatus <= 200) {

digitalWrite(41, HIGH);
Serial.println(ldrStatus);

} else {

digitalWrite(41, LOW);
}

PORTB |=  (1<<ledG2);           // LED和7段显示屏 
PORTB |= (1<<buzz);
PORTF |= (1<<ledtr);
digitalWrite(pinA, LOW);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, LOW);   
  digitalWrite(pinP, LOW);
_delay_ms(800);
PORTB &=  ~(1<<ledG2);
PORTB &= ~(1<<buzz);
PORTF &= ~(1<<ledtr);
digitalWrite(pinA, HIGH);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, HIGH);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);
_delay_ms(200);
OCR5A=140;              // 伺服
    _delay_ms(500);
  OCR5A=130;  
    _delay_ms(500);
PORTB |=  (1<<ledG2);
PORTB |= (1<<buzz);
PORTF |= (1<<ledtr);
digitalWrite(pinA, LOW);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, LOW);   
  digitalWrite(pinP, LOW);
_delay_ms(800);
PORTB &=  ~(1<<ledG2);

PORTB &= ~(1<<buzz);
PORTF &= ~(1<<ledtr);
digitalWrite(pinA, HIGH);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, HIGH);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);
_delay_ms(200);
PORTB |=  (1<<ledG2);
PORTB |= (1<<buzz);
PORTF |= (1<<ledtr);


 ldrStatus = analogRead(A11);   //再次检查LDR

if (ldrStatus <= 200) {

digitalWrite(41, HIGH);


Serial.println(ldrStatus);

} else {

digitalWrite(41, LOW);


}

digitalWrite(pinA, LOW);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, LOW);   
  digitalWrite(pinP, LOW);
_delay_ms(800);
PORTB &=  ~(1<<ledG2);

PORTB &= ~(1<<buzz);
PORTF &= ~(1<<ledtr);
digitalWrite(pinA, HIGH);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, HIGH);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);
_delay_ms(200);
OCR5A=120;                   // 继续伺服(闸门)
    _delay_ms(500);
  OCR5A=75;  
    _delay_ms(500);
PORTB |=  (1<<ledG2);
PORTB |= (1<<buzz);
PORTF |= (1<<ledtr);
digitalWrite(pinA, LOW);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, LOW);   
  digitalWrite(pinP, LOW);
_delay_ms(800);
PORTB &=  ~(1<<ledG2);

PORTB &= ~(1<<buzz);               //蜂鸣器
PORTF &= ~(1<<ledtr);
digitalWrite(pinA, HIGH);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, HIGH);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);
_delay_ms(200);

 ldrStatus = analogRead(A11);      //检查LDR

if (ldrStatus <= 200) {

digitalWrite(41, HIGH);


Serial.println(ldrStatus);

} else {

digitalWrite(41, LOW);


}

OCR5A=100;  
    _delay_ms(500);
  OCR5A=75;  
    _delay_ms(500);
digitalWrite(pinA, HIGH);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, HIGH);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);
 PORTB &= ~(1<<ledG2);
PORTB |=  (1<<ledY2);
_delay_ms(1000);
PORTB &=  ~(1<<ledY2);
PORTB |=  (1<<ledR2);
_delay_ms(3500);

PORTB &=  ~(1<<ledR2);

PORTB |= (1<<ledG2);
PORTF |= (1<<ledtg);
OCR5A=360;                       //过了一段时间,车流恢复了原来的状态,大门又开了
    _delay_ms(1500);


}
  
}

TCNT2=99;
pinchange=0;
}
  
ISR(PCINT1_vect )     // pinchange InturReport(用于主干道白线处的红外传感器,用于在主干道(2车道)为红色时检测任何通过白线的车辆

pinchange=1; 
  x=0;
}
void(* resetFunc) (void) = 0;       //复位功能

ISR(PCINT2_vect ){   //这个夹持器用于主开关重新设置代码,所有东西都会关闭,直到主开关打开
 resetFunc();        //调用重置函数
}

int main() { 
      
pinMode(sens1 , INPUT);             // 配置所有输出、输入、计时器和InturReports
pinMode(sens2 , INPUT);

DDRK &= ~(1<<PK3);
DDRK &= ~(1<<PK0);                                   
PORTK |= (1<<button2);
DDRG |= (1<<PG0);
DDRA &= ~((1<<IR3)|(1<<IR4));
    DDRB=0xff;
    DDRA |= (1<<ledoff) | (1<<ledon) | (1<<ledflash);
  DDRC &= ~(1<<button1);
    DDRD &= ~(1<<IR1);
  PORTC |= (1<<button1);
    DDRC |= (1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC4)|(1<<PC5);
    DDRF |=  (1<<ledtr)|(1<<ledtg); 
    PORTB &=  ~(1<<ledY1);
 PORTA |= (1<<ledoff);
   PORTA &= ~(1<<ledon);

pinMode(pinA, OUTPUT);     
  pinMode(pinB, OUTPUT);     
  pinMode(pinC, OUTPUT);     
  pinMode(pinD, OUTPUT);     
  pinMode(pinE, OUTPUT);     
  pinMode(pinF, OUTPUT);     
  pinMode(pinG, OUTPUT);   
  pinMode(pinP, OUTPUT); 
  digitalWrite(pinA, HIGH);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, HIGH);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);
    DDRJ &= ~(1<<IR2);
  DDRD &= ~(1<<IR1);
    DDRD |= (1<<PD2)|(1<<PD1);
    PORTF &= ~(1<<ledtg);
PORTF &= ~(1<<ledtr);
PORTB &= ~(1<<ledG1);
PORTB &= ~(1<<ledR2);
PORTB &= ~(1<<ledR1);
PORTB &= ~(1<<ledY1);
PORTB &= ~(1<<ledG2);
PORTB &= ~(1<<ledY2);
PORTA &= ~(1<<ledflash);

int w=0;
int k=0;
int h=0;
int y=0;
int b=0;

cli();
PCICR|= ( 1<< PCIE1) ; 
PCMSK1 |= ( 1<< PCINT10);


TCCR5A|=(1<<COM5A1)|(1<<COM5B1)|(1<<WGM51);        
   TCCR5B|=(1<<WGM53)|(1<<WGM52)|(1<<CS51)|(1<<CS50); 
   ICR5=4999;  //fPWM=50Hz 
   DDRL|=(1<<PL3);


TCCR3A = 0 ;
TCCR3B = 0 ; 
TCNT3 = 600 ; 
TIMSK3 |= ( 1<< TOIE3) ;
 TCCR3B |=   ( 1<< CS31 ) | ( 1<< CS30 ) ;


TCCR1A = 0 ;
TCCR1B = 0 ; 
TCNT1 = 0 ; 

TCCR4A = 0 ;
TCCR4B = 0 ; 
TCNT4 = 59285 ; 
TIMSK4 |= ( 1<< TOIE4) ; 
 
Serial.begin(9600);

pinMode(41, OUTPUT);

pinMode(A11, INPUT);

PCICR&= ~(1 <<  PCIE2);
PCMSK2 &= ~( 1<< PCINT16);
TCCR2A = 0 ;
TCCR2B = 0 ; 
TCNT2 = 99 ; 
TIMSK2 |= ( 1<< TOIE2) ;
sei();
 OCR5A=100;  //90 degree
    _delay_ms(500);
    
while(1) {

if((PINK & (1<<button2))==0){
  PORTA &= ~(1<<ledoff);                                //等待按下主开关(打开)
  PORTA |=(1<<ledon);
PORTF |= (1<<ledtr);
PORTB |= (1<<ledG1);
PORTB |= (1<<ledR2);
PORTB &= ~(1<<ledR1);
OCR5A=360;  
    _delay_ms(500);

_delay_ms(3000);

PCICR|= (1 <<  PCIE2);                                      // 设置pinchange inturrept重置整个代码(确保按下按钮时它不工作)
PCMSK2 |= ( 1<< PCINT16);
  y=1;
  
   b =1;

}                                    // 雷达只有在主路绿灯时才工作
if(digitalRead(sens1)==0){        //一旦红外传感器探测到一辆车,雷达就会启动定时器1

         
t1=TCNT1;                               //t1等同于tcnt1
 TCCR1B |=   ( 1<< CS12 ) | ( 1<< CS10 ) ;

while(digitalRead(sens2));                 //红外2感测车再次
t2=TCNT1;                                 // t2等于tcnt1的下一个值,当它经过下一个ir传感器时
t3 =t2-t1;                                   //t3 t2和t1之间的差异

if(t3<30000){                                //如果t3<30000约2秒(闪烁该车)
  
  PORTA |= (1<<ledflash);
_delay_ms(200);
PORTA &= ~(1<<ledflash);
}
else{

TCNT1=0;
}
}

    if((PIND & (1<<IR1)) ==0 && w==0 && y==1){          //IR1是U-turn传感器,当车辆在U-turn等待时,它能感知到
    k=1; 
    w=1; 
    b=0;
    }
     if((PINC & (1<<button1)) ==0 && w==0 && y==1){      //按钮1是主路两侧的行人按钮
    k=1; 
    w=1; 
    b=0;
    }
  if(k==1){                 //按下IR1或其中一个按钮,主路变为红色,同时转弯变为绿色,行人可以通过
    
   _delay_ms(10000);
 
for(int i=0 ; i<=5;i++){
PORTB ^=  (1<<ledG1);
_delay_ms(500);
}
 PORTB &= ~(1<<ledG1);
PORTB |=  (1<<ledY1);
_delay_ms(3000);
PORTB &=  ~(1<<ledY1);
 TCCR2B |= ( 1<< CS22) | ( 1<< CS21 ) | ( 1<< CS20 ) ;
S=0;
PORTB |=  (1<<ledR1);
PORTB &= ~(1<<ledR2);

PORTB |=  (1<<ledG2);
PORTF &= ~(1<<ledtr);
PORTF |=  (1<<ledtg);


 digitalWrite(pinA, LOW);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, LOW);   
  digitalWrite(pinP, HIGH); 
  for(int i =0;i<=999;i++){
  PORTB |= (1<<buzz);  
  _delay_ms(1);               
    PORTB &= ~(1<<buzz); 
    _delay_ms(1); 
  }


  digitalWrite(pinA, LOW);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);   
  for(int i =0;i<=999;i++){
  PORTB |= (1<<buzz);  
  _delay_ms(1);               
    PORTB &= ~(1<<buzz); 
    _delay_ms(1); 
  }

  digitalWrite(pinA, HIGH);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, LOW);   
  digitalWrite(pinP, HIGH);  
  for(int i =0;i<=999;i++){
  PORTB |= (1<<buzz);  
  _delay_ms(1);               
    PORTB &= ~(1<<buzz); 
    _delay_ms(1); 
  }
  digitalWrite(pinA, LOW);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);  
for(int i =0;i<=999;i++){
  PORTB |= (1<<buzz);  
  _delay_ms(1);               
    PORTB &= ~(1<<buzz); 
    _delay_ms(1); 
  }
  digitalWrite(pinA, LOW);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);  
  for(int i =0;i<=999;i++){
  PORTB |= (1<<buzz);  
  _delay_ms(1);               
    PORTB &= ~(1<<buzz); 
    _delay_ms(1); 
  } 


  digitalWrite(pinA, LOW);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, HIGH);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);  
  for(int i =0;i<=999;i++){
  PORTB |= (1<<buzz);  
  _delay_ms(1);               
    PORTB &= ~(1<<buzz); 
    _delay_ms(1); 
  }   

//3
  digitalWrite(pinA, LOW);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, HIGH);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, LOW);   
  digitalWrite(pinP, HIGH);  
 for(int i =0;i<=999;i++){
  PORTB |= (1<<buzz);  
  _delay_ms(1);               
    PORTB &= ~(1<<buzz); 
    _delay_ms(1); 
  } 
  digitalWrite(pinA, LOW);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH); 
    PORTF |=  (1<<ledtg); 
 
  for(int i =0;i<=9999;i++){
        PORTB |= (1<<buzz); 

  _delay_ms(0.1);               
    PORTB &= ~(1<<buzz); 
    
    _delay_ms(0.1); 
  } 
digitalWrite(pinA, HIGH);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, HIGH);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);
    PORTF &=  ~(1<<ledtg); 

 for(int i =0;i<=9999;i++){
  PORTB |= (1<<buzz);
  
  _delay_ms(0.1);               
    PORTB &= ~(1<<buzz); 
    _delay_ms(0.1); 
  } 
  PORTF |=  (1<<ledtg); 

  digitalWrite(pinA, LOW);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);
//4
  digitalWrite(pinA, LOW);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, LOW);   
  digitalWrite(pinP, HIGH);  
 for(int i =0;i<=9999;i++){
  PORTB |= (1<<buzz);
  
  _delay_ms(0.1);               
    PORTB &= ~(1<<buzz); 
    _delay_ms(0.1); 
  }
    PORTF &=  ~(1<<ledtg); 
  
  digitalWrite(pinA, HIGH);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, HIGH);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);   
 for(int i =0;i<=9999;i++){
  PORTB |= (1<<buzz); 
 
  _delay_ms(0.1);               
    PORTB &= ~(1<<buzz); 
    _delay_ms(0.1); 
  } 
    PORTF |=  (1<<ledtg); 

digitalWrite(pinA, LOW);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, LOW);   
  digitalWrite(pinP, HIGH); 

  digitalWrite(pinA, LOW);   
  digitalWrite(pinB, LOW);   
  digitalWrite(pinC, LOW);   
  digitalWrite(pinD, LOW);   
  digitalWrite(pinE, LOW);   
  digitalWrite(pinF, LOW);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH);  
  _delay_ms(1000);
digitalWrite(pinA, HIGH);   
  digitalWrite(pinB, HIGH);   
  digitalWrite(pinC, HIGH);   
  digitalWrite(pinD, HIGH);   
  digitalWrite(pinE, HIGH);   
  digitalWrite(pinF, HIGH);   
  digitalWrite(pinG, HIGH);   
  digitalWrite(pinP, HIGH); 
    PORTF &=  ~(1<<ledtg); 
    PORTF |=  (1<<ledtr); 
    
TCCR2B=0;
TCNT2=99;
k=0;
h=1;
  }

if(k==0 && h==1){        //当10秒通过时,正常情况恢复,主要道路变为绿色,因为这是主要道路
b=1;                     // 很多车都用它
for(int i=0 ; i<=5;i++){
PORTB ^=  (1<<ledG2);
_delay_ms(500);
}
 PORTB &= ~(1<<ledG2);
PORTB |=  (1<<ledY2);
_delay_ms(3000);
PORTB &=  ~(1<<ledY2);
PORTB |=  (1<<ledR2);
PORTB &= ~(1<<ledR1);

PORTB |=  (1<<ledG1);
PORTF |= (1<<ledtr);
  h=0;
  w=0;
}

}  
}  

加入微信技术交流群

技术交流,职业进阶

关注与非网服务号

获取电子工程师福利

加入电路城 QQ 交流群

与技术大牛交朋友

讨论