Momentary Spdt Switch Library and Sample Menu

111

3

Posted

Introduction: Momentary Spdt Switch Library and Sample Menu

This instructable provides a small library I've made for using a spring loaded spdt momentary switch. I made the library so I can easily use the switches for a menu. I've provided debouncing and a means to accelerate the output if the switch is held down. This is my first version of the software. I may rewrite this to use interrupts in the future.

Step 1: Wiring the Sample Circuit.

First off, I tend to be old school and just draw thing on paper. This was my first time using fritzing and I could not find a matching switch to put into the diagram. I apologize for the cut and paste button.

That being said, the switches I've used, have 5 pins. The 3 center pins are connected together. The center pin is wired to Vcc and the outside pins are each wired to the arduino input pins and to ground through a 10k resistor.

The switches are spring loaded so that when you are not touching them they return to the center position. I have not yet found a part # for the pieces I have in the photos. Any type of momentary spdt on-off-on should be fine.

There are some extra wires on sda and scl in the image. Just ignore them. I'd been using that test bed for a different project

Step 2: Installing the Library

Download the attached archive file and extract it into your Arduino IDE library directory. Explaining how to add a library is not within the scope of this instructable, especially since there are plenty of tutorials on how to add a library.

The library contains a sample Menu using two spdt switches. Once the library is installed the example can be found in the IDE under "example->Todd_SpDt->SpDtDemo". The first switch is used to scroll through menu items and the second switch changes values depending on which menu you are in. The menu output for the example is sent through the serial port at 9600 baud.

Step 3: Basic Library Commands

Commands

  • SpDt switch1=SpDt(upPin,downPin,acceleration,function,delaytime);
    • switch1 is your object name
    • upPin and downPin are the pin numbers where the switch is connected
    • acceleration is a counter updated by the read function. it is used for accelerating output
    • function is uint16_t, it controls what output the object creates.
    • wait is an unsigned integer, it is the time used for debounce
  • switch1.read()
    • this reads the pin states and returns a value based on the number in function
      • up=(2*function) -1
      • down=2*function
      • so if the function number is 2 then..
        • up=3
        • down=4
    • this function also updates the acceleration variable.
      • when the switch is held down the acceleration variable adds 1 every output cycle
      • you may access this variable to alter the iteration of something for example
    • yourMenuAction(switch1.read())
      • this will feed the output of the read into yourMenuAction()
      • in my example this function contains a switch/case statement to perform menu actions.
  • switch1.function
    • returns the function number
  • switch1.function=2
    • sets the function number
    • the function must be a positive integer starting at 1
  • switch1.delayTime
    • returns the delay time
  • switch1.delayTime=225
    • sets the delay time
  • switch1.accelerate
    • returns the value of the accelerate flag ( true or false)
  • switch1.accelerate=false
    • sets the accelerate flag
    • When set to true, if the button is held down through 5 outputs the debounce delay will be ignored and will speed up output. Debounce is irrelevant when a button is held.
  • switch1.set(funct, delaytime , accelerate)
    • sets the function, delaytime and accelerate variables/flag

Step 4: Sample Code

#include <Todd_SpDt.h>

// button configuration
const uint8_t sw1upPin=2;
const uint8_t sw1downPin=3;
const uint8_t sw2upPin=5;
const uint8_t sw2downPin=6;
unsigned int sw1Acceleration=0;
unsigned int sw2Acceleration=0;

//  (upPin, downpin, loop counter, function number, bounce wait)
// if a single button press acts like many then put a number greater than 225
// The bounce wait is hardware dependent
SpDt sw1=SpDt(sw1upPin,sw1downPin,sw1Acceleration,100,225);  
SpDt sw2=SpDt(sw2upPin,sw2downPin,sw2Acceleration,1,225);   

// some menu parameters
const uint8_t firstMenuItem=1;
const uint8_t lastMenuItem=5;

// menu acceleration parameter list,  0 is not used.
// ie sw2.accelerate=accel[firstMenuItem]  
// if for example sw2.accelerate=true then bounce checking will 
// stop after 5 outputs
bool accel[]={false,true,true,true,false,true};

//  sample variables
uint8_t red=128;
uint8_t green=128;
uint8_t blue=128;
uint8_t someOption=1;
long someValue=0L;


// increase() and decrease() iterate by 1 and check for upper and lower bounds respectively
uint8_t increase(uint8_t value)
{ 
  if ((value+1)<255) {return (value+1);}
  return 255; 
}

