Assistance w/ multiple "if" scenarios for button usage

Thank you very much for your time.

I’m attempting to make a scoreboard for fun, but I don’t have a lot of programming experience. I built a small version initially w/ a Max7219 ic and those little seven segment displays and was able to make any changes w/ multiple if(button pushed) statements in the void loop. Had no problems.

But then I tried to make it out of WS2811 leds in the classic shape of a scoreboard and just sorta modified the code to use an array instead of the max7219’s functionality. I’ve had limited success at this point.

The leds come on in the proper shape, and I can use a for() loop to count up to 99 and it will reset on its own, but whenever I try to use the buttons, I can only get to the point where a single button works at a time. So all I can do at this point is go from 0 to 1 to 0 to 1. Pressing the same button multiple times does nothing.

Again, I have very little programming experience, so I’m sure its something dumb that I’m missing. But thank you very much, again, for your time.

Follow-up: I thought previously that if I used two if statements instead of the if and if else in the void loop(), that it didn’t work at all. Just displayed 0-0. But it turns out it works perfectly that way, as long as the other button is also depressed. Maybe its just a wiring issue?

But I’m sure you smart guys can point out something incorrect that I did in the code.

#include "FastLED.h"

#define DATA_PIN    6
#define DATA_PIN2   8
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    15
#define BRIGHTNESS  40
#define FRAMES_PER_SECOND 120

#define switchone 3
#define switchtwo 2

CRGB leds[2][NUM_LEDS];
//-----------------------------------------------------------------------------------
int home[][13] = {
  {0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 14, 14},
  {0, 5, 6, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12},
  {2, 1, 0, 5, 6, 7, 8, 9, 14, 13, 12, 14, 14},
  {2, 1, 0, 5, 6, 7, 11, 8, 12, 13, 14, 14, 14},
  {2, 0, 3, 8, 6, 7, 11, 5, 12, 12, 12, 12, 12},
  {0, 1, 2, 3, 8, 7, 6, 11, 12, 13, 14, 14, 14},
  {0, 1, 2, 3, 8, 9, 14, 13, 12, 11, 6, 7, 14},
  {2, 1, 0, 5, 7, 10, 13, 13, 13, 13, 13, 13, 13},
  {1, 13, 2, 12, 0, 14, 5, 9, 11, 3, 6, 8, 7},
  {0, 1, 5, 2, 3, 6, 8, 7, 11, 12, 12, 12, 12},
};
//-----------------------------------------------------------------------------------
uint8_t homescore = 0;  // keeps track of home team score to display
//uint8_t awayscore = 0;  // keeps track of away team score to display
int rightdigit;
int leftdigit;

//---------------------------------------------------------------
void setup() {
  pinMode(switchone, INPUT_PULLUP);
  pinMode(switchtwo, INPUT_PULLUP);

  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds[0], NUM_LEDS);
  FastLED.addLeds<LED_TYPE, DATA_PIN2, COLOR_ORDER>(leds[1], NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();  
  
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[0][i] = CRGB::Black;
    leds[1][i] = CRGB::Black;
  }
  ledcalculate();
}

//---------------------------------------------------------------
void loop() {
 
  FastLED.show();
  if (!digitalRead(switchtwo)) {
    homescore++;
    while (!digitalRead(switchtwo)) {
      delay(5); //delay to debounce switch
      ledcalculate();
    }
    ledcalculate();
  }

  else if (!digitalRead(switchone)) {
    homescore--;
    while (!digitalRead(switchone)) {
      delay(5); //delay to debounce switch
    }
    ledcalculate();
  }
}

//-----------------------------------------------------------------ledcalculate();

void ledcalculate() {
  //int val = analogRead(A2);
  //int c = map(val, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put C in first value of CHSV

  rightdigit = homescore % 10;
  leftdigit = homescore % 100 / 10;

  for (int i = 0; i < NUM_LEDS; i++) {
    leds[0][i] = CRGB::Black;
    leds[1][i] = CRGB::Black;
  }

  for (int x = 0; x < 13; x++) {
    leds[0][home[rightdigit][x]] = CHSV(15, 255, 255);
    leds[1][home[leftdigit][x]] = CHSV(15, 255, 255);
  }

}
  else if (!digitalRead(switchone))

Why is this test negated ?
It seems to be testing whether the switch is not activated

