Help my arduino code

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.

#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?

  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.

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:

1 Like

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

First, 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.

1 Like

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

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?

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

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.

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

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

1 Like

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

Thank you for your continued interest and reply.

I expressed it with a picture, do you understand?

This picture fits not to your start post.

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.

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.

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 (free version)

2 Likes

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 (red LEDs are not blinking)
      • start blink sequence
    • if (red LEDs are blinking )
      • stop blink sequence
  • if (pushbutton2 is pressed)

    • if (buzzer sequence is OFF)
      • start buzzer sequence
    • if (buzzer sequence is ON)
      • stop the buzzer sequence
  • if (pushbutton3 is pressed)

    • if (blue LEDs are not blinking)
      • start blink sequence
    • if (blue LEDs are blinking )
      • stop blink sequence

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

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.