# Help my arduino code

**URL:** <https://forum.arduino.cc/t/help-my-arduino-code/1107422>\
**Category:** Programming\
**Created:** [March 27, 2023, 12:26pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422 "2023-03-27T12:26:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![adspig](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/adspig/32/933822_2.png) [@adspig](https://forum.arduino.cc/u/adspig)\
**Post date:** [March 27, 2023, 12:26pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/1 "2023-03-27T12:26:35Z")

</div>

It's my problem  
• Create a device that controls LED and buzzer using 3 buttons.  
• Buttons are located at the top, middle, and bottom.  
• 1 button: When the button is pressed, the 3 red LEDs turn on for 0.3 seconds in sequence and then turn off.  
• 2 button: When the button is pressed, the buzzer sounds for 0.3 seconds in 3 steps in sequence.  
• 3 button: When the button is pressed, the 3 blue LEDs turn on for 0.3 seconds in sequence and then turn off.  
• The above function is repeated until the button is pressed again.

Constraints  
All functions are performed with the operation cycle synchronized.

```cpp
#define LED_PIN_1 11
#define LED_PIN_2 10
#define LED_PIN_3 9
#define LED_PIN_4 6
#define LED_PIN_5 5
#define LED_PIN_6 4

#define PUSH_PIN_1 12
#define PUSH_PIN_2 7
#define PUSH_PIN_3 3

#define BUZ_PIN 13

int buz[] = {BUZ_PIN};
int led[] = {LED_PIN_1, LED_PIN_2, LED_PIN_3};
int led2[] = {LED_PIN_4, LED_PIN_5, LED_PIN_6};
int push[] = {PUSH_PIN_1, PUSH_PIN_2, PUSH_PIN_3};

bool state[3] = {false, false, false};
bool lastState[3] = {true, true, true};
bool buttonReleased[3] = {true, true, true}; 

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

  for (int i = 0; i < 3; i++) {
    pinMode(led[i], OUTPUT);
    pinMode(push[i], INPUT_PULLUP);
  }

  for (int w = 0; w < 1; w++) {
    pinMode(buz[w], OUTPUT);
  }

  for (int t = 0; t < 3; t++) {
    pinMode(led2[t], OUTPUT);
  }
}

void loop() {
  
  for (int i = 0; i < 3; i++) {
    if (digitalRead(push[i]) == LOW && buttonReleased[i]) { 
      state[i] = !state[i];
      buttonReleased[i] = false;
    }
    else if (digitalRead(push[i]) == HIGH) {
      buttonReleased[i] = true; 
    }
  }

  if (state[0] != lastState[0]) {
    lastState[0] = state[0];
    if (state[0] == true) {
      while (state[0] == true) {
        for (int i = 0; i < 3; i++) {
          digitalWrite(led[i], HIGH);
          delay(300);
          digitalWrite(led[i], LOW);
        }
        delay(300);
      }
      for (int i = 0; i < 3; i++) {
        digitalWrite(led[i], LOW);
      }
    }
  }

  if (state[1] != lastState[1]) {
    lastState[1] = state[1];
    if (state[1] == true) {
      while (state[1] == true) {
        for (int i = 0; i < 3; i++) {
          tone(BUZ_PIN, 200*(i+1), 300);
          delay(300);
        }
        noTone(BUZ_PIN);
        delay(300);
      }
    }
  }

  if (state[2] != lastState[2]) {
    lastState[2] = state[2];
    if (state[2] == true) {
      while (state[2] == true) {
        for (int i = 0; i < 3; i++) {
          digitalWrite(led2[i], HIGH);
          delay(300);
          digitalWrite(led2[i], LOW);
        }
        delay(300);
      }
      for (int i = 0; i < 3; i++) {
        digitalWrite(led2[i], LOW);
      }
    }
  }
}

```

If you press the first button randomly among the buttons once, the red LED lights up in sequence. But even if I press the first button again, the led doesn't turn off.

And when you press the first button, the function of the first button is repeated, right?

After that, when I press the second button, the buzzer should repeat, but it doesn't work. The problem is the same with the third button.

When I saw it, bool seems to be the problem, but I lack the ability to solve it. What should I do?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [March 27, 2023, 12:35pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/2 "2023-03-27T12:35:19Z")

</div>

```arduino
  if (state[0] != lastState[0]) {
...
  if (state[1] != lastState[1]) {
...
  if (state[2] != lastState[2]) {

```

[/quote]

The whole point of using an array for states, is to factor your code by iterating through them. Instead, you treated the elements as individual variables. That is a terrible waste of code.

> • Create a device that controls LED and buzzer using 3 buttons.

This sounds like "assignment language". Please correct me if I'm wrong.

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [March 27, 2023, 12:36pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/3 "2023-03-27T12:36:54Z")

</div>

It looks like you are learning about something similar to finite state machines.  
The first starting part is to draw a diagram showing the states, the transitions between the states, their triggers, and any action on those transitions.  
It could look a bit like this:

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/7/0/e/70e61924069340207386a14c4fed63d0d5f2b688.png)

