Nested Banks

Status
Not open for further replies.

cackland

Well-known member
Hi All,

I currently have a bank / mode button state set up using the following code, however when 'setupMode' is in state 1, i am trying to set up another layer to have a bank within a bank.

Code:
void setupToggle() {
  setupVal = digitalRead(setupButton);      
  delay(10);                         
  setupVal2 = digitalRead(setupButton);     
  if (setupVal == setupVal2) {                 
    if (setupVal != setupState) {         
      if (setupVal == HIGH) {                
        if (setupMode == 0) {
          Serial.println("Setup On");
          setupPixel(setupLED.Color(255, 255, 255), 0);
          setupMode = 1;
        } else {
          if (setupMode == 1) {
            Serial.println("Setup Off");
            setupPixel(setupLED.Color(0, 0, 0), 0);
            setupMode = 0;
          }
        }
      }
      setupState = setupVal;    // save the new state in our variable
      Serial.println(setupVal);
    }
  }
}

So when I toggle into the button state from 0 - 1, i would then press another button to switch on another bank state with another 8 button states. Hope that makes sense. Have yet to figure this out and thought by creating a new void function and calling it within the 'setupMode 1' it would work.

Any help of how to get this going would be great.

Thanks :)
 
Last edited:
Looking at that code it allows 10 milliseconds for a transition to occur? And only a transition across that delay(10) will give the intended results.

Perhaps the calling code - loop()? should monitor the setupButton for a change and only call setupToggle() when that change occurs?

However - switches and electricity being what they are - there will be some number of transitions likely on each switch toggle.

Doing that using the BOUNCE library to monitor the setupButton should give good results. Look at that library and the samples to see if it offers any ideas?
 
Thanks for your response.

Yes, i've looked at several particular examples and still can't reproduce what i want.

setupButton is the toggle button between setup (on) or setup (off) and val & val2 are the variables.

I was adding:
void toggleMIDI (); (which is the function for calling a toggle button with 8 states. I've tried adding it when the state goes on (1) and it doesn't call. So i removed it.
 
Last edited:
As shown : "digitalRead(setupButton);" has setupButton as the PIN_# being read for state?

The variables that hold the PIN's value are setupVal and setupVal 2
 
I highly recommend using the Bounce library for reading buttons. It automatically handles mechanical chatter without requiring delays.

The normal way of using Bounce is to update all the buttons at the beginning of loop(). Then you can add checks for mybutton.fallingEdge() and respond only when the button is pressed (or released if you like). You can also use myotherbutton.read() to read the current state (absent mechanical chatter) of any other button, if you'd like to have have one button do multiple things.

The important point is to use Bounce and to avoid delays. Craft code which responds to the falling or rising edges. That keeps your project highly responsive, and in the end it's usually much simpler and easier to debug.
 
After you get the Bounce library going, I would suggest you check your state variables first in your code and not at the bottom of 4 nested if statements. I am not a big user of switch and case statements, but they work very nicely with state variables. Here is an example where I will overuse switch case just to show what I am thinking. I will use different variable names than yours to avoid any confusion.

I think you are trying to do is something like this:
Code:
loop(){

   int buttons = read_buttons();   // uses Bounce lib and returns a button event in a bit position
                                              //  for example    0000 0100 means the 3rd button from the right was pressed
   if( buttons ) bank_dispatch( buttons );
   ......... more loop stuff
}

void bank_dispatch( int button ){
static int minor_bank;                // static state variable local to this function only

   switch( major_bank ) :            // major bank is a global variable
        case 0:   bank_zero_functions(button);   break;
        case 1:
             if( button == 1 ){         // we are pretending the rightmost switch #1 enables/disables the bank within a bank
                 minor_bank ^= 1;     // switch which bank we are using in the 2nd bank
                 return;                   // it was a minor bank switch only, we don't want to continue below
              }
              switch( minor_bank ){   // or instead of return statement above we could use an else here.  Maybe that would be clearer.
                    case 0:  bank_one_functions(button);  break;
                    case 1:  bank_one_minor_functions(button);   break;
              } 
     }
}

// example of functions
void bank_one_minor_functions( int button ){
    
      switch( button ){
           //  case 1:      there is no button 1 function as that is the extra bank switch button in this example
                case 2:     volume_up();  break;
                case 4:     volume_down();  break;
                case 8:     fuzz_on();  break;
                case 16:   fuzz_off();   break
.... hope you get the idea
Completely untested code probably has errors.
 
Status
Not open for further replies.
Back
Top