//exercise 3: LED ON first button press, LED OFF for second button press, LED ON for next button press etc.
#define TRUE 1u
#define FALSE 0u
#define Button_Led 13
#define Button_Pin 12
//unsigned char Button_Flag=FALSE;
unsigned char Led_Status=FALSE;
unsigned char Led_Prev_State=TRUE;
unsigned char Led_Curr_State=FALSE;
//unsigned char Last_Button_State=FALSE;
//volatile unsigned char Tick10msCounter = 0;
//unsigned char Debounce_Delay=50;
//unsigned char Last_Debounce_Time=0;
unsigned char Current_Button_State;
unsigned char Prev_Button_State=FALSE;
/*#define ledPinRed 13
#define ledPinGreen 2
#define ledPinYellow 3
#define Button_Pin 12
#define Button_Led 13
unsigned char RED_LED_Counter = 0;
unsigned char redStatus=false;
unsigned char GREEN_LED_Counter = 0;
unsigned char greenStatus=false;
unsigned char YELLOW_LED_Counter = 0;
unsigned char yellowStatus=false;*/
volatile unsigned char Tick10msCounter = 0;
volatile unsigned char Tick40msCounter = 0;
volatile unsigned char Tick100msCounter = 0;
unsigned char Tick30msCounter = 0;
unsigned int Last_Debounce_Time=0;
unsigned int Debounce_Delay=50;
unsigned char Button_State=FALSE;
unsigned char Led_State=FALSE;
unsigned int Press_Count=0;
unsigned char Press_Detected_Flag=FALSE;
byte Adc_Read_Value;
void Init(void);
void Interrupt_init(void);
void setup() {
Init();
Serial.begin(2000000);
Interrupt_Init();
}
void Init()
{
pinMode(Button_Led, OUTPUT);
pinMode(Button_Pin, INPUT);
}
void Interrupt_Init()
{
cli(); //stop interrupts for till we make the settings
TCCR1A = 0; // Reset entire TCCR1A to 0
TCCR1B = 0; // Reset entire TCCR1B to 0
TCCR1B |= B00000100;
TIMSK1 |= B00000010; //Set OCIE1A to 1 so we enable compare match A
OCR1A=62; //1ms
sei();
}
//With the settings above, this IRS will trigger each 1ms.
void loop()
{
// put your main code here, to run repeatedly:
if(Tick10msCounter>10)//10ms task
{
Tick10msCounter = 0;
Button_Press();
}
if(Tick40msCounter>40)//40 ms task
{
Tick40msCounter = 0;
}
if(Tick100msCounter>100)//100 ms task
{
Tick100msCounter = 0;
}
}
void Button_Press()
{
/*int Current_Button_State=digitalRead(Button_Pin);
if(Current_Button_State!=Prev_Button_State)
{
Last_Debounce_Time=millis();
}
if((millis()-Last_Debounce_Time)>Debounce_Delay)
{
if(Current_Button_State!=Button_State)
{
Button_State=Current_Button_State;
}
if(Button_State==HIGH)
{
Led_State=!Led_State;
}
}
digitalWrite(Button_Led,Led_State);
Prev_Button_State=Current_Button_State;*/
//Serial.println("10 ms task: Press Button");
/*if(Tick30msCounter++>10) //30ms Debounce_delay
{
Tick30msCounter=0;
if ((digitalRead(Button_Pin)==HIGH))
{
if ((Led_Prev_State==TRUE)&&(Led_Curr_State==FALSE))
{
Serial.println("ON");
digitalWrite(Button_Led,HIGH);
Led_Prev_State=FALSE;
Led_Curr_State=TRUE;
}
else if ((Led_Prev_State==FALSE)&&(Led_Curr_State==TRUE))
{
Serial.println("OFF");
digitalWrite(Button_Led,LOW);
Led_Prev_State=TRUE;
Led_Curr_State=FALSE;
}
else
{
}
} */
Current_Button_State=digitalRead(Button_Pin);
//if(Prev_Button_State!=Current_Button_State)
//{
if ((Current_Button_State==HIGH))
{
Serial.println("1");
if(Press_Count++>3)
{
Press_Count=0;
if(Press_Detected_Flag==FALSE)
{
if((Led_Status==FALSE))
{
Serial.println("ON");
digitalWrite(Button_Led,HIGH);
//Led_Prev_State=FALSE;
// Led_Curr_State=TRUE;
Led_Status=TRUE;
}
else if ((Led_Status==TRUE))
{
Serial.println("OFF");
digitalWrite(Button_Led,LOW);
Led_Prev_State=TRUE;
Led_Curr_State=FALSE;
Led_Status=FALSE;
}
else{}
Press_Detected_Flag=TRUE;
Serial.println("Press is Detected");
}
else{}
}
else
{
Serial.println("ignore signal");
}
}
else
{
Serial.println("Press is Not Detected");
Press_Detected_Flag=FALSE;
Press_Count=0;
//Prev_Button_State=Current_Button_State;
}
//Prev_Button_State=Current_Button_State;
//}
}
ISR(TIMER1_COMPA_vect){
TCNT1 = 0;
Tick10msCounter++;
Tick40msCounter++;
Tick100msCounter++;
}
/*
// exercise 3: LED ON first button press, LED OFF for second button press, LED ON for next button press etc.
//without debouncing
#define TRUE 1u
#define FALSE 0u
//10ms task
#define Button_Led 13
#define Button_Pin 12
//unsigned char Button_Flag=FALSE;
unsigned char Led_Status=FALSE;
//unsigned char Last_Button_State=FALSE;
volatile unsigned char Tick10msCounter = 0;
unsigned char Debounce_Delay=50;
unsigned char Last_Debounce_Time=0;
unsigned char Last_Button_State=FALSE;
unsigned char Button_State=FALSE;
unsigned char Led_State=FALSE;
void Button_Press(void);
void setup() {
pinMode(Button_Led,OUTPUT);
pinMode(Button_Pin,INPUT);
//Set the pin to be OUTPUT
Serial.begin(9600);
cli(); //stop interrupts for till we make the settings
TCCR1A = 0; // Reset entire TCCR1A to 0
TCCR1B = 0; // Reset entire TCCR1B to 0
TCCR1B |= B00000011; //Prescalar 256
TIMSK1 |= B00000010; //Set OCIE1A to 1 so we enable compare match A
OCR1A=250; //1ms
sei(); //Enable back the interrupts
}
void loop()
{
// put your main code here, to run repeatedly:
if (Tick10msCounter>10) //10ms task
{
Tick10msCounter = 0;
Button_Press();
}
}
void Button_Press()
{
/*int Current_State=digitalRead(Button_Pin);
if(Current_State!=Last_Button_State)
{
Last_Debounce_Time=millis();
}
if((millis()-Last_Debounce_Time)>Debounce_Delay)
{
if(Current_State!=Button_State)
{
Button_State=Current_State;
}
if(Button_State==HIGH)
{
Led_State=!Led_State;
}
}
digitalWrite(Button_Led,Led_State);
Last_Button_State=Current_State;
Serial.println("10 ms task: Press Button");
if((millis()-Last_Debounce_Time)>Debounce_Delay)
{
if ((digitalRead(Button_Pin)==HIGH) && (Led_Status==FALSE))
{
digitalWrite(Button_Led,HIGH);
Led_Status=TRUE;
}
else if ((digitalRead(Button_Pin)==HIGH) && (Led_Status==TRUE))
{
digitalWrite(Button_Led,LOW);
Led_Status=FALSE;
}
else
{
}
Last_Debounce_Time=millis();
}
}
ISR(TIMER1_COMPA_vect){
TCNT1 = 0;
Tick10msCounter++;
}*/