Need help fast: many buttons as switches

ok, so i know how to use a button as a switch using variables like

int state = HIGH;
int reading;
int previous = LOW;

but do i need a set of said variables for EACH button i wish to make a switch, for example

int state1 = HIGH;   //Button1 state
int reading1;   //Read Button1
int previous1 = LOW;  //Button1 LOW

int state2 = HIGH; //Button2 state
int reading2;  //Read Button2
int previous2 = LOW;  //Button2 LOW

would that work, becuase if so, i have a lot of work to do, but from my understanding, i need a set for each button, but i figured id ask you guys. also can i made a void function that happens only if a button is pressed and its previous state is low as well, something like:

void setup(){
void loop(){
void Button1(){
if(buttonpressed && previously not pressed){
//do stuff

Yes, lot of work :cold_sweat:
Every button bounces, so you have add debouncing for every button. Even more work :cold_sweat: :cold_sweat:
If you make a class for a single button, that would make it easier.
Or use a library, for example the Bounce2-master :

ok, what is a class, oh wait, nvm ill look it up in the reference, ok thanks, i jsut needed to to know but what about the functions, one for each button, its setup with a homemade LED strip(a sh**ton of LEDs in a row on a breadboard) and it has a 'debug keyboard' and each button on that will light up a certain part(s) of the strip to test it, what if i hade a function for each mode button as well as the pattern buttons? like:

Void setup(){
 setup program
void Debug1(){
if(this && this == TRUE){
do stuff}
void loop(){

and so on for each button?

Luckily C++ allows you to define your own storage containers (that's how I think of them). Look up structures.

// here's a struct similar to one I've used
// the last two items are pointers to functions

struct myButton {
  byte pin;
  int state;
  boolean debounceFlag;
  unsigned long timer;
  void (*pressed)(void);
  void (*released)(void);

void whenPressed()
    // do something

void whenReleased()
    // do something

// then we can instance an array switches like

myButton buttonSwitch[] = 
    {2, HIGH, false, 0UL, &whenPressed, &whenReleased},
    {3, HIGH, false, 0UL, &whenPressed, &whenReleased}

// both of these are pointed to the same functions for press or release but the functions could all be different
// you may not actually need a released function

// Then to read the first switch's pin signal you do this


// The functions can be called by


Does this seem helpful?

it seems a little advanced, i can try it, but i figure i could always just do the pile of if statements in the loop, i can also look up structures, is it in the arduino reference?

Instead of the complexity of a class, an array of pin numbers, an array of states (current and previous) and a function that dealt with an index seems far simpler to me.

Thanks for all the help but i have one more question, the program is only designed to take input from one button at a time, in that case, do i need one of these:

long time = 0; 
long debounce = 200;

and one of these:
  time = millis(); 
for each button, or just these and thats it?

My old question still stands but i figured i would show u guys what i have now to give you an idea of what it is im doing:

/* This code is to control a simple LED strip with 2 yellow LEDs on the sides, 6 red and blue LEDs inbetween those, and 5 clear LEDs inbetween those.
    -Made by Mike P. (Mike44449) */
#include <toneAC.h>
//The following variables are for the mode selection
const int modeButton1 = 51;
const int modeButton2 = 49;
const int modeButton3 = 47;
const int modeButton4 = 45;
//The following variables are for the debug keyboard
const int LYB = A7;    //Left yellow button     
const int RYB = A6;    //Right yellow button    
const int YB = A5;  //Yellow only button        
const int RB = A4;  //Red only button           
const int WB = A3; //White only button          
const int BB = A2; //Blue only button           
const int ACB = A1; //All color button          
const int IMB = A0;  //Idle mode button         
//The following is the ON indicator light
const int powerOnLED = 53;
//The following is for the LED strip
const int LY1 = 22;
const int LY2 = 23;
const int RY1 = 14;
const int RY2 = 42;
const int red1 = 24;
const int red2 = 25;
const int red3 = 26;
const int red4 = 27;
const int red5 = 28;
const int red6 = 29;
const int clear1 = 30;
const int clear2 = 31;
const int clear3 = 32;
const int clear4 = 33;
const int clear5 = 34;
const int blue1 = 35;
const int blue2 = 36;
const int blue3 = 37;
const int blue4 = 38;
const int blue5 = 49;
const int blue6 = 40;
//ButtonState Components
int button1State = HIGH;
int button1Reading;           
int button1Previous = LOW;
int button2State = HIGH;     
int button2Reading;           
int button2Previous = LOW;
int button3State = HIGH;     
int button3Reading;           
int button3Previous = LOW;
int button4State = HIGH;     
int button4Reading;           
int button4Previous = LOW;
int LYBState = HIGH;     
int LYBReading;           
int LYBPrevious = LOW;
int RYBState = HIGH;     
int RYBReading;           
int RYBPrevious = LOW;
int YBState = HIGH;     
int YBReading;           
int YBPrevious = LOW;
int RBState = HIGH;     
int RBReading;           
int RBPrevious = LOW;
int WBState = HIGH;     
int WBReading;           
int WBPrevious = LOW;
int BBState = HIGH;     
int BBReading;           
int BBPrevious = LOW;
int ACBState = HIGH;     
int ACBReading;           
int ACBPrevious = LOW;
int IMBState = HIGH;     
int IMBReading;           
int IMBPrevious = LOW;

void setup() {
  // put your setup code here, to run once:
  //Turn on the green power LED
  //Label all inputs/outputs as so:
  //Mode buttons
  //Debug Keyboard

void loop() {
  // put your main code here, to run repeatedly:


I use this library: GitHub - JChristensen/JC_Button: Arduino library to debounce button switches, detect presses, releases, and long presses.

Use use a library for that. It does all the work for you so you don’t have to reinvent the wheel (Dutch saying). I use the Bounce library.

Also, as a rule of thumb. If you are writing code (like variables) and you think, mm, this looks like a lot of repetition/work there is probably a better/easier way to do it. Like using array’s :wink:

You just end up with something like

/* This code is to control a simple LED strip with 2 yellow LEDs on the sides, 6 red and blue LEDs inbetween those, and 5 clear LEDs inbetween those.
    -Made by Mike P. (Mike44449) */
#include <toneAC.h>
#include <Bounce2.h>

//The following variables are for the mode selection
const byte ModeButtonsMap[] = {51, 49, 47, 45};
Bounce modeButtons[sizeof(ModeButtonsMap)];
/* Don't need this
const int modeButton1 = 51;
const int modeButton2 = 49;
const int modeButton3 = 47;
const int modeButton4 = 45; */
//The following variables are for the debug keyboard
const byte KeyboardMap[] = {A7, A6, A5, A4, A3, A2, A1, A0};
Bounce keyboard[sizeof(KeyboardMap)];
#define LYB	0
#define RYB	1
#define YB	2
#define RB	3
#define WB	4
#define BB	5
#define ACB	6
#define IMB	7

//made the following list a lot smaller ;)
//The following is the ON indicator light
const byte powerOnLED = 53;
//The following is for the LED strip
const byte ly[] = {22, 23, 14, 42};
const byte red[] = {24, 25, 26, 27, 28, 29};
const byte clear[] = {30, 31, 32, 33, 35};
const byte blue[] = {35, 36, 37, 38, 49, 40};
//ButtonState Components
/* Are now gone :p

void setup() {
  // put your setup code here, to run once:
  //Turn on the green power LED
  //Label all inputs/outputs as so:
  //Mode buttons
  for(byte i = 0; i < sizeof(ModeButtonsMap); i++){
    pinMode(ModeButtonsMap[i], INPUT_PULLUP);
  //Debug Keyboard
  for(byte i = 0; i < sizeof(KeyboardMap); i++){
    pinMode(KeyboardMap, INPUT_PULLUP);
  for(byte i = 0; i < sizeof(ly); i++){
    pinMode(ly[i]. OUTPUT);
  for(byte i = 0; i < sizeof(red); i++){
    pinMode(red[i]. OUTPUT);
  for(byte i = 0; i < sizeof(clear); i++){
    pinMode(clear[i]. OUTPUT);
  for(byte i = 0; i < sizeof(blue); i++){
    pinMode(blue[i]. OUTPUT);

void loop() {
  //Update (check/read) all the mode buttons, incl debounce
  for(byte i = 0; i < sizeof(ModeButtonsMap); i++){
  //Update (check/read) whole keyboard, incl debounce
  for(byte i = 0; i < sizeof(KeyboardMap); i++){
  // put your main code here, to run repeatedly:


Now you can just use the library functions like:

modeButtons[0].read(); //return state of first button
modeButtons[1].changed(); //returns if the second button changed since last update/loop
modeButtons[2].fell(); //returns if the third buttons became pressed since the last loop (because of pullups the buttons are active low)

//same for keyboard
keyboard[RB].rose(); //returns if the red buttons was released since the last update/loop


Do yourself a favour. Read what @PaulS said in Reply #5

Don't waste time solving a bounce problem until you are sure you need to. The code in several things at a time has a very simple anti-bounce system. The code in planning and implementing a program does not need one.

Generally speaking the bounce problem is solved by leaving a few millisecs between successing reads of a switch. One interval will be sufficient for all switches. Create a readSwitches() function and leave a small interval between calls to it.

You also need to be careful to distinguish between the changing state of a switch and bounce. Sometimes you need to be sure that the switch was released before you take notice of the next press. That is a state-change issue and is quite different from bounce.


the program is only designed to take input from one button at a time, in that case, do i need one of these...and one of these...for each button, or just these and thats it?

If you need to keep track of the last time a particular switch was pressed, you need one or each per switch. If it only matters when ANY switch was last pressed (which would be extremely unlikely), then just one of each is needed.

Using "click" type buttons can reduce bounce times and issues.