Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Relay causes a problem to Button's functioning.

Status
Not open for further replies.
It may be caused by a lot of things.

Before you exit the button interrupt, do you turn off the 100 ms timer?
 
Hey Hayato.

I probably had a mistake in the code.
I rewrote it now and before testing it on Sunday (I won't be able to do it on Saturday), i'd like to post it up and receive your opinion please.
Code:
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]
      }
 
Last edited:
This statement "WaitForButtonToBeReleased", how does it changes from true to false?

You have to check that status ("WaitForButtonToBeReleased") constantly, otherwise something not predicted is going to happen. If you just do one verification.

Another thing is, I just saw you turning the timer on, but I couldn't see where you turn off the timer.
 
Hey Hayato.
Thanks a lot for the help.

This statement "WaitForButtonToBeReleased", how does it changes from true to false?
WaitForButtonToBeReleased is a boolean variable which receives:
- false = when button was released after switching the relay (default value when unit is powered-up).
- true = when relay was switched and button is not released yet.

You have to check that status ("WaitForButtonToBeReleased") constantly, otherwise something not predicted is going to happen. If you just do one verification
Therefore, I set it to true only when relay is switched, and to false only when button is released.
How shall I additionally check it?

Another thing is, I just saw you turning the timer on, but I couldn't see where you turn off the timer.
The whole attached code is called each time the timer expires, therefore I didnt stop the timer.
However, it is indeed wise to stop the timer just in case, so I stopped the timer in the attached code after the button is released and after the LED blinks - 3 spots which I marked with green.

When the button is pressed and the Uc vectors to its ISR, all it does is to call this timer and set it to expire after 10ms.
 
Last edited:
I don't know the uC you are using, so I'll suggest some lines:

Code:
[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.
}
 
Thank you so much Hayato.
I'll implement these great additions in my mechanism.

I checked today the mechanism I wrote here and it works well so far, however i'll keep checking it to see that it remains steady and good.

I really thank you, I learned so much from you!
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top