# How do I make this by using the most basic code possible?

**URL:** <https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696>\
**Category:** Programming\
**Created:** [November 19, 2022, 8:03pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696 "2022-11-19T20:03:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![averagestudent](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@averagestudent](https://forum.arduino.cc/u/averagestudent)\
**Post date:** [November 19, 2022, 8:03pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/1 "2022-11-19T20:03:06Z")

</div>

Hello everybody,  
I have to create this circuit by using variables and after trying so many codes, i haven't yet found a way to do it. Could somebody pls help find a way to do it?  
Sorry for the quality (it had to be a GIF so...)  
 ![3-pulsanti-4-led-con-memoria](https://europe1.discourse-cdn.com/arduino/original/4X/6/9/d/69d6b1b10d277e5593b8080a7cb86383cee8ca95.gif)

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [November 19, 2022, 8:05pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/2 "2022-11-19T20:05:37Z")

</div>

have a look at [arduino-button-tutorial](https://create.arduino.cc/projecthub/muhammad-aqib/arduino-button-tutorial-using-arduino-digitalread-function-08adb5)

---

<div class="post-metadata">

**Author:** ![averagestudent](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@averagestudent](https://forum.arduino.cc/u/averagestudent)\
**Post date:** [November 19, 2022, 8:13pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/3 "2022-11-19T20:13:08Z")

</div>

The thing is I've tried that before I even got the push button to act as a toggle button. I just can't find a way for the first button to make every LED blink one after the other. And the other 2 buttons too.

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [November 19, 2022, 8:15pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/4 "2022-11-19T20:15:27Z")

</div>

without seeing code it is difficult to advise!  
upload your code (using code tags \</\>)  
also a print of the serial monior output

---

<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:** [November 19, 2022, 8:32pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/5 "2022-11-19T20:32:34Z")

</div>

```cpp
// 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);
    }
}

```

---

<div class="post-metadata">

**Author:** ![averagestudent](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@averagestudent](https://forum.arduino.cc/u/averagestudent)\
**Post date:** [November 19, 2022, 8:34pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/6 "2022-11-19T20:34:42Z")

</div>

This is the code i used (when i press the button it turns the first led on, and when i press it again it turns it off). I just have to find a way to make it so when i press it:  
It turns on and off the leds one after the other (like the GIF above).

```cpp
//C++ code

int LEDstate = 0;
int OldButton;
int NewButton = 1;

void setup()
{
  pinMode(7,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(13,INPUT_PULLUP);
  pinMode(12,INPUT_PULLUP);
  pinMode(11,INPUT_PULLUP);
}
void loop()
{
NewButton=digitalRead(13);
  if(OldButton==0 && NewButton==1)
{
  if(LEDstate==0){
  digitalWrite(7,1);
  LEDstate=1;
}
  else
{
  digitalWrite(7,0);
  LEDstate=0;
}
}
OldButton=NewButton;
}

```

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [November 19, 2022, 9:06pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/7 "2022-11-19T21:06:55Z")

</div>

try a web search for _blink sequence of leds arduino_

---

<div class="post-metadata">

**Author:** ![averagestudent](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@averagestudent](https://forum.arduino.cc/u/averagestudent)\
**Post date:** [November 19, 2022, 9:41pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/8 "2022-11-19T21:41:27Z")

</div>

nothing, still can't find a way to make them work like the GIF above.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [November 19, 2022, 10:17pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/9 "2022-11-19T22:17:11Z")

</div>

> [@averagestudent](#):
>
> still can't find a way to make them work like the GIF above.

Start with only one pattern and then work up the code to add more. Don't try and do everything at once.  
When is this school assignment due?  
Have they taught you about arrays?

> [@averagestudent](#):
>
> and after trying so many codes,

You do not "try" code you write them. There is far too much of this attitude is that all you need to do is to look for a code that does what you want. Without bothering to learn to read what a code does.  
This is why your teacher set you these patterns.

For an idea of what you need to do, for the first pattern perform a web search for  
Larson Scanner

---

<div class="post-metadata">

**Author:** ![averagestudent](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@averagestudent](https://forum.arduino.cc/u/averagestudent)\
**Post date:** [November 19, 2022, 10:41pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/10 "2022-11-19T22:41:15Z")

</div>

> [@Grumpy\_Mike](#):
>
> Start with only one pattern and then work up the code to add more.

That's what I did, I tried to do everything I could to make it work. But it just doesn't seem to work.

> [@Grumpy\_Mike](#):
>
> You do not "try" code you write them.

Yeah, I know that, I'm sorry I didn't express myself clearly, by "trying" I meant I used everything I was thought to try and make it work but it doesn't. I can't just go on the internet and copy a complex code and call it a day, since it would've been too obvious and not very correct of my part.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [November 19, 2022, 10:46pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/11 "2022-11-19T22:46:57Z")

</div>

> [@averagestudent](#):
>
> I can't just go on the internet and copy a complex code and call it a day,

No one is suggesting that you do that. It isn't even complex code anyway. But if you look how one pattern is created you can see how to create others.

> [@averagestudent](#):
>
> That's what I did, I tried to do everything I could to make it work. But it just doesn't seem to work.

So you should post the code where you tried to get the first pattern to work and we can show you where you went wrong.  
Post the code correctly. Read this because it tells you how, along with other things you need to know before using this forum.  
[How to get the best from this from this Forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966)

---

<div class="post-metadata">

**Author:** ![bill6](https://avatars.discourse-cdn.com/v4/letter/b/8e7dd6/32.png) [@bill6](https://forum.arduino.cc/u/bill6)\
**Post date:** [November 19, 2022, 11:26pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/12 "2022-11-19T23:26:43Z")

</div>

I would use a sub void for each button and write the code for each button in its own sub void  
"void loop()" main program, create conditional call for sub loop, one for each button  
"void Subrta()" sub void a, need one for each button  
in the sub loop you can write step by step code for the LED's  
I am a beginner program writer but this seems an easy solution to your program

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [November 19, 2022, 11:44pm UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/13 "2022-11-19T23:44:04Z")

</div>

> [@bill6](#):
>
> I would use a sub void for each button and write the code for each button in its own sub void

There is nothing called a sub void, you are making it up.  
Void in front of a function name just means that function returns nothing.

I think you mean have a separate function for each pattern. However that is not the way to solve this problem.

---

<div class="post-metadata">

**Author:** ![bill6](https://avatars.discourse-cdn.com/v4/letter/b/8e7dd6/32.png) [@bill6](https://forum.arduino.cc/u/bill6)\
**Post date:** [November 20, 2022, 12:04am UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/14 "2022-11-20T00:04:18Z")

</div>

They are actually called subroutines in programming. Its a piece of the code which only runs when called for from the main program code.

---

<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:** [November 20, 2022, 12:36am UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/15 "2022-11-20T00:36:47Z")

</div>

> [@bill6](#):
>
> I am a beginner program writer but this seems an easy solution to your program

not sure it's that simple. could write explicit logic with delays for each pattern, but a better approach is non-blocking and can generically handle various patterns

> [@averagestudent](#):
>
> Could somebody pls help find a way to do it?

consider this approach

```arduino
const byte LedPins [] = { 13, 12, 11, 10 };
#define Nleds sizeof(LedPins)

byte pat0 [] = { 1, 6, 0xe, 8, 4, 2, 1, 0 };
unsigned idx;

unsigned long msecPeriod = 250;
unsigned long msecLst;
unsigned long msec;

// -----------------------------------------------------------------------------
void
setLeds (
    byte pattern )
{
    Serial.print ("setLeds: ");
    Serial.println (pattern);
    for (unsigned n = 0; n < Nleds; n++) {
        digitalWrite (LedPins [n], pattern & 1);
        pattern = pattern >> 1;

        Serial.println (pattern);
    }
}

// ---------------------------------------------------------
void
ledLoop (
    byte pattern [],
    unsigned size )
{
    if (msec - msecLst > msecPeriod) {
        msecLst = msec;

        setLeds (pattern [idx++]);
        if (size <= idx)
            idx = 0;
    }
}

// ---------------------------------------------------------
void
loop (void)
{
    msec = millis ();

    ledLoop (pat0, sizeof(pat0));
}

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

    for (unsigned n = 0; n < Nleds; n++)
        pinMode (LedPins [n], OUTPUT);
}

```

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [November 20, 2022, 2:48am UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/16 "2022-11-20T02:48:26Z")

</div>

> [@bill6](#):
>
> They are actually called subroutines in programming.

Nope. They are called functions. Unless they are part of a class, in which case they are called methods.

> [@bill6](#):
>
> Its a piece of the code which only runs when called for from the main program code.

Functions and methods can be called from many places within a program. They can be called from other functions. They can even be called from within themselves, this is called recursion.

> [@bill6](#):
>
> I am a beginner program writer but this seems an easy solution to your program

It's good that you are trying to provide advice, but it's bad that you feel the need to correct other users. Sub void is nonsense, and subroutine is an archaic term that does not apply to C++.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [November 20, 2022, 2:55am UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/17 "2022-11-20T02:55:40Z")

</div>

> [@bill6](#):
>
> They are actually called subroutines in programming.

Interesting. I googled and found this, wondering what the diff is between a function and a subroutine. You know no one says anything wrong on the internet, so I'm going with this:

> Most modern languages don't even have the concept of a subroutine anymore, at least not with the limitations that originally distinguished it from a function.

A function returns a value, a subroutine does not. Is one thing ppl say.

In C/C++ there are both functions. Not "voids" or "void subs" or subroutines.

a7

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [November 20, 2022, 2:58am UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/18 "2022-11-20T02:58:38Z")

</div>

Hi,  
Welcome to the forum.

Have you looked at the switch.. case function?

> **[switch...case - Arduino Reference](https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

Tom.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [November 20, 2022, 5:45am UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/19 "2022-11-20T05:45:40Z")

</div>

> [@averagestudent](#):
>
> Could somebody pls help find a way to do it?

Hello [averagestudent](https://forum.arduino.cc/u/averagestudent)

This task can be solved very well with OOP.  
Do you already have experience with programming in OOP?  
No classes are necessary, just a few objects for the buttons and the LED show. An event-driven timer is used to run the sequence of the LED shows.

It is best to try to abstract the task. For example, I am thinking of a ringmaster who makes penguins run left or right or jump.

INPUT=ringmaster --\> Read keys  
PROCESSING: Start of selected LED show  
OUTPUT: Run selected LED show

p.s. I like the gif presentation

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [November 20, 2022, 9:53am UTC](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696/20 "2022-11-20T09:53:01Z")

</div>

simple program I implemented some years back to test a PCB with two switches and two LEDs  
when switches pressed get different combinations of LEDs flashing

```cpp
// display LED sequence polled version
// when switches is pressed change pattern for one sequence

// note: switches are low when pressed
int switch1 = 10; // SWITCH1
int switch2 = 12; // SWITCH2

int ledPin1 = 5; // SMALL YELLOW LED
int ledPin2 = 6; // SMALL YELLOW LED
boolean switchState = false;

void setup() {
  Serial.begin(115200);
  pinMode(switch1, INPUT);
  pinMode(switch2, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

// LED test pattern sequences repeat - assume would be different in a real application
byte leds1[][2] = {{HIGH, HIGH}, {LOW, LOW}, {HIGH, HIGH}, {LOW, LOW}, {HIGH, HIGH}, {LOW, LOW}};
byte leds2[][2] = {{HIGH, LOW}, {LOW, HIGH}, {HIGH, LOW}, {LOW, HIGH}, {HIGH, LOW}, {LOW, HIGH}};
byte leds3[][2] = {{HIGH, LOW}, {LOW, LOW}, {HIGH, LOW}, {LOW,LOW}, {HIGH, LOW}, {LOW, LOW}};
byte leds4[][2] = {{LOW, HIGH}, {LOW, LOW}, {LOW, HIGH}, {LOW,LOW}, {LOW, HIGH}, {LOW, LOW}};
unsigned long int timer = 0;
void loop() {
  Serial.print("Swich state SW1 ");
  Serial.print(!digitalRead(switch1));
  Serial.print(" SW2 ");
  Serial.println(!digitalRead(switch2));
  //Serial.println(sizeof(leds1));
  // depending on switch pressed blink LED patterns
  for (int i = 0; i < sizeof(leds1) / 2; i++)
  {
    // if switch1 = 1 and switch 2=1
    if ((!digitalRead(switch1)) && (!digitalRead(switch2)))  
    {
      digitalWrite(ledPin1, leds4[i][0]);
      digitalWrite(ledPin2, leds4[i][1]);
    }
    else                         
    if (!digitalRead(switch1)) // if switch1 = 1
    {
      digitalWrite(ledPin1, leds1[i][0]);
      digitalWrite(ledPin2, leds1[i][1]);
    }
    else                         
    if (!digitalRead(switch2)) // if switch2 = 1
    {
      digitalWrite(ledPin1, leds2[i][0]);
      digitalWrite(ledPin2, leds2[i][1]);
    }
    else // if switch1 and switch2 = 0
    {
      digitalWrite(ledPin1, leds3[i][0]);
      digitalWrite(ledPin2, leds3[i][1]);
    }
    timer = millis(); // delay to blink leds
    while ((millis() - timer) < 200);
  }
}
```

[Next page](https://forum.arduino.cc/t/how-do-i-make-this-by-using-the-most-basic-code-possible/1055696.md?page=2)