uint8_t decrease(uint8_t value)
{
  if (value>0) {return (value-1);} 
  return 0;
}


// this is an example of how to use the acceleration variable
// this function will use an increasingly larger number to iterate
// something as the button is held down longer
uint16_t launch()
{
  uint8_t iteration=1000;
  if (sw2Acceleration<80) {iteration=200;}
  if (sw2Acceleration<50) {iteration=20;}
  if (sw2Acceleration<20) {iteration=1; } 
  return (iteration);
}


// function to go to the next or previous menu
// true for next, false for previous
void changeMenu(bool up)
{
  if (up) 
    {
     if (sw2.function==lastMenuItem) { sw2.set(firstMenuItem,accel[firstMenuItem]);} 
     else { ++sw2.function; sw2.accelerate=accel[sw2.function];}
     Serial.print("Menu up  :");
    }
  else 
    {
    if (sw2.function==firstMenuItem) {sw2.set(lastMenuItem,accel[lastMenuItem]);}
    else {--sw2.function; sw2.accelerate=accel[sw2.function];}  
    Serial.print("Menu down:");
    }
 Serial.print(sw2.function);
 Serial.print("   :");
 switch(sw2.function)
 {
  case 1: Serial.println("Red Menu"); break;  
  case 2: Serial.println("Green Menu"); break;
  case 3: Serial.println("Blue Menu"); break;
  case 4: Serial.println("Option Menu"); break;
  case 5: Serial.println("Long# Menu"); break;
 }
}







// this function is for interpreting the button output
void switchAction(uint16_t action)
{
 switch(action)
 {

  case 0: break;

  // the following two entries are for switch 1
  //  function 100 outputs 199 for up, and 200 for down
  case 199: changeMenu(1); break; 
  case 200: changeMenu(0); break; 

  // the following are for switch 2
  // button function 1 outputs 1 for up and 2 for down
  case 1: red=increase(red); Serial.print("Red up      :"); Serial.println(red); break; 
  case 2: red=decrease(red); Serial.print("Red down    :"); Serial.println(red); break; 

  case 3: green=increase(green); Serial.print("Green up    :"); Serial.println(green); break; 
  case 4: green=decrease(green); Serial.print("Green down  :"); Serial.println(green); break; 
  
  case 5: blue=increase(blue); Serial.print("Blue up     :"); Serial.println(blue); break; 
  case 6: blue=decrease(blue); Serial.print("Blue down   :"); Serial.println(blue); break; 
 
  case 7: ++someOption; Serial.print("Some Option up   :"); Serial.println(someOption); break; 
  case 8: --someOption; Serial.print("Some Option down :"); Serial.println(someOption); break; 

  case 9: someValue+=launch(); Serial.print("Some value up   :"); Serial.println(someValue); break; 
  case 10: someValue-=launch(); Serial.print("Some value down :"); Serial.println(someValue); break; 
 }
  
}
// -------------------------------------------------- setup -----------------------------------
void setup() 
{
Serial.begin(9600);
}


void loop()
{      
 // reads button state, returns a value to the function switchAction(), Acceleration variable updated
 switchAction(sw1.read());  
 switchAction(sw2.read());
}





Step 5: Sample Output

This is a sample of output from the menu. I've included the long list of numbers at the end to show how the acceleration example is done. The program will iterate by larger values as the button is held down longer. When the button is released it will return to the state where numbers advance slowly. The red green and blue variable will go slowly for the first five and then increase the numbers fast. The static image of the output doesn't do it justice. I am using this for an led controller I'm working on.

