#define LED_OUTPUT 8
#define BUTTON_INPUT 9
unsigned char LED_STATE_OFF=0;
unsigned char LED_STATE_ON=1;
unsigned char buttonState = 0; // Variable to store the button state
unsigned int lastButtonState = 0; // Variable to store the last button state
unsigned char led_Output = LOW; // Variable to store the LED state //COUNTER FOR 10MS
unsigned char PushSwitchDebCount=0;/*debounce counter for input switch*/
static unsigned int counterfor10ms=0;
static unsigned int counterfor100ms=0;
unsigned char LED_Current_State=0;
void Task_10ms(void);
void LED_on(void);
void LED_off(void);
void LED_debounce(void);
void LED_state_manager(void);
void timer_init();
void Task_100ms(void);
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=62; /* 625 - 10ms, 62 - 1ms*/
sei();
}
void pin_init() // digital pins
{
pinMode(LED_OUTPUT, OUTPUT);
pinMode(BUTTON_INPUT, INPUT);
}
void setup() /* This function will execute only 1 time*/
{
timer_init();
pin_init();
LED_State_Init();
}
/* Do not modify*/
void loop()
{
// put your main code here, to run repeatedly:
Task_10ms();
Task_100ms();
}
void Task_10ms()
{
if(counterfor10ms>=10)
{
Read_button();
counterfor10ms=0;
}
}
void Task_100ms()
{
if(counterfor100ms>=100)
{
LED_state_manager();
counterfor100ms=0;
}
}
void LED_State_Init(void)
{
led_Output = LOW;
digitalWrite(LED_OUTPUT, led_Output);
LED_Current_State =0;
}
void Read_button(void)
{
buttonState = digitalRead(BUTTON_INPUT);
//Serial.println(buttonState);
}
void LED_state_manager(void)
{
LED_off();
}
void LED_off(void)
{
if(LED_Current_State==0 && HIGH==buttonState)
{
digitalWrite(LED_OUTPUT, led_Output);
LED_debounce();
}
}
void LED_debounce(void)
{
if(PushSwitchDebCount++>0)
{
Serial.println(PushSwitchDebCount );
PushSwitchDebCount = 0;
if(LOW == led_Output)
{
led_Output = HIGH;
digitalWrite(LED_OUTPUT, led_Output);
LED_Current_State=LED_STATE_OFF;
LED_on();
}
else if(HIGH == led_Output)
{
led_Output = LOW;
digitalWrite(LED_OUTPUT, led_Output);
LED_Current_State=LED_STATE_ON;
LED_off();
}
else
{
/* do nothing*/
}
}
else
{
/* do nothing*/
}
}
void LED_on(void)
{
if(LED_Current_State==1 && buttonState ==LOW)
{
digitalWrite(LED_OUTPUT, led_Output);
LED_debounce();
}
}
// #define BUTTON_INPUT 9 // Pin connected to the push button
// #define LED_OUTPUT 8 // Pin connected to the LED
// unsigned char buttonState = 0; // Variable to store the button state
// unsigned char lastButtonState = 0; // Variable to store the last button state
// unsigned char led_Output = LOW; // Variable to store the LED state
// unsigned char counterfor100ms=0; //COUNTER FOR 10MS
// unsigned char PushSwitchDebCount=0; /*debounce counter for input switch*/
// unsigned char counterfor10ms;
// void LED_State_Init(void);
// void LED_State_Manager(void);
// void Task_10ms(void);
// void Task_100ms(void);
// enum State_LED
// {
// LED_STATE_OFF,
// LED_STATE_DEBOUNCE,
// LED_STATE_ON
// } ;
// static State_LED LED_Current_State;
// static State_LED LED_Previous_State;
// /* Do not modify*/
// ISR(TIMER1_COMPA_vect) // ISR
// {
// TCNT1=0;
// counterfor10ms++;
// counterfor100ms++;
// }
// /* Do not modify*/
// 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();
// }
// /* Do not modify*/
// void digitalpin_init() // digital pins
// {
// pinMode(LED_OUTPUT, OUTPUT);
// pinMode(BUTTON_INPUT, INPUT);
// }
// /* Do not modify*/
// void setup() /* This function will execute only 1 time*/
// {
// timer_init();
// digitalpin_init();
// LED_State_Init();
// Serial.begin(9600);
// }
// /* Do not modify*/
// void loop()
// {
// // put your main code here, to run repeatedly:
// Task_10ms();
// Task_100ms();
// }
// void Task_10ms(void)
// {
// if(counterfor10ms>=1) /* enters for every 10ms*/
// {
// /*task_pushbutton();*/
// LED_State_Manager();
// counterfor10ms=0;
// }
// }
// void Task_100ms(void)
// {
// }
// /* Start to write your logic from here*/
// /* Led state machine init function*/
// void LED_State_Init(void)
// {
// led_Output = LOW;
// digitalWrite(LED_OUTPUT, led_Output);
// LED_Current_State = LED_STATE_OFF;
// }
// /* State machine function for LED_STATE Manager. Executes every 10ms*/
// void LED_State_Manager(void)
// {
// buttonState = digitalRead(BUTTON_INPUT);
// switch(LED_Current_State)
// {
// case LED_STATE_OFF:
// if(HIGH == buttonState)
// {
// LED_Current_State = LED_STATE_DEBOUNCE;
// }
// break;
// case LED_STATE_DEBOUNCE:
// if(PushSwitchDebCount++>4)
// {
// PushSwitchDebCount = 0;
// if(LOW == led_Output)
// {
// led_Output = HIGH;
// digitalWrite(LED_OUTPUT, led_Output);
// LED_Current_State = LED_STATE_ON;
// }
// else if(HIGH == led_Output)
// {
// led_Output = LOW;
// digitalWrite(LED_OUTPUT, led_Output);
// LED_Current_State = LED_STATE_OFF;
// }
// else
// {
// /* do nothing*/
// }
// }
// else
// {
// /* do nothing*/
// }
// break;
// case LED_STATE_ON:
// if(LOW == buttonState)
// {
// LED_Current_State = LED_STATE_DEBOUNCE;
// }
// break;
// }
// }