Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
if (WaitForButtonToBeReleased==false)
{
if ( (ButtonIsPressed()) && (Counter>=20) )
{[COLOR="blue"]//If button is still pressed and at least 2s have passed.[/COLOR]
WaitForButtonToBeReleased=true;
osal_start_timerEx( EndptApp_TaskID,EP_JOIN_BUTTON_PRESSING, 100); [COLOR="blue"]//100ms[/COLOR]
Counter=0;
SwitchRelay();
}
else if ( (!ButtonIsPressed()) && (Counter<=4) )
{[COLOR="blue"]//If button is not pressed and less than 400ms have passed.[/COLOR]
Counter=0;
BlinkLED();
[COLOR="seagreen"]osal_stop_timerEx( EndptApp_TaskID, EP_JOIN_BUTTON_PRESSING);[/COLOR]
}
else if ( (!ButtonIsPressed()) && (Counter>4) && (Counter<=20) )
{[COLOR="blue"]//If button is not pressed + more than 400ms have passed + less than 2s
have passed.[/COLOR]
Counter=0;
[COLOR="seagreen"]osal_stop_timerEx( EndptApp_TaskID, EP_JOIN_BUTTON_PRESSING);[/COLOR]
}
else if ( (ButtonIsPressed()) && (Counter<=20) )
{[COLOR="blue"]//If button is pressed and no task was performed yet (neither Relay
switching nor LED blinking).[/COLOR]
PressingCheckingCounter++;
osal_start_timerEx( EndptApp_TaskID,EP_JOIN_BUTTON_PRESSING, 100); [COLOR="blue"]//100ms[/COLOR]
}
}
else [COLOR="blue"]//if Relay was switched and we wait for button to be released.[/COLOR]
{
if ( ButtonIsPressed() ) [COLOR="blue"]//If button is still pressed.[/COLOR]
{
osal_start_timerEx( EndptApp_TaskID,EP_JOIN_BUTTON_PRESSING, 100); [COLOR="blue"]//100ms[/COLOR]
}
else [COLOR="blue"]//If button is not pressed.[/COLOR]
{
WaitForButtonToBeReleased=false;
[COLOR="seagreen"]osal_stop_timerEx( EndptApp_TaskID, EP_JOIN_BUTTON_PRESSING);[/COLOR]
}
WaitForButtonToBeReleased is a boolean variable which receives:This statement "WaitForButtonToBeReleased", how does it changes from true to false?
Therefore, I set it to true only when relay is switched, and to false only when button is released.You have to check that status ("WaitForButtonToBeReleased") constantly, otherwise something not predicted is going to happen. If you just do one verification
The whole attached code is called each time the timer expires, therefore I didnt stop the timer.Another thing is, I just saw you turning the timer on, but I couldn't see where you turn off the timer.
[COLOR="Red"]Counter = 0; //Global Var.
RelayLatched = 0;//Global Var.
SwitchRelease = 0;//Global Var.
OnSwitchInterrupt(){
ActivateTimer(); //"osal_start_timerEx( EndptApp_TaskID,EP_JOIN_BUTTON_PRESSING, 100);"?
RETI; //Return From Switch Int.
}[/COLOR]
OnTimerEvent(){
if (ButtonPressed==1 && RelayLatched == 1 && SwitchRelease == 1){
DelatchRelay();
SwitchRelease = 0;
}
if (ButtonPressed==1 && RelayLatched == 0){ //Let's call 'ButtonPressed' a beautiful name for uC's pin where the button is connected to. And for this code 1 = pressed, 0 = released.
Counter++;
if (Counter>=20){ //If button is still pressed and at least 2s have passed, it is time to latch the relay.
LatchRelay();
RelayLatched = 1;//We are going to use this flag.
Counter = 0;
}
osal_start_timerEx( EndptApp_TaskID,EP_JOIN_BUTTON_PRESSING, 100); [B]//Keep the timer runing.[/B]
}
if (ButtonPressed==0 && RelayLatched == 0){
if (Counter<=4 ) {//If button is not pressed and less than 400ms have passed.
BlinkLED();
}
Counter=0; //If the Counter is >4, just clear it and stop the timer
osal_stop_timerEx( EndptApp_TaskID, EP_JOIN_BUTTON_PRESSING); [B]//TimerStop[/B]
}
if (ButtonPressed==0 && RelayLatched == 1){//If button is not pressed and the relay is latched, then it is a switch release, so we are going to flag it.
SwitchRelease = 1; // Flag that it was a switch release.
osal_stop_timerEx( EndptApp_TaskID, EP_JOIN_BUTTON_PRESSING); [B]//TimerStop[/B]
}
RETI; //Return From Timer Int.
}