Menu up  :1   :Red Menu
Menu up  :2   :Green Menu
Menu up  :3   :Blue Menu
Menu up  :4   :Option Menu
Menu up  :5   :Long# Menu
Some value up   :1
Some value up   :2
Some value up   :3
Some value up   :4
Some value up   :5
Menu down:4   :Option Menu
Some Option up   :2
Some Option up   :3
Some Option up   :4
Some Option down :3
Some Option down :2
Menu down:3   :Blue Menu
Blue up     :129
Blue up     :130
Blue up     :131
Blue up     :132
Blue up     :133
Blue up     :134
Menu down:2   :Green Menu
Green down  :127
Green down  :126
Green down  :125
Green down  :124
Green down  :123
Green down  :122
Green down  :121
Green down  :120
Green down  :119
Green down  :118
Menu up  :3   :Blue Menu
Menu up  :4   :Option Menu
Menu up  :5   :Long# Menu
Some value up   :6
Some value up   :7
Some value up   :8
Some value up   :9
Some value up   :10
Some value up   :11
Some value up   :12
Some value up   :13
Some value up   :14
Some value up   :15
Some value up   :16
Some value up   :17
Some value up   :18
Some value up   :19
Some value up   :20
Some value up   :21
Some value up   :22
Some value up   :23
Some value up   :24
Some value up   :25
Some value up   :45
Some value up   :65
Some value up   :85
Some value up   :105
Some value up   :125
Some value up   :145
Some value up   :165
Some value up   :185
Some value up   :205
Some value up   :225
Some value up   :245
Some value up   :265
Some value up   :285
Some value up   :305
Some value up   :325
Some value up   :345
Some value up   :365
Some value up   :385
Some value up   :405
Some value up   :425
Some value up   :445
Some value up   :465
Some value up   :485
Some value up   :505
Some value up   :525
Some value up   :545
Some value up   :565
Some value up   :585
Some value up   :605
Some value up   :625
Some value up   :825
Some value up   :1025
Some value up   :1225
Some value up   :1425
Some value up   :1625
Some value up   :1825
Some value up   :2025
Some value up   :2225
Some value up   :2425
Some value up   :2625
Some value up   :2825
Some value up   :3025
Some value up   :3225
Some value up   :3425
Some value up   :3625
Some value up   :3825
Some value up   :4025
Some value up   :4225
Some value up   :4425
Some value up   :4625
Some value up   :4825
Some value up   :5025
Some value up   :5225
Some value up   :5425
Some value up   :5625
Some value up   :5825
Some value up   :6025
Some value up   :6225
Some value up   :6425
Some value up   :6625
Some value up   :6857
Some value up   :7089
Some value up   :7321
Some value up   :7553
Some value up   :7785
Some value up   :8017
Some value up   :8249
Some value up   :8481
Some value up   :8713
Some value up   :8945
Some value up   :9177
Some value up   :9409
Some value up   :9641
Some value up   :9873
Some value up   :10105
Some value up   :10337
Some value up   :10569
Some value up   :10801
Some value up   :11033
Some value up   :11265
Some value up   :11497
Some value up   :11729
Some value up   :11961
Some value up   :12193
Some value up   :12425
Some value up   :12657
Some value up   :12889
Some value up   :13121
Some value up   :13353
Some value up   :13585
Some value up   :13586
Some value up   :13587
Some value up   :13588
Some value up   :13589
Some value up   :13590
Some value up   :13591
Some value up   :13592
Some value up   :13593
Some value up   :13594
Some value up   :13595
Some value up   :13596
Some value up   :13597
Some value up   :13598
Some value up   :13599
Some value up   :13600
Some value up   :13601
Some value up   :13602
Some value up   :13603
Some value up   :13604
Some value up   :13605
Some value up   :13606
Some value up   :13607
Some value up   :13627
Some value up   :13647
Some value up   :13667
Some value up   :13687
Some value up   :13707
Some value up   :13727
Some value up   :13747
Some value up   :13767
Some value up   :13787
Some value up   :13807
Some value up   :13827
Some value up   :13847
Some value up   :13867
Some value up   :13887
Some value up   :13907
Some value up   :13927
Some value up   :13947
Some value up   :13967
Some value up   :13987
Some value up   :14007
Some value up   :14027
Some value up   :14047
Some value up   :14067
Some value up   :14087
Some value up   :14107
Some value up   :14127
Some value up   :14147
Some value up   :14167
Some value up   :14187
Some value up   :14207
Some value up   :14407
Some value up   :14607
Some value up   :14807
Some value up   :15007
Some value up   :15207
Some value up   :15407
Some value up   :15607
Some value up   :15807
Some value up   :16007
Some value up   :16207
Some value up   :16407
Some value up   :16607
Some value up   :16807
Some value up   :17007
Some value up   :17207
Some value up   :17407
Some value up   :17607
Some value up   :17807
Some value up   :18007
Some value up   :18207
Some value up   :18407
Some value up   :18607
Some value up   :18807
Some value up   :19007
Some value up   :19207
Some value up   :19407
Some value up   :19607
Some value up   :19807
Some value up   :20007
Some value up   :20207
Some value up   :20439
Some value up   :20671
Some value up   :20903
Some value up   :21135
Some value up   :21367
Some value up   :21599
Some value up   :21831
Some value up   :22063
Some value up   :22295

Share

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • First Time Author Contest 2018

    First Time Author Contest 2018
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

Comments