---

<div class="post-metadata">

**Author:** ![adspig](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/adspig/32/933822_2.png) [@adspig](https://forum.arduino.cc/u/adspig)\
**Post date:** [March 27, 2023, 12:40pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/4 "2023-03-27T12:40:18Z")

</div>

I'm sorry, I can't speak English well, so I think it was difficult

LED and buzzer control using 3 buttons.

I'm inexperienced so I don't know what to do

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [March 27, 2023, 12:42pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/5 "2023-03-27T12:42:41Z")

</div>

First, is this a school assignment?

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [March 27, 2023, 12:44pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/6 "2023-03-27T12:44:30Z")

</div>

> [@anon57585045](#):
>
> is this a school assignment?

I'm sure you are right but this presents the opportunity to say now that the OP will not be helped much if a ready made solution, consisting of a large lump of C code, comes crashing into the thread.

---

<div class="post-metadata">

**Author:** ![adspig](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/adspig/32/933822_2.png) [@adspig](https://forum.arduino.cc/u/adspig)\
**Post date:** [March 27, 2023, 12:44pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/7 "2023-03-27T12:44:32Z")

</div>

Yes, but I couldn't solve it during class, so I made it at home by myself and couldn't solve it.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [March 27, 2023, 12:49pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/8 "2023-03-27T12:49:08Z")

</div>

Did you understand my post? Have you investigated it yet?

Do you have hardware, or is this performed in simulation?

Did you write this code from scratch, or did you edit "boilerplate" code that you were given with the assignment?

What does this mean, exactly?

> Constraints  
> All functions are performed with the operation cycle synchronized.

Were there no previous lessons, that introduced the foundation knowledge that allow you to complete this one?

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [March 27, 2023, 12:54pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/9 "2023-03-27T12:54:58Z")

</div>

> [@6v6gt](#):
>
> will not be helped much if a ready made solution, consisting of a large lump of C code

I think not a worry in this case, the plagiarism would be too obvious.

---

<div class="post-metadata">

**Author:** ![adspig](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/adspig/32/933822_2.png) [@adspig](https://forum.arduino.cc/u/adspig)\
**Post date:** [March 27, 2023, 1:07pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/10 "2023-03-27T13:07:45Z")

</div>

After writing the code with the IDE program, connect the board and operate it.

The class itself is a student class. But after going to the army, I'm going back to school and learning the basics again.

There was no example for the assignment, I just told you to do it like that.

Constraints  
All functions are performed with the operation cycle synchronized.

The meaning of is hard to express due to lack of English. The first button is a red led and we assume that it is repeating.  
It is getting brighter in order of red (led1, led2, led3)  
The moment the blue LED is turned on sequentially by pressing the third button, when the red led2 is turned on, the blue led2 is turned on together.

If you still do not understand the part of my English  
Feel free to ignore it.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [March 27, 2023, 1:08pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/11 "2023-03-27T13:08:53Z")

</div>

> [@adspig](#):
>
> If you still do not understand the part of my English  
> Feel free to ignore it.

I would rather reach an understanding of it. I also speak a second language, not very well.

---

<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:** [March 27, 2023, 1:09pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/12 "2023-03-27T13:09:06Z")

</div>

> [@adspig](#):
>
> • 2 button: When the button is pressed, the buzzer sounds for 0.3 seconds in 3 steps in sequence.

Post a sequence timming diagram to clarify how buzzer sounds will happens.

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [March 27, 2023, 1:16pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/13 "2023-03-27T13:16:18Z")

</div>

> [@adspig](#):
>
> Constraints  
> All functions are performed with the operation cycle synchronized.

I'd hazard a guess that it is to rule out solutions using timer callbacks or similar running asynchronously with the main loop().

---

<div class="post-metadata">

**Author:** ![adspig](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/adspig/32/933822_2.png) [@adspig](https://forum.arduino.cc/u/adspig)\
**Post date:** [March 27, 2023, 1:16pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/14 "2023-03-27T13:16:34Z")

</div>

Thank you for your continued interest and reply.

I expressed it with a picture, do you understand?

 ![제목 없음](https://europe1.discourse-cdn.com/arduino/original/4X/4/c/3/4c309c7d3099cfaf660538b69b2ef0ade1cfa9ae.png)

---

<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:** [March 27, 2023, 1:18pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/15 "2023-03-27T13:18:20Z")

</div>

This picture fits not to your start post.

```cpp
adspig
1hpost #1

It's my problem
• Create a device that controls LED and buzzer using 3 buttons.
• Buttons are located at the top, middle, and bottom.
• 1 button: When the button is pressed, the 3 red LEDs turn on for 0.3 seconds in sequence and then turn off.
• 2 button: When the button is pressed, the buzzer sounds for 0.3 seconds in 3 steps in sequence.
• 3 button: When the button is pressed, the 3 blue LEDs turn on for 0.3 seconds in sequence and then turn off.

```

---

<div class="post-metadata">

**Author:** ![adspig](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/adspig/32/933822_2.png) [@adspig](https://forum.arduino.cc/u/adspig)\
**Post date:** [March 27, 2023, 1:25pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/16 "2023-03-27T13:25:28Z")

</div>

I think it's because I can't communicate well because of my stupid English skills.

It is drawn except for the situation of the second button.

Doesn't the three-step scale beep1-beep2-beep3? If you press the second button, you can see the sound of the buzzer and the LED next to it.  
Beep1(led1on)-beep2(led2on)-beep3(led3on)  
You want it to repeat like this.

I can only express it in writing.

---

<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:** [March 27, 2023, 1:26pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/17 "2023-03-27T13:26:34Z")

</div>

> [@adspig](#):
>
> I think it's because I can't communicate well because of my stupid English skills.

> **[DeepL Translate: The world's most accurate translator](https://www.deepl.com/translator)**
>
> Translate texts & full document files instantly. Accurate translations for individuals and Teams. Millions translate with DeepL every day.

---

<div class="post-metadata">

**Author:** ![adspig](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/adspig/32/933822_2.png) [@adspig](https://forum.arduino.cc/u/adspig)\
**Post date:** [March 27, 2023, 1:31pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/18 "2023-03-27T13:31:46Z")

</div>

My goal is

1. when pushbutton 1 is pressed once, the red LED lights up sequentially for 0.3 seconds.

2. when pushbutton 2 is pressed once, the buzzer will sound 3 pitches sequentially for 0.3 seconds (the behavior of pushbutton 1 is still repeating, so repeat it together).

3. press pushbutton 3 once and the blue LED will light up sequentially for 0.3 seconds (since 1 and 2 are repeating, repeat 3 as well).

4. press pushbutton 3 to stop the blue LEDs from lighting up sequentially for 0.3 seconds.

5. press pushbutton 2 once to stop the buzzer from repeating the 3 tones sequentially for 0.3 seconds.

6. Press pushbutton 1 once to stop the red LED from lighting up sequentially for 0.3 seconds.

The picture I drew shows the synchronized sequence of the red LED, buzzer sound, and blue LED that repeats when each button is pressed.

Translated with [DeepL Translate: The world's most accurate translator](http://www.DeepL.com/Translator) (free version)

---

<div class="post-metadata">

**Author:** ![ec2021](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ec2021/32/997272_2.png) [@ec2021](https://forum.arduino.cc/u/ec2021)\
**Post date:** [March 27, 2023, 1:44pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/19 "2023-03-27T13:44:22Z")

</div>

Hi @adspig,

so the task is as follows:

Input/Output Devices:

- 3 red LED
- One buzzer
- 3 blue LED
- 3 pushbuttons

Relationships:

- pushbutton1 controls the 3 red LEDs
- pushbutton2 controls the buzzer
- pushbutton3 controls the 3 blue LEDs

Wanted behaviour:

- if (pushbutton1 is pressed)

- if (pushbutton2 is pressed)

- if (pushbutton3 is pressed)

Is that correct?

If yes:

Is the behaviour of each pushbutton

- **independent** from the others

or shall they only work in the sequence

- pushbutton1 ON -\> pushbutton2 ON -\> pushbutton3 ON -\> pushbutton3 OFF -\> pushbutton2 OFF -\> pushbutton1 OFF

ec2021

---

<div class="post-metadata">

**Author:** ![adspig](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/adspig/32/933822_2.png) [@adspig](https://forum.arduino.cc/u/adspig)\
**Post date:** [March 27, 2023, 1:48pm UTC](https://forum.arduino.cc/t/help-my-arduino-code/1107422/20 "2023-03-27T13:48:43Z")

</div>

That's right, and the  
pushbutton1 ON -\> pushbutton2 ON -\> pushbutton3 ON -\> pushbutton3 OFF -\> pushbutton2 OFF -\> pushbutton1 OFF  
is also correct.

Press the three buttons once to make the red, buzzer, and blue LEDs repeat, and when the first red LED is glowing, the first blue LED should also be glowing, as well as the tone of the buzzer. In summary, the three are working in synchronization.

[Next page](https://forum.arduino.cc/t/help-my-arduino-code/1107422.md?page=2)
