About buttons aet count question need help

Hi all,
I have 7 buttons, and a count to record the buttons' action.
if button0 pressed count = 0; button1 count = 1; .... till button 6 count=6.

if button0 pressed second time count = 7; button1 count = 8; .... till button 7 count=13.
etc.

the button pressed random, and repeated random.

attached a code for button0/1 works and printed, my question is how to do for all 7 buttons and use just one count ?

Thanks
Adam

int button0 = 10, button1 = 11, button2 = 12, button3 = 13, button4 = 14, button5 = 15, button6 = 16;
int count0 = 100, count00 = 100, count1 = 100, count01 = 100;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);
  Serial.println("setup!");

}

void loop() {
  // put your main code here, to run repeatedly:
//  Serial.print("loop!");
  delay(1000);

  // for button0:
  if (digitalRead(button0)) {
    if (count0 == 100) {
      count0 = 0;
      count00 = count0;
    } else if (count0 == count00) {
      count0 = count00 + 7;
      count00 = count0;
    }
    Serial.print("count0 =");
    Serial.println(count0);
  }

// for button1:
  if (digitalRead(button1)) {
    if (count1 == 100) {
      count1 = 1;
      count01 = count1;
    } else if (count1 == count01) {
      count1 = count01 + 7;
      count01 = count1;
    }
    Serial.print("count1 =");
    Serial.println(count1);
  }
}

output:

19:52:01.620 -> setup!count1 =1
19:52:07.625 -> count1 =8
19:52:08.605 -> count1 =15
19:52:09.622 -> count1 =22
19:52:31.618 -> count0 =0
19:52:32.602 -> count0 =7
19:52:33.619 -> count0 =14
19:52:43.632 -> count1 =29
19:52:51.604 -> count0 =21
19:52:52.623 -> count0 =28

I’d suggest you use one of the numerous button library such as Button in easyRun or OneButton or Toggle or EasyButton or Bounce2, ...

Your button should be in an array ordered by index 0 to 6 and you should keep also the number of times a given button has been pressed (either a side array or you create a dedicated class/subclass)
When button at index n is pressed you increase its press count and use both information to calculate the new value of count (n plus 7 times the number of presses of button n)

1 Like

Thank you.
good idea.

might consider this approach where a sub-function (chkButtons()) returns which button was pressed instead of having to check each button if it was pressed

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

// -----------------------------------------------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
        break;

    case 1:
        digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
        break;

    case 0:
        digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}
1 Like

Thank you gcjr.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.