Thank you very much, UKHeliBob. That actually solved all of my problems. When I was reading about pullup switch operations the example used had a "!" and that was how I got it to work w/ the max7219 project, so I guess I just kind of assumed that that was how they worked.

When it didn't work, I tried to remove the negation, but I think I forgot to do it on the while() loop, so it just resulted in craziness. So oversight on my part. Lots to learn.

Still have a lot to learn. Thank you very much for resolving my first post on the first reply... You're truly the man.

Using a while loop and a delay() could be a problem in some programs because it stops loop() running freely. If you detect when a button becomes pressed rather than when it is pressed then the loop() function can run freely and maybe do other things and you could also detect short and long button presses, for example

Take a look at the StateChangeDetection example in the IDE to see how to implement it

I certainly will check that out and do my best to implement that detection change. That does seem like the smarter way to go about it.

While I've got your attention, though. I was curious if you could answer one more question. Is it possible to use the same ledcalculate(); function for the away side? It will just be two more 15 LED length strips done up in the same style.

I remember from my school days that you can send and return values from functions if they aren't "void," but I can't recall the methodology. Right now I adjust the left and right digits by the "homescore" variable, so I'm thinking if that was another variable that was associated with one of the button pushes, that it should be possible.

I don't need you to do the work or anything, I'm just curious if it is a path worth pursuing? My current idea was to write a second ledCalculateHome and ledCalculateAway functions.

An example of a function that takes 2 parameters and returns a third

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int x = 1; x < 4; x++)
  {
    for (int y = 5; y < 10; y++)
    {
      int result = multiply(x, y);
      Serial.print(result);
      Serial.print("\t");
    }
    Serial.println();
  }
}

void loop()
{
}

int multiply(int firstNumber, int secondNumber) //a function to multiply 2 numbers and return the result
{
  int answer = firstNumber * secondNumber;
  return answer;
}

You are incredibly awesome, sir. Thank you so much for all of your help. I know I’ve got a lot of studying and whatnot to build an understanding of these processes, but its so useful to have a “teacher” that you can just kinda ask dumb questions and they know what you’re trying to express.

Let’s see how far I can push this. In the ledcalculate() function there’s a variable ‘c’ used to read a pot and use that value as the color. Figured it would be good for picking team colors. But as it is now, it only reads the color value when you push a button to change values.

When I was just counting 0-99 with a for loop I was able to rotate the pot and see the change in real time, for obvious reasons. But is there a way w/ the current setup that the pot can change the value in real time? So that when you’re starting the game and the scores are 0 v 0, each team can pick their color and see it changing through the numbers?

Again, thank you. Totally understandable if you don’t feel like going this far with my problems.

In the ledcalculate() function there’s a variable ‘c’ used to read a pot and use that value as the color

This is the ledcalculate() function from your original post

void ledcalculate()
{
  //int val = analogRead(A2);
  //int c = map(val, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put C in first value of CHSV
  rightdigit = homescore % 10;
  leftdigit = homescore % 100 / 10;
  for (int i = 0; i < NUM_LEDS; i++)
  {
    leds[0][i] = CRGB::Black;
    leds[1][i] = CRGB::Black;
  }
  for (int x = 0; x < 13; x++)
  {
    leds[0][home[rightdigit][x]] = CHSV(15, 255, 255);
    leds[1][home[leftdigit][x]] = CHSV(15, 255, 255);
  }
}

It does not seem to match your description above

My apologies, it was the //'d out portion of what I posted. In the code it currently looks like what is below. I’ve tried moving it around. And making ‘c’ a global variable. I should probably name it something more reasonable. But my goal would be to have a real-time result from turning the potentiometer. This is how it should have looked in my original post. I had just replaced the ‘c’ with a static number while I was testing. Making certain there was no extra stuff that was clouding my goal.

void ledcalculate() {
  int val = analogRead(A2);
  int c = map(val, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put C in first value of CHSV

  rightdigit = homescore % 10;
  leftdigit = homescore % 100 / 10;

  for (int i = 0; i < NUM_LEDS; i++) {
    leds[0][i] = CRGB::Black;
    leds[1][i] = CRGB::Black;
  }

  for (int x = 0; x < 13; x++) {
    leds[0][home[rightdigit][x]] = CHSV(c, 255, 255);
    leds[1][home[leftdigit][x]] = CHSV(c, 255, 255);
    delay(25);
    FastLED.show();
  }
}