// #define BUTTON_INPUT 4 // Pin connected to the push button
// #define LED_OUTPUT 13 // 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;
// unsigned char currentValuePush;
// unsigned char prv_Value_push;
// unsigned char push_count;
// unsigned char push_count2;
// unsigned char ledFlag;
// unsigned char count_off_Flag;
// unsigned char count_on_Flag;
// void LED_State_Init(void);
// void LED_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_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);
// push_count=0;
// push_count2=0;
// ledFlag=HIGH;
// // LED_Current_State = LED_STATE_OFF;
// //currentValuePush=LOW;
// prv_Value_push=0;
// }
// /* State machine function for LED_STATE Manager. Executes every 10ms*/
// void LED_Manager(void)
// {
// buttonState = digitalRead(BUTTON_INPUT);
// //Serial.println(buttonState);
// currentValuePush=buttonState;
// if (ledFlag==HIGH && currentValuePush==1)
// {
// if( push_count2==1)
// {
// Serial.println("bro");
// push_count2=0;
// ledFlag=LOW;
// // Serial.println("i am in low");
// }
// else if(ledFlag==HIGH && push_count<4)
// {
// Serial.println("hello");
// push_count++;
// }
// else
// {
// // push_count2++;
// ledFlag=LOW;
// // Serial.println(ledFlag);
// push_count=0;
// digitalWrite(LED_OUTPUT, HIGH);
// }
// }
// else if( ledFlag==LOW && currentValuePush==1 )
// { Serial.println(" i am in low");
// if( push_count2==1 )
// {
// push_count2=0;
// ledFlag=HIGH;
// Serial.println("i am in low");
// }
// else if(ledFlag==LOW && push_count<4)
// {
// push_count++;
// // Serial.println(ledFlag);
// }
// else
// {
// push_count2++;
// Serial.println(" i am in low");
// ledFlag=HIGH;
// push_count=0;
// digitalWrite(LED_OUTPUT,LOW);
// }
// }
// }
// //Serial.println(PushSwitchDebCount);
// // if(currentValuePush!=prv_Value_push)
// // {
// // if(PushSwitchDebCount++>4) /* this will check the debouncing */
// // {
// // Serial.println(push_count);
// // PushSwitchDebCount=0;
// // // Serial.println(PushSwitchDebCount);
// // if(HIGH==currentValuePush && push_count%2==0)
// // {
// // digitalWrite(LED_OUTPUT, HIGH); /* led high */
// // push_count=push_count+1;
// // }
// // else if(HIGH==currentValuePush && push_count%2!=0)
// // {
// // digitalWrite(LED_OUTPUT, LOW);/* led low */
// // push_count=push_count+1;
// // }
// // prv_Value_push=currentValuePush; /* it will store the current value in prv */
// // }
// // }
// // //prv_Value_push=currentValuePush; /* it will store the current value in prv */
#define LED_OUTPUT 13
#define BUTTON_INPUT 4
static unsigned int LED_STATE_OFF=0;
static unsigned int LED_STATE_ON=1;
static unsigned char buttonState = 0;
static unsigned int cur_ButtonState = 0; // Variable to store the button state
static unsigned int lastButtonState = 0; // Variable to store the last button state
static unsigned char led_Output = LOW; // Variable to store the LED state //COUNTER FOR 10MS
static unsigned int button_count=0;/*debounce counter for input switch*/
static unsigned int counterfor10ms=0;
static unsigned int counterfor100ms=0;
static unsigned int debounce_count=1;
static unsigned char LED_Current_State=0;
static void Task_10ms(void);
static void LED_on(void);
static void LED_off(void);
static void LED_debounce(void);
static void task_manager(void);
void timer_init();
//static 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 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_manager();
}
static void task_manager(void)
{
if(counterfor10ms>=1)
{
Task_10ms();
counterfor10ms=0;
}
}
static void pin_init() // digital pins
{
pinMode(LED_OUTPUT, OUTPUT);
pinMode(BUTTON_INPUT, INPUT);
}
static void Task_10ms()
{
Read_button();
LED_state_manager();
}
static void Read_button(void)
{
buttonState = digitalRead(BUTTON_INPUT);
cur_ButtonState=buttonState;
Serial.println(cur_ButtonState);
}
static void LED_State_Init(void)
{
led_Output = LOW;
LED_Current_State =0;
}
static void LED_state_manager(void)
{
LED_debounce();
}
static void LED_debounce(void)
{
if(cur_ButtonState==1)
{
if( LED_Current_State ==0)
{
if( debounce_count<4)
{
debounce_count++;
}
else{
digitalWrite(LED_OUTPUT,HIGH);
debounce_count=0;
led_Output=HIGH;
}
//LED_Current_State=1;
}
if( LED_Current_State ==1)
{
if( debounce_count<4)
{
debounce_count++;
}
else{
digitalWrite(LED_OUTPUT,LOW);
debounce_count=0;
led_Output=LOW;
}
// LED_Current_State=0;
}
}
else{
if(led_Output==HIGH)
{
LED_Current_State=1;
}
if(led_Output==LOW)
{
LED_Current_State=0;
}
}
}