Multiple Button setup

Hello all, I am newer to world of Arduino, I have many years experience with Event Driven processors, applications, etc... (mainly OOP). I am working on a personal project (Cornhole boards) where button presses will change the color of an LED strip (2812) in a bargraph pattern for score. I am trying to come up with the best course of action to my processes. I can make both buttons work just fine, but coming from the object oriented world, my sketch looks messy (IMHO). As of right now I am just reading the state of the buttons, and incrementing a count, and manipulating the bargraph accordingly all in the loop(). I toyed with the Idea of Interrupts, calling another "function" upon a High value read of a button, and a few other methods. I just wanted to get others take, or ideas. Just a point in a direction is all I need to get moving. Examples are great as well.

Since you did not post your code, only you can decide how "messy" it is.

Interrupts are NOT the answer if you are just detecting a button press. You can take a look at the state change detection example in the IDE (File->examples->02.digital->State Change Detection) to see how a typical button press gets handled. Once detected, you can call another function or just execute some code in place. Totally your choice.

You can also break out the bargraph update as a separate function and call as needed when something changes.

If you want more help, please post your complete sketch (using code tags)

blh64:
Since you did not post your code, only you can decide how "messy" it is.

Interrupts are NOT the answer if you are just detecting a button press. You can take a look at the state change detection example in the IDE (File->examples->02.digital->State Change Detection) to see how a typical button press gets handled. Once detected, you can call another function or just execute some code in place. Totally your choice.

You can also break out the bargraph update as a separate function and call as needed when something changes.

If you want more help, please post your complete sketch (using code tags)

This is the path I am currently heading down, So Thank you, I was mainly looking for guidance, not really code review. I do understand the importance of that however. If I run into an issue, I will definitely post code.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

We need to see your current code, so we cab advise on how to accomplish what you are trying to do.
That way we can see your code structure and provide accurate suggestions.

Thanks.. Tom.. :slight_smile:

If you like OOP I have a mechButton class you may like. It set up its own pin, debounces, can be polled for current state true/false, can have callback if you so desire, or even used as a base class for exiting new things.

Ih the Arduino IDE's "manage libraries" menu search for LC_baseTools. You will find mechButton, and all its friends, in there.

-jim lee

This is what I have as of right now, a few revisions in, and not quite complete, just in the testing phases.

#include <FastLED.h>

#define NumLEDS 9
#define LEDPIN 7
#define ScoreLimit 4
#define Btn1_Pin 2
#define Btn2_Pin 4


CRGB leds[NumLEDS];

int Teams[2];

int TeamStart[] = {1,8};

CRGB TeamColor[] = {CRGB (255,0,0),CRGB (0,0,255)};

int btn1Prev = LOW;
int btn2Prev = LOW;

unsigned long lastDebounceTime_1 = 0;
unsigned long lastDebounceTime_2 = 0;
unsigned long debounceDelay = 50;

void setup() {

  Serial.begin(9600);
  // put your setup code here, to run once:
  pinMode(Btn1_Pin,INPUT);
  //pinMode(Btn2_Pin,INPUT);

  FastLED.addLeds<WS2812, LEDPIN, GRB>(leds, NumLEDS);

  for (int i = 0; i <= NumLEDS; i++){
    leds[i] = CRGB (255,255,255);
    FastLED.show();
  }

  leds[TeamStart[0]] = TeamColor[0];
  leds[TeamStart[0] + (ScoreLimit + 1)] = TeamColor[0];
  leds[TeamStart[1]] = TeamColor[1];
  leds[TeamStart[1] + (ScoreLimit + 1)] = TeamColor[1];

  FastLED.show();
  Teams[0] = 0;
  Teams[1] = 0;
  
}

void loop() {

    int ChangeMade = 0;

    int btn1 = digitalRead(Btn1_Pin);
    int btn2 = digitalRead(Btn2_Pin);

    delay(50);

    
    if(btn1 == 1 && btn1Prev == 0)
    {
      Teams[0]++;
      ChangeMade = 1;
      btn1Prev = 1;      
    }
    if(btn1 == 0 && btn1Prev == 1)
    {
      btn1Prev = 0;      
    }
    if(btn2 == 1 && btn2Prev == 0)
    {
      Teams[1]++;
      ChangeMade = 1;
      btn2Prev = 1;      
    }
    if(btn2 == 0 && btn2Prev == 1)
    {
      btn2Prev = 0;      
    }

    if(ChangeMade) {
      Update();
    }
    
}

void Update(){

  for(int i = 0; i <= 1; i++)
  {
    
    for(int T = TeamStart[i]; T < (TeamStart[i] + Teams[i]); T++) {
      leds[T] = TeamColor[i];
    }
    FastLED.show();
  }
  
}

 void Winner(CRGB winColor) {

 for(int x = 0; x <= 10; x++) {
      for (int i = 0; i <= NumLEDS - 1; i++) {
        leds[i] = winColor;    
      }
      FastLED.show();
      delay(500);
      for (int i = 0; i <= NumLEDS; i++) {
        leds[i] = CRGB (255,255,255);    
      }
      FastLED.show();
      delay(500);
  
   }
  setup();
 }

jimLee:
If you like OOP I have a mechButton class you may like. It set up its own pin, debounces, can be polled for current state true/false, can have callback if you so desire, or even used as a base class for exiting new things.

Ih the Arduino IDE's "manage libraries" menu search for LC_baseTools. You will find mechButton, and all its friends, in there.

-jim lee

I will check that out, Thanks.

Hi,
If you are using HIGH for your button activate signal, don't forget to place pull down resistors on each of the button input pins to gnd.
So the pin is at gnd when the button is not being pressed.

Tom... :slight_smile:

I would like to recommend you a simple way to use the multiple button: https://forum.arduino.cc/index.php?topic=657367.0