#define RED_LED 9
#define YELLOW_LED 8
#define GREEN_LED 7
#define Switch_pin 11
#define di_pin1 13
#define di_pin2 1
static unsigned char led_Output = LOW; // Variable to store the LED state //COUNTER FOR 10MS
static unsigned int counterfor10ms=0;
static unsigned char LED_Current_State=0;
static unsigned int RED_count=0;
static unsigned int YELLOW_count=0;
static unsigned int GREEN_count=0;
static unsigned int RED_LED_STATE=0;
static unsigned int Yellow_STATE=0;
static unsigned int YELLOW_LED_STATE=0;
static unsigned int GREEN_LED_STATE=0;
static unsigned char Switch_state=0;
static unsigned int count=0;
static unsigned char i=0;
static void Task_10ms(void);
static void LED_debounce(void);
static void task_manager(void);
void timer_init(void);
static void Traffic_light(void);
static void RED_light(void);
static void GREEN_light(void);
static void YELLOW_light(void);
void Yellow(void);
unsigned int dig[2]={di_pin1,di_pin2};
unsigned int seg[8]={2,3,4,5,6,10,12,13};// for pins a,b,c,d,e,f,g
const byte digitPatterns[16] = {
B11000000, // 0
B11111001, // 1
B10100100, // 2
B10110000, // 3
B10011001, // 4
B10010010, // 5
B10000010, // 6
B11111000, // 7
B10000000, // 8
B10010000, // 9
B10001001, //A
B10000000, ///B
B11000110,//C
B11000000,//D
B10000110,
B10000110,
};
ISR(TIMER1_COMPA_vect) // ISR
{
TCNT1=0;
counterfor10ms++;
//counterfor100ms++;
}
void timer_init() //TIMER INITIALIZATION
{
cli();
TCCR1A=0;
TCCR1B=0;
TCCR1B|=B00000100; //PSC=256
TIMSK1|=B00000010; // INITIALLY SETTING TIM A,B BITS=0
OCR1A=625; /* 625 - 10ms, 62 - 1ms*/
sei();
}
void setup() /* This function will execute only 1 time*/
{
timer_init();
pin_init();
LED_State_Init();
pin_dec();
}
/* Do not modify*/
void loop()
{
// put your main code here, to run repeatedly:
task_manager();
}
static void task_manager(void)
{
if(counterfor10ms>=1)
{
Task_10ms();
Read_switch();
counterfor10ms=0;
}
if(counterfor10ms>=10)
{
count++;
}
}
static void pin_init() // digital pins
{
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(Switch_pin, INPUT);
}
static void Read_switch(void)
{
Switch_state=digitalRead(Switch_pin);
}
static void Task_10ms()
{
if(Switch_state==1)
{
Traffic_light();
}
else{
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
LED_State_Init();
}
}
static void LED_State_Init(void)
{
led_Output = LOW;
LED_Current_State =0;
RED_LED_STATE=1;
}
static void Traffic_light(void)
{
RED_light();
YELLOW_light();
GREEN_light();
Yellow();
}
static void RED_light(void)
{
if(RED_LED_STATE==1)
{
if(RED_count<=1000)
{
digitalWrite(RED_LED, HIGH);
RED_count++;
seven_segment();
}
else
{
digitalWrite(RED_LED, LOW);
YELLOW_light();
RED_count=0;
RED_LED_STATE=0;
YELLOW_LED_STATE=1;
}
}
}
static void YELLOW_light(void)
{
if(RED_LED_STATE==0 && YELLOW_LED_STATE==1)
{
if(YELLOW_count<=500)
{
digitalWrite(YELLOW_LED, HIGH);
YELLOW_count++;
}
else
{
digitalWrite(YELLOW_LED, LOW);
YELLOW_LED_STATE=0;
GREEN_light();
YELLOW_count=0;
GREEN_LED_STATE=1;
}
}
}
static void GREEN_light(void)
{
if(YELLOW_LED_STATE==0 && GREEN_LED_STATE==1)
{
if(GREEN_count<=1000)
{
digitalWrite(GREEN_LED, HIGH);
GREEN_count++;
}
else
{
digitalWrite(GREEN_LED, LOW);
GREEN_LED_STATE=0;
Yellow_STATE=1;
Yellow();
GREEN_count=0;
}
}
}
void Yellow(void)
{
if(Yellow_STATE==1)
{
if(YELLOW_count<=200)
{
digitalWrite(YELLOW_LED, HIGH);
YELLOW_count++;
}
else
{
digitalWrite(YELLOW_LED, LOW);
RED_light();
YELLOW_count=0;
Yellow_STATE=0;
RED_LED_STATE=1;
}
}
}
void pin_dec()
{
for(int i=0;i<2;i++)
{
pinMode(dig[i], OUTPUT);
}
for(int i=0;i<8;i++)
{
pinMode(seg[i], OUTPUT);
}
}
void display_pin(int num ,int num2)
{
for(int i=0;i<2;i++)
{
digitalWrite(dig[i],LOW);
}
for(int i=0;i<8;i++)
{
digitalWrite(seg[i],LOW);
}
for(int i=0;i<10;i++)
{
digitalWrite(seg[i],bitRead(digitPatterns[num], i));
delay(1000);
}
digitalWrite(dig[num2], HIGH);
//delay(1000);
}
static void seven_segment(void)
{
count++;
while(i<count)
{Serial.println(count);
for(int j=0;j<10;j++)
{
display_pin(i,0);
display_pin(j,1);
}
i++;
}
}