Issue with the Control Surface Library and 3pdt switches

So I have a pretty simple project I'm looking to do. I want to hook up 10 foot switches, 8 to send midi control messages and the two others to increment up and decrement up. I'd also like to have an RGB led to identify which bank the controller is currently on. Before on the breadboard with a few two pin buttons and led the code worked perfectly, but since I moved on to the 3pdt I've encountered a recurring problem. Rather than going from low when you press in and back to high on release these switches go from high to low with each press.

The issue that has arisen is that when the switch is changed from high to low it cycles through the increments as if a standard momentary switch was being held down. But for my purposes I just want it to switch one and stop.

I'm not sure if that made sense but here's my code

#include <Control_Surface.h> // Include the Control Surface library

int redPin = 9;
int greenPin = 10;
int bluePin = 11;
 
//comment this line if using a Common Cathode LED
#define COMMON_ANODE

// Instantiate a MIDI interface for use with the Hairless MIDI<->Serial bridge.
HairlessMIDI_Interface midi;

// Instantiate 3 Banks, with one address per bank.
// Banks can change the address of the buttons below.
// Having 3 banks means that each button has 3 possible addresses.
// 1 address per bank means that changing the bank increments the
// address (controller number) by 1.
Bank<4> bank(1);
//   │       └───── number of addresses per bank
//   └───────────── number of banks

// Instantiate a Bank selector to control which one of the three Banks is active.
// Pressing the push button will select the next bank, adding 1 to the address
// of the two buttons below. After 3 pushes, you're back at Bank 1.
IncrementDecrementSelector<4> selector = {
    bank,       // Bank to manage
    {2, 3},     // push button pins (increment, decrement)
    Wrap::Wrap, // Wrap around
};

// Instantiate two push buttons that send Control Change events when pressed
// or released.
Bankable::CCButton button1 = {
  bank, // bank determines the address offset, relative to the base address
  5,    // push button pin
  55,   // base address (controller number)
};
Bankable::CCButton button2 = {
  bank, // bank determines the address offset, relative to the base address
  4,    // push button pin
  65,   // base address (controller number)
};
Bankable::CCButton button3 = {
  bank, // bank determines the address offset, relative to the base address
  6,    // push button pin
  75,   // base address (controller number)
};
Bankable::CCButton button4 = {
  bank, // bank determines the address offset, relative to the base address
  7,    // push button pin
  85,   // base address (controller number)
};
Bankable::CCButton button5 = {
  bank, // bank determines the address offset, relative to the base address
  8,    // push button pin
  95,   // base address (controller number)
};
Bankable::CCButton button6 = {
  bank, // bank determines the address offset, relative to the base address
  A5,    // push button pin
  105,   // base address (controller number)
};
Bankable::CCButton button7 = {
  bank, // bank determines the address offset, relative to the base address
  A3,    // push button pin
  115,   // base address (controller number)
};
Bankable::CCButton button8 = {
  bank, // bank determines the address offset, relative to the base address
  A1,    // push button pin
  45,   // base address (controller number)
};

void setup() {
  Control_Surface.begin(); // Initialize Control Surface
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);  
}

void loop() {
  Control_Surface.loop(); // Update the Control Surface
  
  if (bank.getSelection() == 0) setColor(14, 130, 150);
  if (bank.getSelection() == 1) setColor(110, 0, 45);
  if (bank.getSelection() == 2) setColor(200, 0, 0);
  if (bank.getSelection() == 3) setColor(255, 120, 0);
}

void setColor(int red, int green, int blue)
{
  #ifdef COMMON_ANODE
    red = 255 - red;
    green = 255 - green;
    blue = 255 - blue;
  #endif
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

You cannot use toggle switches with the IncrementDecrementSelector, it works with momentary switches only. If you want to use toggle switches, you'll have to program the logic yourself. You can use the Button class (example) to handle debouncing and edge detection, and then use the Bank::select() method to change the bank.

Pieter

Alright thanks, I made an attempt to see if I could get the code to work. I didn't really get what you sent me on how to do edge detection with my particular buttons and there wasn't a lot of info on the internet either so my attempt at that is probably flawed as well. But the code I set up for edge detection at least made it through the IDE's verification process (but was totally untested). When I tried to add functions to set the bank that's when I ran into trouble. I cut my code down to what is important to bank selection and edge detection but if you want I can post my full code. Thanks!

int  lastButtonState;
unsigned long startTime;
int buttonvar = 1;

Bank<4> bank(1);
void setup() {
    
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
pinMode(2, INPUT_PULLUP);

}

void loop() {
  int buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState)
  {
    startTime = millis();
  }
  {
  lastButtonState = buttonState;
  flash();
  }
  {
  if (buttonvar==1)Bank::select(0)
  if (buttonvar==2)Bank::select(1)
  if (buttonvar==3)Bank::select(2)
  if (buttonvar==4)Bank::select(3)
  }
}


}

void flash()
{
  if (millis() - startTime < 500UL)
  {
    if (buttonvar == 1,2,3)
    {buttonvar++;
      
    }
    
  }
  {
    if (buttonvar == 4)
  {
    buttonvar = 1;
  }
  
 
  }
  }


}

I've updated it to be "if (buttonvar==1)Bank.select(0)".... for all selections and have made it through the verification phase but it when uploaded to the arduino the bank change button does nothing productive.

The following works for me:

#include <Control_Surface.h> // Include the Control Surface library

// Instantiate a MIDI interface for use with the Hairless MIDI<->Serial bridge.
HairlessMIDI_Interface midi;

// Instantiate 3 Banks, with one address per bank.
// Banks can change the address of the buttons below.
// Having 3 banks means that each button has 3 possible addresses.
// 1 address per bank means that changing the bank increments the
// address (controller number) by 1.
Bank<4> bank(1);
//   │       └───── number of addresses per bank
//   └───────────── number of banks

// Switch to change the bank
Button bankSwitch = 2; // switch connected between ground and pin 2

// Instantiate a push button that sends Control Change events when pressed
// or released.
Bankable::CCButton button1 = {
  bank, // bank determines the address offset, relative to the base address
  5,    // push button pin
  55,   // base address (controller number)
};

void setup() {
  Control_Surface.begin(); // Initialize Control Surface
  bankSwitch.begin();      // Enables pull-up resistor
}

void loop() {
  Control_Surface.loop();

  bankSwitch.update();
  if (bankSwitch.getState() == Button::Falling ||
      bankSwitch.getState() == Button::Rising) {
    setting_t newSetting = bank.getSelection() + 1;
    if (newSetting >= bank.getNumberOfBanks())
      newSetting = 0;
    bank.select(newSetting);
  }
}