# Storing buttonclicks in an Array

**URL:** <https://forum.arduino.cc/t/storing-buttonclicks-in-an-array/701456>\
**Category:** Programming\
**Created:** [March 22, 2021, 2:08pm UTC](https://forum.arduino.cc/t/storing-buttonclicks-in-an-array/701456 "2021-03-22T14:08:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![viperwave](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/viperwave/32/550972_2.png) [@viperwave](https://forum.arduino.cc/u/viperwave)\
**Post date:** [March 22, 2021, 2:08pm UTC](https://forum.arduino.cc/t/storing-buttonclicks-in-an-array/701456/1 "2021-03-22T14:08:51Z")

</div>

```cpp
A program that stores 4 button presses in an array and then plays them on the LEDs. 
Example: If the user presses the button sequence 1-2-2-1, 
then the LEDs light up in the order 1-2-2-1.

Personally I find this very difficult to do, so if anyone can give me some leads that would be great.

```

---

<div class="post-metadata">

**Author:** ![void-basil](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@void-basil](https://forum.arduino.cc/u/void-basil)\
**Post date:** [March 22, 2021, 3:13pm UTC](https://forum.arduino.cc/t/storing-buttonclicks-in-an-array/701456/2 "2021-03-22T15:13:53Z")

</div>

Off the top of my head, and I don't have time to do any coding, this would as a start keep track of the four pressed buttons

- Have a look at the state change detect example in the ide to see how to detect a button has become pressed
- Put all the button pins in an array
- Put all the led pins in an array
- Have another array to hold the array index of each button that's newly pressed
- Have a counter set to 0 initially, to count how many presses there have been
- Continually monitor all the buttons (for loop across the button array)
- When you detect a button is newly pressed store its array index in the presses array, using the counter value as the index into that array
- Increment the counter, and when it's 3 (that would be 4 presses since arrays start at 0) you're done with that part
- Then use the values in the presses array to be the indices into the led array to access the led pins and turn them on
- You need some way to start over.... perhaps when you get a 5th button press, that counts as a new 1st press, and turn the leds all off, zero the counter and go again

Very off the top of my head, that.

---

<div class="post-metadata">

**Author:** ![dougp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dougp/32/238908_2.png) [@dougp](https://forum.arduino.cc/u/dougp)\
**Post date:** [March 22, 2021, 5:03pm UTC](https://forum.arduino.cc/t/storing-buttonclicks-in-an-array/701456/3 "2021-03-22T17:03:10Z")

</div>

What @void-basil said plus, familiarize yourself with [arrays](https://www.arduino.cc/reference/en/).

For state change , debouncing, etc. get to know the first five demos in IDE -\> file/examples/digital.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [March 22, 2021, 5:59pm UTC](https://forum.arduino.cc/t/storing-buttonclicks-in-an-array/701456/4 "2021-03-22T17:59:32Z")

</div>

consider

```cpp
byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };

#define N_BUT   sizeof(pinsBut)

enum { Off = HIGH, On = LOW };

#define LIST_SIZE  4
byte butList [LIST_SIZE] = {};
unsigned  butIdx = 0;

char s [80];

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (115200);
    for (unsigned n = 0; n < N_BUT; n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode (pinsLed [n], OUTPUT);
        pinMode (pinsBut [n], INPUT_PULLUP);
    }
}

// -----------------------------------------------------------------------------
void
chkButtons (void)
{
    static byte butSt [N_BUT] = {};

    for (unsigned n = 0; n < N_BUT; n++)  {
        byte but = digitalRead (pinsBut [n]);
        if (butSt [n] != but)  {
            butSt [n] = but;   
            if (On == but)  {
                butList [butIdx++] = n;
#if 0
                sprintf (s, "%s: %d %d", __func__ , butIdx, butList [butIdx-1]);
                Serial.println (s);
#endif
            }
        }
        delay (20);
    }
}

// -------------------------------------
void loop ()
{
    if (LIST_SIZE > butIdx)
        chkButtons ();

    else  {
        for (unsigned i = 0; i < butIdx; i++)  {
#if 0
            sprintf (s, "%s: i %d, list %d pin %d",
                 __func__ , i, butList [i], pinsLed [butList [i]]);
            Serial.println (s);
#endif

            digitalWrite (pinsLed [butList [i]], On);
            delay (500);
            digitalWrite (pinsLed [butList [i]], Off);
            delay (500);
        }

        butIdx = 0;
    }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 20, 2021, 6:00pm UTC](https://forum.arduino.cc/t/storing-buttonclicks-in-an-array/701456/5 "2021-07-20T18:00:09Z")

</div>

Closed on July 20, 2021, 6:00pm UTC

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