Help me understand the switch code

Hi, I am using a code from member carlos. He has been a great help, but wants me to get help in the forum to help others. I need to use one switch for each LED state and have the LED’s only lit when the button is held high. Right now it changes LED tables on press and stays on when the button is released. I think I can just add another #define SWITCH_TABLE, but I’m not sure how to add it to the bool switchState(void), or how to make it only true when held down. I do search, and have read tons, but this sketch is the most complicated I’ve seen. Yes I am new to arduino and have a working sequencer but with delays, so I wanted to play further. I would love some help. Thanks, adown

#include "Multi_Blink4.h"  // type definitions

#define  SWITCH_TABLE	2  // switch between patterns using this input

// Blink Table T - Modify this table to suit whatever the output requirements are 
// Add or delete lines as required to achieve the desired effects.
// Have multiple tables and switch between them to create different effects based on external inputs
// To add additional states the structure in the header file needs to be modified

ledTable  T1[] = // Running Lights
//Pin  St WUp  State 0              State 1              etc
{ 
  { 10, 0, 0, {{MB_ON, 500, 0, 0}}  },   //{MB_OFF, 50, 0, 0},  {MB_LOOP, 0, LOOP_PARAM(0, 3), 0},  {MB_ON, 250, 0, 0},  {MB_OFF, 450, 0, 0}} },
  { 11, 0, 0, {{MB_ON, 500, 0, 0}}  }, // {MB_ON, 400, 0, 0}}  },  //{MB_OFF, 100, 0, 0},  {MB_LOOP, 0, LOOP_PARAM(1, 3), 0},  {MB_ON, 250, 0, 0}}  },
  { 12, 0, 0, {{MB_ON, 500, 0, 0}}  }, // {MB_ON, 300, 0, 0}}  },  //{MB_OFF, 100, 0, 0},  {MB_NULL,0,0,0}, {MB_NULL,0,0,0}} },
  
};

ledTable  T2[] = // Turn Signal
//Pin  St WUp  State 0              State 1              etc
{ 
  { 10, 0, 0, {{MB_ON, 500, 0, 0},   {MB_OFF, 200, 0 ,0},  {MB_LOOP, 0, LOOP_PARAM(0, 255), 0},  }  },
  { 11, 0, 0, {{MB_OFF, 150, 0, 0}, {MB_ON, 350, 0, 0},   {MB_OFF, 200, 0 ,0},  {MB_LOOP, 0, LOOP_PARAM(0, 255), 0},  }  },
  { 12, 0, 0, {{MB_OFF, 250, 0, 0}, {MB_ON, 250, 0, 0},   {MB_OFF, 200, 0, 0}, {MB_LOOP, 0, LOOP_PARAM(0, 255), 0},  }  },
  
};

ledTable T3[] = // Brakes
{
  { 5, 0, 0, {{MB_ON, 500, 0, 0}}  }, //  {MB_OFF, 200, 0 ,0},  {MB_LOOP, 0, LOOP_PARAM(0, 255), 0},  }  },
  { 6, 0, 0, {{MB_OFF, 150, 0, 0}, {MB_ON, 350, 0, 0}}  }, // {MB_OFF, 200, 0 ,0},  {MB_LOOP, 0, LOOP_PARAM(0, 255), 0},  }  },
  { 7, 0, 0, {{MB_OFF, 250, 0, 0}, {MB_ON, 250, 0, 0}}  }, // {MB_OFF, 200, 0, 0}, {MB_LOOP, 0, LOOP_PARAM(0, 255), 0},  }  },
  
};

void BlinkInit(ledTable *pT, uint8_t tableSize)
// Initialise one Blink Table
{
  for (uint8_t i=0; i < tableSize; i++, pT++)
  {
    pinMode(pT->ledPin, OUTPUT);
    
    pT->nextWakeup = 0;
    digitalWrite(pT->ledPin, LOW);
	pT->currentState = 0;
    for (uint8_t j=0; j<MAX_STATE; j++)
    {
      pT->state[j].counter = CTR_UNDEF;
    }
  }
}

