#include <TaskScheduler.h>
enum{
OFF, ON
}led_state;
/*Macro declaration section*/
#define RED_LED_PIN 12
#define YELLOW_LED_PIN 11
#define GREEN_LED_PIN 10
#define RED_LED_DELAY 10 /* 1sec=100ms *10 count*/
#define YELLOW_LED_DELAY 20 /* 2sec = 10ms * 200 count*/
#define GREEN_LED_DELAY 50 /* 5 sec=10ms * 500 count*/
/*Variable declaration section*/
unsigned char Red_Delay_Counter;
unsigned char Yellow_Delay_Counter;
unsigned int Green_Delay_Counter;
unsigned char RED_LED_STATE;
unsigned char YELLOW_LED_STATE;
unsigned char GREEN_LED_STATE;
/*funtion declaration section*/
void Red_Led_CntrlFun(void);
void Yellow_Led_CntrlFun(void);
void Green_Led_CntrlFun(void);
void Tick_timer(void);
/*RED LED function declaration section*/
void Red_Led_Init_State(void);
void Red_Led_ON_State_fun(void);
void Red_Led_OFF_State_fun(void);
void Red_Led_ON_State_entry_fun(void);
void Red_Led_OFF_State_entry_fun(void);
void Red_Led_ON_State_exit_fun(void);
void Red_Led_OFF_State_exit_fun(void);
void Red_Led_OFF_State_during_fun(void);
/*YELLOW LED function declaration section */
void Yellow_Led_Init_State(void);
void Yellow_Led_ON_State_fun(void);
void Yellow_Led_OFF_State_fun(void);
void Yellow_Led_ON_State_entry_fun(void);
void Yellow_Led_OFF_State_entry_fun(void);
void Yellow_Led_ON_State_exit_fun(void);
void Yellow_Led_OFF_State_exit_fun(void);
void Yellow_Led_OFF_State_during_fun(void);
/*GREEN LED function declaration section */
void Green_Led_Init_State(void);
void Green_Led_ON_State_fun(void);
void Green_Led_OFF_State_fun(void);
void Green_Led_ON_State_entry_fun(void);
void Green_Led_OFF_State_entry_fun(void);
void Green_Led_ON_State_exit_fun(void);
void Green_Led_OFF_State_exit_fun(void);
void Green_Led_OFF_State_during_fun(void);
void Task_10msec(void);
void Task_40msec(void);
void Task_100msec(void);
#define FAST_MS_TASK 10
#define MID_MS_TASK 40
#define SLOW_MS_TASK 100
Scheduler ts;
//Task(unsigned long aInterval, long aIterations, void (\*aCallback)(), Scheduler\* aScheduler, bool aEnable, bool (\*aOnEnable)(), void (\*aOnDisable)())
Task fast(FAST_MS_TASK, -1, &Task_10msec, &ts, true, NULL, NULL);
Task mid(MID_MS_TASK, -1, &Task_40msec, &ts, true, NULL, NULL);
Task slow(SLOW_MS_TASK, -1, &Task_100msec, &ts, true, NULL, NULL);
void setup()
{
// initialize digital pin LED_BUILTIN as an output.
Red_Led_Init_State();
Yellow_Led_Init_State();
Green_Led_Init_State();
Serial.begin(115200);
}
void loop()
{
ts.execute();
}
void Task_10msec(void)
{
}
void Task_40msec(void)
{
}
void Task_100msec(void)
{
Tick_timer();
Red_Led_CntrlFun();
Yellow_Led_CntrlFun();
Green_Led_CntrlFun();
}
void Red_Led_Init_State(void)
{
RED_LED_STATE = OFF;
Red_Delay_Counter = 0;
pinMode(RED_LED_PIN, OUTPUT);
digitalWrite(RED_LED_PIN, OFF);
}
void Yellow_Led_Init_State(void)
{
Yellow_Delay_Counter=0;
YELLOW_LED_STATE=OFF;
pinMode(YELLOW_LED_PIN, OUTPUT);
digitalWrite(YELLOW_LED_PIN, OFF);
}
void Green_Led_Init_State(void)
{
Green_Delay_Counter=0;
GREEN_LED_STATE=OFF;
pinMode(GREEN_LED_PIN, OUTPUT);
digitalWrite(GREEN_LED_PIN, OFF);
}
void Tick_timer(void)
{
Red_Delay_Counter++;
Yellow_Delay_Counter++;
Green_Delay_Counter++;
}
/*RED LED Entry and Exit functions*/
void Red_Led_CntrlFun(void)
{
switch(RED_LED_STATE)
{
case OFF:
Red_Led_OFF_State_fun();
break;
case ON:
Red_Led_ON_State_fun();
break;
default:
/* do nothing*/
break;
}
}
void Red_Led_OFF_State_fun(void)
{
/*check the exit critiria*/
if(Red_Delay_Counter > RED_LED_DELAY)
{
RED_LED_STATE = ON;
Red_Led_ON_State_entry_fun();
Red_Led_OFF_State_exit_fun();
}
else
{
/*during OFF state*/
Red_Led_OFF_State_during_fun();
}
}
void Red_Led_ON_State_fun(void)
{
/*check the exit critiria*/
if(Red_Delay_Counter > RED_LED_DELAY)
{
RED_LED_STATE = OFF;
Red_Led_OFF_State_entry_fun();
Red_Led_ON_State_exit_fun();
}
else
{
/*during OFF state*/
/*do nothing*/
}
}
void Red_Led_OFF_State_during_fun(void)
{
/*do nothing*/
}
void Red_Led_ON_State_entry_fun(void)
{
digitalWrite(RED_LED_PIN, ON);
}
void Red_Led_OFF_State_entry_fun(void)
{
digitalWrite(RED_LED_PIN, OFF);
}
void Red_Led_ON_State_exit_fun(void)
{
Red_Delay_Counter = 0;
}
void Red_Led_OFF_State_exit_fun(void)
{
Red_Delay_Counter = 0;
}
unsigned char Red_Led_State_value(void)
{
return RED_LED_STATE;
}
/*End of Red Led functions*/
/*Yellow LED Entry and Exit state function*/
void Yellow_Led_CntrlFun(void)
{
switch(YELLOW_LED_STATE)
{
case OFF:
Yellow_Led_OFF_State_fun();
break;
case ON:
Yellow_Led_ON_State_fun();
break;
default:
/* do nothing*/
break;
}
}
void Yellow_Led_OFF_State_fun(void)
{
/*check the exit critiria*/
if(Yellow_Delay_Counter > YELLOW_LED_DELAY)
{
YELLOW_LED_STATE = ON;
Yellow_Led_ON_State_entry_fun();
Yellow_Led_OFF_State_exit_fun();
}
else
{
/*during OFF state*/
Yellow_Led_OFF_State_during_fun();
}
}
void Yellow_Led_ON_State_fun(void)
{
/*check the exit critiria*/
if(Yellow_Delay_Counter > YELLOW_LED_DELAY)
{
YELLOW_LED_STATE = OFF;
Yellow_Led_OFF_State_entry_fun();
Yellow_Led_ON_State_exit_fun();
}
else
{
/*during OFF state*/
/*do nothing*/
}
}
void Yellow_Led_OFF_State_during_fun(void)
{
/*do nothing*/
}
void Yellow_Led_ON_State_entry_fun(void)
{
digitalWrite(YELLOW_LED_PIN, ON);
}
void Yellow_Led_OFF_State_entry_fun(void)
{
digitalWrite(YELLOW_LED_PIN, OFF);
}
void Yellow_Led_ON_State_exit_fun(void)
{
Yellow_Delay_Counter = 0;
}
void Yellow_Led_OFF_State_exit_fun(void)
{
Yellow_Delay_Counter = 0;
}
/*End of Yellow Led Functions*/
/*Green LED entry and exit functions*/
void Green_Led_CntrlFun(void)
{
switch(GREEN_LED_STATE)
{
case OFF:
Green_Led_OFF_State_fun();
break;
case ON:
Green_Led_ON_State_fun();
break;
default:
/* do nothing*/
break;
}
}
void Green_Led_OFF_State_fun(void)
{
/*check the exit critiria*/
if(Green_Delay_Counter > GREEN_LED_DELAY)
{
GREEN_LED_STATE = ON;
Green_Led_ON_State_entry_fun();
Green_Led_OFF_State_exit_fun();
}
else
{
/*during OFF state*/
Green_Led_OFF_State_during_fun();
}
}
void Green_Led_ON_State_fun(void)
{
/*check the exit critiria*/
if(Green_Delay_Counter > GREEN_LED_DELAY)
{
GREEN_LED_STATE = OFF;
Green_Led_OFF_State_entry_fun();
Green_Led_ON_State_exit_fun();
}
else
{
/*during OFF state*/
/*do nothing*/
}
}
void Green_Led_OFF_State_during_fun(void)
{
/*do nothing*/
}
void Green_Led_ON_State_entry_fun(void)
{
digitalWrite(GREEN_LED_PIN, ON);
}
void Green_Led_OFF_State_entry_fun(void)
{
digitalWrite(GREEN_LED_PIN, OFF);
}
void Green_Led_ON_State_exit_fun(void)
{
Green_Delay_Counter = 0;
}
void Green_Led_OFF_State_exit_fun(void)
{
Green_Delay_Counter = 0;
}
/*End of Green LED Functions*/