making a trigger a toggle

how can i set my relay on my controller to just turn a pwm port to on or off so that i dont have to hold down the trigger?

Hi, I’ll post below what I’m using right now… As a sidenote, I have two variables you’ll need. One is name “port1_trigger” and the other “Lift.”

void Lift_Control ( void )
{
      if ( port1_trigger == 1 ) // If the trigger was pressed last.
      {
            Last_Button_Pressed = 1 ; // Then the trigger was pressed last.
      }
      if ( port1_thumb == 1 )
      { // If the thumb button was pressed last.
            Last_Button_Pressed = 2 ; // Then the thumb button was pressed last.
      }
      if ( Last_Button_Pressed == 1 ) // If the last button pressed was the trigger, then....
      {
            Lift = 1  ; // Lift the platform!
      }
      else
      {
            Lift = 0 ; // Don't lift the platform.
      }
      SetRelay ( 1 , Lift , -1 ) ; // The spike relay will have the value of the variable "Lift."
}

I hope that helps :slight_smile:
-Nathan


static char p1_trig_state = 1;
static unsigned char p1_trig_pressed = 0;

if(p1_sw_trig && !p1_trig_pressed) {
    p1_trig_state *= -1;
    p1_trig_pressed = 1;
}
else if(!p1_sw_trig)
    p1_trig_pressed = 0;

if(p1_trig_state==1)
    pwm01 = 255;
else
    pwm01 = 127;

with any toggle code, if your output switches state rapidly a few times after your input is pressed, you might need to add a little delay to debounce the input

well, if you’re trying to turn a motor/solenoid/whatever else on/off only, without variable speed, i would use a spike. the code is simple.



static char trigold = 0; // Define this with global scope

if(p1_sw_trig == 1 && trigold == 0){
       relay1_fwd = !relay1_fwd;
}

trigold = p1_sw_trig;


That should toggle relay1’s fwd direction each time there is a 0->1 transition on the port 1 trigger.