Runaway buttons

I am building a universal remote control using the IR remote and Button libraries with an aircraft stick grip for control. The stick has 8 normally open buttons (including a trigger) that are setup for PULLDOWN per the Button library. Because I only have 8 buttons to work with, it's imperative that a couple of the buttons can select more than one function. I want my trigger switch to control volume on my AV receiver - the first pull will increase volume and the 2nd pull will decrease volume. I pieced together some code but it no workie so well - when I pull the trigger, the volume rapidly increases, and when I release and pull again, it stops the increase but doesn't start decreasing the volume.

The missile uncage button is for selecting the mode on the AV receiver. For some reason it works better than the trigger button but I notice that in case 1 and case 2 it continues to emit IR code until case 3 is selected. For example, if I press the button the first time, "Net/USB" mode come up, but the IR led keeps emitting signals for the "net/USB" code. Only when I select case 3 (ipod port) does the LED emit just once. I'm pretty new at writing code so I'm sure it's something simple...Anyway, here's a code snippet from the affected part:

//Trigger Switch Volume UP/DOWN

if (trigger.uniquePress())
pressCount1++;

switch(pressCount1)

{
case 1:

  irsend.sendNEC (0x4BB640BF,32); //Volume Up code
  delay(40);
  Serial.println("Volume UP");

  break;

case 2:

  irsend.sendNEC (0x4BB6C03F,32); // Volume Down code
  delay(40);
  pressCount1 = 0;
  Serial.println("Volume Down");

  break;  

}


//Missile uncage Button Multi-select 

if (uncage.uniquePress())
pressCount2++;

switch(pressCount2)

{
case 1:

  irsend.sendNEC (0x4B40E11E,32); //Net/USB mode ;
  delay(40);
  Serial.println("NET/USB Mode");

  break;

case 2:

  irsend.sendNEC (0x4BB6708F,32); //CBL/SAT mode
  delay(40);
  Serial.println("CBL/SAT Mode");

  break;

case 3:

  irsend.sendNEC (0x4B3551AE,32);//IPod Port
  delay(40);
  Serial.println("Ipod PORT Mode");


  pressCount2 = 0;
  break;  

}

Any help would be greatly appreciated!

if (trigger.uniquePress())
pressCount1++;

The only thing that is controlled by the if statement is the increment of pressCount1 or pressCount2.

The value stays constant, then, so the switch statements are executed every time this code snippet is called. Once you pull the trigger or press the button, the action that results happens again and again.

It seems to me like you want the first switch statement to be executed only when trigger.uniquePress() is true, and the second switch statement to be executed only when uncage.uniquePress() is true. Add some curly braces after the if statements.

OK, i added the curly brackets after the if statements. I added them like this:

//Trigger Switch Volume UP/DOWN

if (trigger.uniquePress())
{
pressCount1++;

switch(pressCount1)

{
case 1:

  irsend.sendNEC (0x4BB640BF,32); //Volume Up code
  delay(40);
  Serial.println("Volume UP");

  break;

case 2:

  irsend.sendNEC (0x4BB6C03F,32); // Volume Down code
  delay(40);
  pressCount1 = 0;
  Serial.println("Volume Down");

  break;  
}

}

It partially solved the trigger issue - now i get one step up in volume on the 1st press and one step down on the volume for the 2nd press. Instead of being a runaway, the button is now stuck in Ground Hog Day :-) I'd like the button to continue to increase volume up until released on the first press, and then decrease volume until released until the 2nd press. jus like a Vol Up/Down button on a remote.

The uncage switch is doing the same thing - continually emitting IR signals when selecting Case 1 and case 2, but only once when selecting case 3. I added the curly brackets in the same place. No idea why that's still happening...

I'd like the button to continue to increase volume up until released on the first press, and then decrease volume until released until the 2nd press. jus like a Vol Up/Down button on a remote.

Then, uniquePress() is not the method you want to be using, is it?

The uncage switch is doing the same thing - continually emitting IR signals when selecting Case 1 and case 2, but only once when selecting case 3. I added the curly brackets in the same place. No idea why that's still happening...

My crystal ball has a crack in it. I can't see where you put the curly braces. Before posting your code, though, run the autoformat tool, to line up the braces, and properly indent the code. That makes it much easier to see the structure of the code.

Thanks Paul, got the curly brackets in the right place and now the uncage switch works like a champ. As for the trigger, you're right - but what do I need to do to get it to toggle volume up and down? I tried the isPressed function and that's not working either...

I tried the isPressed function and that's not working either...

Everybody's favorite lament. You need to explain what you expected to have happen, what actually did happen, and what code you used. It would also be nice to be clued in on exactly what the trigger object is.

This is the grip.The trigger switch is the trigger on the front side. I figured out the pinout and it's just a simple switch with two leads:

Again, here is the code I was using. As it is now, when I pull the trigger, the volume goes up one step. When I pull it again, it goes down one step...

/*Trigger Switch Volume UP/DOWN. When the trigger is pulled, volume should increase while held. Once released, the volume should stay where its at. When the trigger is pulled again, the volume should decrease when held and continue decreasing until let go */

if (trigger.isPressed()) { pressCount1++;

switch(pressCount1)

{ case 1:

irsend.sendNEC (0x4BB640BF,32); //Volume Up code delay(40); Serial.println("Volume UP");

break;

case 2:

irsend.sendNEC (0x4BB6C03F,32); // Volume Down code delay(40); pressCount1 = 0; Serial.println("Volume Down");

break; }

}

It would also be nice to be clued in on exactly what the trigger object is.

Not that trigger!

if (trigger.isPressed())

This trigger! What is trigger an instance of?

Every time the (physical) trigger is determined to be pressed, pressCount1 is incremented. Wrong. The pressCount1 variable should be incremented only if trigger.uniquePress() returns true. The action should then be performed while(trigger.isPressed()).

Oh, OK. I guess you meant this line of code:

Button trigger = Button(4,PULLDOWN);//Volume Up/Down

I guess you meant this line of code:

I did.

The uniquePress() and isPressed() functions play well together. If trigger.uniquePress() returns true, increment pressCount1. Then, in the case sections, create a while(trigger.isPressed()) loop that will increment/decrement the volume.

Thanks! I’ll give it a go tonight when I get home.