Nicks, SwitchManager Class for handling switch pushes FYI

This isn't a question, I thought that others might find this interesting and keep it mind if they ever come across a similar need.

I had a need to control a 7 segment display which outputs 6 different functions. Using just one switch and Nick Gammon’s Class, SwitchManager.h I was able to easily achieve what I wanted.

You could easily use a similar process to create a menu structure application.
Thanks Nick

//See Nick Gammon's  http://www.gammon.com.au/forum/?id=11955
//See reply #1 for the class ZIP file to download
//
//     Setup:
//we have only one switch
//it is connected to input 4
//with only one switch we can detect what the user wants to do
//by simply looking at the current state of the switch (whether it is pushed or not)
//and how long it has been since the last push

#include <SwitchManager.h>
SwitchManager modeSW;  // create the object

//****************************************************************
//just some stuff to make this example compile

const byte ModeSW = 4;             //input from the Mode push button switch     
byte modeNumber;                   //is the current # of the display mode
byte counter;
boolean flag;
int i;
int pulsesSent;

//****************************************************************

void setup ()
{
  modeSW.begin (ModeSW, modeSwitchManager);  
}


//****************************************************************

void loop()
{
  //check to see what's happening with the Mode switch
  modeSW.check();  
  
}



//                M O D E   S W I T C H   M A N A G E R
//****************************************************************
// function looks after pushes on the Mode switch
void modeSwitchManager (const byte newState, const unsigned long interval)
{


  // H I G H   M O D E   S W I T C H   P U S H   S E C T I O N
  
  //is the new switch position HIGH and has it been that way for >= 1 second?
  if (newState == HIGH && interval >= 1000UL)
  {
    // whatever code you want to execute, example clear a counter
    counter = 0;

    return;
  }

  //*****************
  //is the new switch position HIGH and has it been that way for < 1 second?
  if (newState == HIGH)
  {
    return;  //ignor the switch push or do what ever you need to do for this situation
  }

  
  
  // L O W   M O D E   S W I T C H   P U S H   S E C T I O N

  //was the switch pressed for <= 1/4 second 
  //i.e. are we switching to a new mode?
  if (interval <= 250)
  {
    modeNumber++;

    //only go up to mode 6 maximum or whatever you need it to be for your application
    if (modeNumber > 6)
    {
      modeNumber = 0;
    }

    //example of 6 differnt diplay modes. (on a LCD or 7 segment display)
    //update to the new display mode
    switch (modeNumber) //execute whatever code is necessary for each mode
    {
      //------------------------------------------------------------
    case 0:           //Input pulse counter   displays CXXXXXXX

      break;

      //------------------------------------------------------------
    case 1:          //Frequency counter       displays FXXXXXXX
      //if flag = true, means we can start counting
      flag = true;
      i = 0;

      break;

      //------------------------------------------------------------
    case 2:          //High Pulse Width        displays HXXXXXXX
      //prepareForInterrupts ();             

      break;

      //------------------------------------------------------------
    case 3:          //Low Pulse Width         displays LXXXXXXX
      //prepareForInterrupts ();             

      break;

      //------------------------------------------------------------
    case 4:          //Auto range voltmeter    displays EXXXXXXX
      //
      break;

      //------------------------------------------------------------
    case 5:          //Pulse output 1X         displays P1_XXXXX
      //clear7219();                            
      //start out at zero
      //pulsesSent = 0UL;

      break;

      //------------------------------------------------------------
    case 6:          //Pulse output 10X        displays P10XXXXX
      //clear7219();
      //start out at zero
      //pulsesSent = 0UL;

      break;

      //------------------------------------------------------------

    } //  END of switch(mode)

    return;
  }

  //Lets check for some different LOW Mode switch pushes
  
  //*****************
  //if we are in modeNumber 5 and there was a switch push >= 3 seconds  
  //Have to wait initially 3 seconds then 1 sec there after 
  if (modeNumber == 5 && interval >=3000)
  {
    pulsesSent++;
    //pulseOutput(1);   //send 1 pulse out on the pulse probe

    return;  
  }  

  //if we met the previous condition we now only have to wait 1 second to do the same thing
  else if (modeNumber == 5 && pulsesSent > 0 && interval >=1000)
  {
    pulsesSent++;
    //pulseOutput(1);   //send 1 pulse out on the pulse probe

    return;  
  }  

  //*****************

  return;

}  //                  END of modeSwitchManager()