How to write an Array for Buttons & LEDS

Hey! Very new programmer here, would really appreciate any help.

I'm trying to make an array that turns on the LED pin values that I put inside of it.

Imagine there are buttons for the LED's, and you need to click them in the order of the LED's lighting up. How would you code that? and a step further, how would you confirm that the buttons clicked are in the same order as the LED's lighting up?

Please and thanks

It sounds like you have not finished learning how to program yet.
Without looking, I bet one of the many button libraries will have a sample of an array of buttons. It is a trivial matter to extend that array to also include the LED pins.
Start by reading the pinned post re 'how to get the most from this forum'. When you have code that is mostly working, post it in code tags, any error log also in code tags, a picture of a hand drawn wiring diagram plus a brief statement of what you expect to happen and what is actually happening. That is a start.

Say more. Do the LEDs turn on in some random order that the player must match by pressing a corresponding button?

Do the LEDs stay on until the end of the round, or go off after some short time, then repeat after waiting a bit for the player?

What are the consequences of pressing the wrong next button? What happens if we make it through all N LEDs?

Arrays can hold pin numbers, pin numbers can be outputs or inputs. A simple solution to that part of your effort would be to have two arrays, one for the LEDs and one for the buttons, with the LED and button at the same index in each array woukd be the correspondence.

If you need an array of values scrambled for presentation in a random order, you can use a shuffle algorithm, add Arduino to any search to focus your results, viz:

arduino shuffle algorithm

a7

1 Like

I would start by learning to read a button...
https://docs.arduino.cc/built-in-examples/digital/Button/

Then learn how to turn an LED on/off...
https://docs.arduino.cc/built-in-examples/basics/Blink/

digitalRead()

1 Like

look this over
The C Programming Language can provide explanations, see array, but you can always ask questions

// 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

ok, thanks, ill do that!

no they won't turn on in a random order, I want to make an order that stays the same whenever the code runs again
The LED's wont repeat if you don't press anything and if you don't press anything in maybe like 30 seconds then I want the whole thing to restart
If you press the pattern the same as how it lit up then you move on to like a "next stage", I want it to be a memory game type thing with multiple stages
also ok, thank u for your guidance!! ill look into random orders too

thank you!! i will look this over :smiley:

This sounds like a game called Simon, there are quite a few Arduino versions to be found.

Here's one:

You might like the wokwi simulator for your own development efforts.

Simon can repeat LEDs and obvsly has to when the sequence is longer than N number of real LED/button combos.

The sketch in the link just adds a new random number to a stored sequence when the sequence length grows, that is to say after the player gets the current sequence correct and before the toy plays the sequence with an additional element.

The real Simon did not, I think, have a reward tone every time you got a sequence right. I don't like it, it messes up my memory of the so-far sequence.

A few other things I don't like, if I was taking more time I'd challenge myself to understand the code well enough to fix it to work how I wanted.

google for others. None of the code I saw was very complicated, at keast in terms of the language features used. A few were junk code or poorly presented; read around and dive in when something catches you fancy.

If it isn't quite Simon you are aiming at, please describe in some detail exact how you want this to work.

a7

1 Like

yes this is what I'm trying to make!! except my circuit looks a lot messier :moyai: Thank you :pray:, this is a great site

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