Components and supplies
Solderless Breadboard Half Size
Arduino UNO
Tactile Switch, Top Actuated
Jumper wires (generic)
Resistor 10k ohm
Toggle Switch, Toggle
Apps and platforms
Arduino IDE
Project description
Code
Sketch With Many Switches Serviced By A Single Interrupt Service Routine (ISR)
c_cpp
Configure any number of switches of any type and any wiring scheme to work with a single ISR without any physical interrupt wiring.
1// Ron Bentley, Stafford UK 2// September 2021 3// 4// This example and code is in the public domain and may be used without 5// restriction and without warranty. 6// 7// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8// Example sketch - Multiple switches handled by a single interrupt 9// ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 10// This sketch demonstrates how the ez_switch_lib may be used to handle multiple 11// switches (button & toggle switches in this example)with a single interrupt routine. 12// 13// The use of the ez_switch_lib library for switches provides: 14// * switch type independence 15// * switch circuit type independence 16// * automatic multiple switch debounce handling 17// * parallel switching capabilities, and 18// * automatic interrupt handling for all switches 19// 20// The sketch will use digital pin 2 as the common interrupt pin and 21// pins 3, 4, 5, 6, 7 and 8 as the switch pins. 22// 23// For an understanding of the full capabilities of the 'ez_switch_lib' library see 24// the USER GUIDE: 25// https://github.com/ronbentley1/ez_switch_lib-Arduino-Library/blob/main/ez_switch_lib_user_guide%2C%20v1.04.pdf 26// 27// or a synopsis CRIB SHEET: 28// https://github.com/ronbentley1/ez_switch_lib-Arduino-Library/blob/main/ez_switch_lib%20crib%20sheet.pdf 29// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 30 31#include <ez_switch_lib.h> // declare the switch library 32 33#define common_interrupt_pin 2 // external (common) interrupt pin that all declared switches will use 34 35#define num_switches 6 // number of switches the sketch will declare & use 36 37Switches my_switches(num_switches); // create the 'Switches' instance for the given number of switches 38 39// Declare the data required for each of our switches, 'my_switch_data': 40// 'my_switch_data' layout, one row of data for each switch to be configured, 41// as follows: 42// [][0] = switch type (button_switch or toggle_switch) 43// [][1] = digital pin connected to switch 44// [][2] = the circuit type connecting the switch, here the first 3 switches 45// will have 10k ohm pull down resistors wired (circuit_C1 - INPUT type) 46// and the remainder none (circuit_C2 - INPUT_PULLUP type). 47byte my_switch_data[][3] = 48{ 49 toggle_switch, 3, circuit_C1, // this will be the switch entry for switch_id 0 50 toggle_switch, 4, circuit_C1, // this will be the switch entry for switch_id 1 51 toggle_switch, 5, circuit_C1, // etc 52 button_switch, 6, circuit_C2, 53 button_switch, 7, circuit_C2, 54 button_switch, 8, circuit_C2, 55}; 56 57void setup() { 58 int result; 59 Serial.begin(115200); 60 // Add all switches to the library switch control structure we defined above 61 // and link all switches to the same interrupt pin as a linked output 62 for (uint8_t switch_id = 0; switch_id < num_switches; switch_id++) { 63 result = my_switches.add_switch( 64 my_switch_data[switch_id][0], // switch type 65 my_switch_data[switch_id][1], // digital pin switch is wired to 66 my_switch_data[switch_id][2]); // type of circuit switch is wired as 67 // Check that we have successfully added the current switch or not 68 if (result < 0) { 69 // No slots left or bad parameters 70 Serial.println("*** Failure to add switch.\ 71 Terminated"); 72 Serial.flush(); 73 exit(0); 74 } 75 // now make the linkage for this switch_id to the common interrupt pin 76 result = my_switches.link_switch_to_output( 77 switch_id, // switch to be linked 78 common_interrupt_pin, // digital pin to link to for interrupt 79 LOW); // start with interrupt pin LOW, as interrupt will be triggered on RISING 80 // Check that we have successfully linked the current switch or not 81 if (result < 0) { 82 // Could not link the given switch 83 Serial.println("*** Failure to link switch.\ 84 Terminated"); 85 Serial.flush(); 86 exit(0); 87 } 88 } 89 // set debounce period 90 my_switches.set_debounce(150); // can be reduced if you have good quality switches 91 // Now establish the common interrupt service routine (ISR) that 92 // will be used for all declared switches. 93 attachInterrupt( 94 digitalPinToInterrupt(common_interrupt_pin), 95 switch_ISR, // name of the sketch's ISR handler for switch interrupts 96 RISING); // trigger on a rising pin value 97} // end of setup function 98 99void loop() { 100 // Keep testing switches, and let the interrupt handler do its thing 101 // once a switch is switched to 'on' 102 // We use 'read_switch' function as this deals with both switch types for us transparently. 103 for (uint8_t switch_id = 0; switch_id < num_switches; switch_id++) { 104 if (my_switches.read_switch(switch_id) == switched) { 105 // *** Add any code here, if any, to process this switch as it has been actuated. 106 // *** This is in addition to whatever the ISR does following the triggering 107 // *** of the interrupt, if this switch has been linked to the common interrupt pin. 108 109 } 110 } 111} 112 113// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 114// Common ISR for handling interrupt triggers arising from associated switches 115// when they transition to on then off for button switches and on or off 116// for toggle switches. The routine knows which switch has generated 117// the interrupt because the ez_switch_lib switch read function records the 118// actuated switch in the library variable 'last_switched_id'. 119// 120// The routine does nothing more than demonstrate the effectiveness of the 121// use of a single ISR handling multiple switches by using the serial monitor 122// to confirm correct operation. 123// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 124void switch_ISR() 125{ 126 // Reset the interrupt pin to LOW, so that any other switch will fire the 127 // interrupt whist one or more switches complete their transition stage 128 byte switch_id = my_switches.last_switched_id; // switch id of switch currently switched 129 digitalWrite(my_switches.switches[switch_id].switch_out_pin, LOW); 130 // Reset the soft status of the switch setting to ensure that we get an interrupt event 131 // on the linked interrupt pin at next switch change 132 my_switches.switches[switch_id].switch_out_pin_status = LOW; 133 Serial.print("** Interrupt triggered for switch id: "); 134 Serial.print(switch_id); // the id of the last triggering switch 135 if (my_switches.switches[switch_id].switch_type == button_switch) { 136 // this was a button switch triggering the interrupt 137 // *** Button switch processing 138 // *** Put end user code here to deal with this event 139 140 Serial.println(".. button switch..switched"); 141 } else { 142 // this was a toggle switch triggering the interrupt 143 // determine if the switch transitioned to 'on' or 'off' 144 // and process accordingly 145 Serial.print(".. toggle switch.. "); 146 if (my_switches.switches[switch_id].switch_status == on) { 147 // *** Toggle switch processing for switch being 'on' 148 // *** Put end user code here to deal with this event 149 150 Serial.println("is on"); 151 } else { 152 // *** Toggle switch processing for switch being 'off' 153 // *** Put end user code here to deal with this event 154 155 Serial.println("is off"); 156 } 157 } 158 Serial.flush(); 159} // end of switch_ISR 160
Downloadable files
Multiple Switches, One ISR
Multiple Switches, One ISR
Multiple Switches, One ISR
Multiple Switches, One ISR
Comments
Only logged in users can leave comments