void MultiBlink(ledTable *pT, uint8_t tableSize)
// Finite state machine that uses the data passed to it to run the show
{
  for (int i=0; i < tableSize; i++, pT++)
  {
      uint8_t  cs = pT->currentState;  // current state shortcut

    // check if the state active time has expired (ie, it is less than current time)
    if (millis() >= pT->nextWakeup)
    {
      pT->nextWakeup = millis() + pT->state[cs].activeTime;

      switch (pT->state[cs].stateID)
      {
        case MB_OFF:
        case MB_ON:    // Write digital value
        {
          digitalWrite(pT->ledPin, pT->state[cs].stateID == MB_ON ? HIGH : LOW);
          pT->currentState = (pT->currentState + 1) % MAX_STATE;
        }
        break;
        
        case MB_LOOP:  // loop back to specified state if there is still count left
        {
          // first time in this state? Check the counter
          if (pT->state[cs].counter == CTR_UNDEF)
          {
            pT->state[cs].counter = LOOP_SP_GET(pT->state[cs].data);
          }

          // loop around or keep going?          
          if (pT->state[cs].counter-- > 0)
          {
            pT->currentState = LOOP_STATE_GET(pT->state[cs].data);
            pT->nextWakeup = 0;  // do it immediately
          }
          else 
          {
            pT->state[cs].counter = CTR_UNDEF; // set up loop counter
            pT->currentState = (pT->currentState + 1) % MAX_STATE;
          }
        }
      }
    }
  }
}

bool switchState(void)
// Detect a rising edge from the switch
// return true when detected
{
  static bool	bLastHigh = true;
  bool  b = (digitalRead(SWITCH_TABLE) == HIGH);
  bool  bRet = !bLastHigh && b;

  bLastHigh = b;

  return(bRet);
}

void InitTables(void)
// Initialise all the LED tables
{
  BlinkInit(T1, ARRAY_SIZE(T1));
  BlinkInit(T2, ARRAY_SIZE(T2));
  BlinkInit(T3, ARRAY_SIZE(T3));
}

void setup(void)
{
	InitTables();
}

void loop(void)
{
  static uint8_t nTable = 0;

  // check if we need to switch table
  if (switchState())
  {
	 nTable++;
	 InitTables();
  }

  // run with the selected table
  switch(nTable)
  {
  case 0:	MultiBlink(T1, ARRAY_SIZE(T1));	break;
  case 1:	MultiBlink(T2, ARRAY_SIZE(T2));	break;
  case 2:	MultiBlink(T3, ARRAY_SIZE(T3));	break;
  default:	nTable = 0;
  }    
}

I think I can just add another #define SWITCH_TABLE

No, you can't A #define statements defines a name and a value. The value is substituted everywhere the name appears. If you add another #define statement with the same name, only the last value assigned to the name gets substituted.

ok. Describe clearly what you want to do.

How many patterns? What is the switch supposed to do?

I have three LED states in the table and would like each state to have its own physical switch. If switch 1 is pressed do LED state 1. If switch 2 is pressed, do LED state 2, and so on. I was trying not to go with if statements and keep the sketch as written. I have switch_state on pin 2, can I add switch_state2 on pin 3. Then I would like the switches to only illuminate the LED's while it is held high. Thanks, adown

So then you need to add a state zero for when no switches are pressed. When this condition is detected then write the appropriate LEDs to the dark state.

I was trying not to go with if statements

Why ?

If switch 1 is pressed do LED state 1. If switch 2 is pressed, do LED state 2,

Sounds like a job for if to me.

morganS, I am actually going to illuminate the running lights on power up, so I will only have two physical switches. One for turn and one for brake.

UKhelibob, maybe it would have sounded better if I used "when". I would like to keep the format of the sketch in tack. I might have to go with if statements if I cant use it the way it is.

Thanks for the replies

bool switchState(void)
// Detect a rising edge from the switch
// return true when detected
{
  static bool  bLastHigh = true;
  bool  b = (digitalRead(SWITCH_TABLE) == HIGH);
  bool  bRet = !bLastHigh && b;

  bLastHigh = b;

  return(bRet);
}

This will only work for one switch as it has a static in it and uses the #define in the function. If you want to have multiple switches then I suggest that you use the MD_Switch library (as per your PM) and create one object per switch.

I have three LED states in the table and would like each state to have its own physical switch.

Seems to me like you have 3 states but probably only need 2 switches. Won't the running lights be on by default?

You will need to have if statements because you are not really just using one status value. The if statements may just set nTable to the correct value or directly call the Multiblink function. Something like this will work with the right values for your switches, replacing the current code in loop() that sets

if (switchBrakesIsOn)
  nTable = 3;
else if (switchTurnIsOn)
  nTable = 2;
else
  nTable = 1;