//Define constants
#define LED_INTERNAL 13
#define LED_External 10
#define LEDPORT PORTB //port registers for controlling LED states
#define LEDDDR DDRB //data direction port for setting pins as outputs
#define L_I_BIT 1<<7
#define L_E_BIT 1<<4
#define NTASKS
//define Global variables
void(*tasks [NTASKS]) ();
int delay[NTASKS] = {0};
int task_active = 0;
void sleep (int sleeptime){
delay[task_active] = sleeptime;
}
void setup() {
// put your setup code here, to run once:
LEDDDR |= (L_I_BIT | L_E_BIT);
tasks[0] = taskA;
tasks[1] = taskB;
tasks[2] = NULL;
}
void loop() {
// put your main code here, to run repeatedly:
for(task_active = 0; task_active < NTASKS; task_active++){
if((task_active != NULL) && (delays[task_active] <=0 )){
(*tasks[task_active])();//exciute the task
}
else if (tasks[tasl_active] != NULL){
delays[task_active]--;//
}
}
delay(1); //sync
}
void taskA() {
static int flag = 0; //because its a static variable it retains its value
flag ^=1;
sleep(1000);
if(flag){
LEDPORT |= L_I_BIT;
}
else{
LEDPORT &= ~L_I_BIT;
}
}
void taskB(){
static int time = 0;
time++;
if(time == 500){
LED_Port |= L_E_BIT;
}
if(time == 100) {
LEDPORT &= ~L_E_BIT;
time = 0;
}
}