2

I have a problem with getting my buzzer to only do the tones I programmed in once. I know I have to add some sort of code but I'm relatively new to this so I don't really know. I want it to start playing the tones I have in the code once I press the button. After that it should immediately stop, without looping everything until I press the button again. Basically I have these LEDs that light up in a row with every time I press my button and once the last LED lights up, the buzzer should play its melody. After that, I can press my button again and the whole thing starts from the beginning. If anyone could help, I'd be more than grateful!

This is my code:

#define button 3

#define LED 1
#define twoLED 2
#define threeLED 4
#define fourLED 5
#define fiveLED 6
#define sixLED 7
#define sevenLED 8
#define eightLED 9
#define nineLED 10
#define tenLED 11




int state = 0;
int old = 0;
int buttonPoll = 0;

void setup() {
  pinMode (3,INPUT);
  pinMode (11,OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(twoLED, OUTPUT);
  pinMode(threeLED, OUTPUT);
  pinMode(fourLED, OUTPUT);
  pinMode(fiveLED, OUTPUT);
  pinMode(sixLED, OUTPUT);
  pinMode(sevenLED, OUTPUT);
  pinMode(eightLED, OUTPUT);
  pinMode(nineLED, OUTPUT);
  pinMode(tenLED, OUTPUT);
  

  digitalWrite(LED, LOW);
  digitalWrite(twoLED,LOW);
  digitalWrite(threeLED,LOW);
  digitalWrite(fourLED, LOW);
  digitalWrite(fiveLED, LOW);
  digitalWrite(sixLED, LOW);
  digitalWrite(sevenLED, LOW);
  digitalWrite(eightLED, LOW);
  digitalWrite(nineLED, LOW);
  digitalWrite(tenLED, LOW);
  
}

void loop() {


  buttonPoll = digitalRead (3);
  if (buttonPoll ==1) {
    delay (50);
    buttonPoll = digitalRead (3);
    if (buttonPoll == 0) {
      state = old + 1;
    }
  }
  else {
    delay(100);
    
  }

  switch (state) {
    case 1:
  digitalWrite(LED, HIGH);
  digitalWrite(twoLED,LOW);
  digitalWrite(threeLED,LOW);
  digitalWrite(fourLED, LOW);
  digitalWrite(fiveLED, LOW);
  digitalWrite(sixLED, LOW);
  digitalWrite(sevenLED, LOW);
  digitalWrite(eightLED, LOW);
  digitalWrite(nineLED, LOW);
  digitalWrite(tenLED, LOW);
    old = state;
    break;
    
    case 2:
  digitalWrite(LED, HIGH);
  digitalWrite(twoLED,HIGH);
  digitalWrite(threeLED,LOW);
  digitalWrite(fourLED, LOW);
  digitalWrite(fiveLED, LOW);
  digitalWrite(sixLED, LOW);
  digitalWrite(sevenLED, LOW);
  digitalWrite(eightLED, LOW);
  digitalWrite(nineLED, LOW);
  digitalWrite(tenLED, LOW);
    old = state;
    break;
    
    case 3:
  digitalWrite(LED, HIGH);
  digitalWrite(twoLED,HIGH);
  digitalWrite(threeLED,HIGH);
  digitalWrite(fourLED, LOW);
  digitalWrite(fiveLED, LOW);
  digitalWrite(sixLED, LOW);
  digitalWrite(sevenLED, LOW);
  digitalWrite(eightLED, LOW);
  digitalWrite(nineLED, LOW);
  digitalWrite(tenLED, LOW);
    old = state;
    break;

    case 4:
  digitalWrite(LED, HIGH);
  digitalWrite(twoLED,HIGH);
  digitalWrite(threeLED,HIGH);
  digitalWrite(fourLED, HIGH);
  digitalWrite(fiveLED, LOW);
  digitalWrite(sixLED, LOW);
  digitalWrite(sevenLED, LOW);
  digitalWrite(eightLED, LOW);
  digitalWrite(nineLED, LOW);
  digitalWrite(tenLED, LOW);
   old = state;
    break;

  case 5:
    digitalWrite(LED, HIGH);
  digitalWrite(twoLED,HIGH);
  digitalWrite(threeLED,HIGH);
  digitalWrite(fourLED, HIGH);
  digitalWrite(fiveLED, HIGH);
  digitalWrite(sixLED, LOW);
  digitalWrite(sevenLED, LOW);
  digitalWrite(eightLED, LOW);
  digitalWrite(nineLED, LOW);
  digitalWrite(tenLED, LOW);
   old = state;
    break;

  case 6:
    digitalWrite(LED, HIGH);
  digitalWrite(twoLED,HIGH);
  digitalWrite(threeLED,HIGH);
  digitalWrite(fourLED, HIGH);
  digitalWrite(fiveLED, HIGH);
  digitalWrite(sixLED, HIGH);
  digitalWrite(sevenLED, LOW);
  digitalWrite(eightLED, LOW);
  digitalWrite(nineLED, LOW);
  digitalWrite(tenLED, LOW);
   old = state;
    break;

  case 7:
    digitalWrite(LED, HIGH);
  digitalWrite(twoLED,HIGH);
  digitalWrite(threeLED,HIGH);
  digitalWrite(fourLED, HIGH);
  digitalWrite(fiveLED, HIGH);
  digitalWrite(sixLED, HIGH);
  digitalWrite(sevenLED, HIGH);
  digitalWrite(eightLED, LOW);
  digitalWrite(nineLED, LOW);
  digitalWrite(tenLED, LOW);
   old = state;
    break;

  case 8:
    digitalWrite(LED, HIGH);
  digitalWrite(twoLED,HIGH);
  digitalWrite(threeLED,HIGH);
  digitalWrite(fourLED, HIGH);
  digitalWrite(fiveLED, HIGH);
  digitalWrite(sixLED, HIGH);
  digitalWrite(sevenLED, HIGH);
  digitalWrite(eightLED, HIGH);
  digitalWrite(nineLED, LOW);
  digitalWrite(tenLED, LOW);
   old = state;
    break;

  case 9:
    digitalWrite(LED, HIGH);
  digitalWrite(twoLED,HIGH);
  digitalWrite(threeLED,HIGH);
  digitalWrite(fourLED, HIGH);
  digitalWrite(fiveLED, HIGH);
  digitalWrite(sixLED, HIGH);
  digitalWrite(sevenLED, HIGH);
  digitalWrite(eightLED, HIGH);
  digitalWrite(nineLED, HIGH);
  digitalWrite(tenLED, LOW);
   old = state;
    break;

  case 10:
    digitalWrite(LED, HIGH);
  digitalWrite(twoLED,HIGH);
  digitalWrite(threeLED,HIGH);
  digitalWrite(fourLED, HIGH);
  digitalWrite(fiveLED, HIGH);
  digitalWrite(sixLED, HIGH);
  digitalWrite(sevenLED, HIGH);
  digitalWrite(eightLED, HIGH);
  digitalWrite(nineLED, HIGH);
  digitalWrite(tenLED, HIGH);
   old = state;


       tone (11,261);
      delay(250);
      
      tone(11, 330);
      delay(250);
      
      tone(11,391.99);
      delay(250);

      tone(11,440);
      delay(250);

      tone(11, 391.995);
      delay(250);

      tone(11, 330);
      delay(250);

        }
      
    break;

   default:
  digitalWrite(LED, LOW);
  digitalWrite(twoLED,LOW);
  digitalWrite(threeLED,LOW);
  digitalWrite(fourLED, LOW);
  digitalWrite(fiveLED, LOW);
  digitalWrite(sixLED, LOW);
  digitalWrite(sevenLED, LOW);
  digitalWrite(eightLED, LOW);
  digitalWrite(nineLED, LOW);
  digitalWrite(tenLED, LOW);
  
    old = 0;

    break;


  }
}
0

Enshrine the first section of the loop() code inside a while loop.

while (condition)
{
    // this code executes continually until 'condition' is true
}

In your case, the variable state changes when an active button press is recorded: use that as your condition.

while (state == old)
{
  buttonPoll = digitalRead (3);
  if (buttonPoll ==1)
  {
    delay (50);
    buttonPoll = digitalRead (3);
    if (buttonPoll == 0)
    {
      state = old + 1;
    }
  }
  else
  {
    delay(100);   
  }
}

So long as state stays the same, i.e. it equals old, the code within the while loop will execute indefinitely. NOTE: Any code outside the while loop will effectively only run once. You may find, if you expand the code, most of it lives within the while loop. If that is the case there may be better approach to consider.

4
  • Thanks so much for your help! I really appreciate it! – Éemia Apr 27 at 14:25
  • I'm so glad you took the effort to answer me. Unfortunately, I've tried it out and the outcome was the same as before. Thanks anyways though! – Éemia Apr 27 at 14:46
  • Oh! You forgot to add noTone(11); at the end of the different tones! You will also need to reset state to zero when you reset old to zero, otherwise you will get stuck in an infinite loop. – CharlieHanson Apr 27 at 20:03
  • Oh my gosh thank you so much!!! You have no idea how much you've helped me! I am so grateful for your help. – Éemia Apr 28 at 7:32
0

Once the LEDs & Piezo sequence finishes, your code doesn't wait for the button to be pressed again. If the button is not pressed, the code doesn't wait, it merely delays for 100ms, then repeats the sequence.

0

Welcome to ASE :)

  1. You have mapped the Tone function and the LED to the same Pins. So, you may have to find another pin for the buzzer (pin 0 )
  2. I have taken code from @charlieHanson as well.
  3. The braces for the switch statement were put in the wrong place. I have fixed it.

Have a try: The code is here

#define button 3

#define LED 1
#define twoLED 2
#define threeLED 4
#define fourLED 5
#define fiveLED 6
#define sixLED 7
#define sevenLED 8
#define eightLED 9
#define nineLED 10
#define tenLED 11




int state = 0;
int old = 0;
int buttonPoll = 0;

void setup() {
  pinMode (3, INPUT_PULLUP);
  pinMode (11, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(twoLED, OUTPUT);
  pinMode(threeLED, OUTPUT);
  pinMode(fourLED, OUTPUT);
  pinMode(fiveLED, OUTPUT);
  pinMode(sixLED, OUTPUT);
  pinMode(sevenLED, OUTPUT);
  pinMode(eightLED, OUTPUT);
  pinMode(nineLED, OUTPUT);
  pinMode(tenLED, OUTPUT);


  digitalWrite(LED, LOW);
  digitalWrite(twoLED, LOW);
  digitalWrite(threeLED, LOW);
  digitalWrite(fourLED, LOW);
  digitalWrite(fiveLED, LOW);
  digitalWrite(sixLED, LOW);
  digitalWrite(sevenLED, LOW);
  digitalWrite(eightLED, LOW);
  digitalWrite(nineLED, LOW);
  digitalWrite(tenLED, LOW);

}

void loop() {



  while (state == old)
  {
    buttonPoll = digitalRead (3);
    if (buttonPoll == 1)
    {
      delay (50);
      buttonPoll = digitalRead (3);
      if (buttonPoll == 0)
      {
        state = old + 1;
      }
    }
    else
    {
      delay(100);
    }
  }

  switch (state) {
    case 1:
      digitalWrite(LED, HIGH);
      digitalWrite(twoLED, LOW);
      digitalWrite(threeLED, LOW);
      digitalWrite(fourLED, LOW);
      digitalWrite(fiveLED, LOW);
      digitalWrite(sixLED, LOW);
      digitalWrite(sevenLED, LOW);
      digitalWrite(eightLED, LOW);
      digitalWrite(nineLED, LOW);
      digitalWrite(tenLED, LOW);
      old = state;
      break;

    case 2:
      digitalWrite(LED, HIGH);
      digitalWrite(twoLED, HIGH);
      digitalWrite(threeLED, LOW);
      digitalWrite(fourLED, LOW);
      digitalWrite(fiveLED, LOW);
      digitalWrite(sixLED, LOW);
      digitalWrite(sevenLED, LOW);
      digitalWrite(eightLED, LOW);
      digitalWrite(nineLED, LOW);
      digitalWrite(tenLED, LOW);
      old = state;
      break;

    case 3:
      digitalWrite(LED, HIGH);
      digitalWrite(twoLED, HIGH);
      digitalWrite(threeLED, HIGH);
      digitalWrite(fourLED, LOW);
      digitalWrite(fiveLED, LOW);
      digitalWrite(sixLED, LOW);
      digitalWrite(sevenLED, LOW);
      digitalWrite(eightLED, LOW);
      digitalWrite(nineLED, LOW);
      digitalWrite(tenLED, LOW);
      old = state;
      break;

    case 4:
      digitalWrite(LED, HIGH);
      digitalWrite(twoLED, HIGH);
      digitalWrite(threeLED, HIGH);
      digitalWrite(fourLED, HIGH);
      digitalWrite(fiveLED, LOW);
      digitalWrite(sixLED, LOW);
      digitalWrite(sevenLED, LOW);
      digitalWrite(eightLED, LOW);
      digitalWrite(nineLED, LOW);
      digitalWrite(tenLED, LOW);
      old = state;
      break;

    case 5:
      digitalWrite(LED, HIGH);
      digitalWrite(twoLED, HIGH);
      digitalWrite(threeLED, HIGH);
      digitalWrite(fourLED, HIGH);
      digitalWrite(fiveLED, HIGH);
      digitalWrite(sixLED, LOW);
      digitalWrite(sevenLED, LOW);
      digitalWrite(eightLED, LOW);
      digitalWrite(nineLED, LOW);
      digitalWrite(tenLED, LOW);
      old = state;
      break;

    case 6:
      digitalWrite(LED, HIGH);
      digitalWrite(twoLED, HIGH);
      digitalWrite(threeLED, HIGH);
      digitalWrite(fourLED, HIGH);
      digitalWrite(fiveLED, HIGH);
      digitalWrite(sixLED, HIGH);
      digitalWrite(sevenLED, LOW);
      digitalWrite(eightLED, LOW);
      digitalWrite(nineLED, LOW);
      digitalWrite(tenLED, LOW);
      old = state;
      break;

    case 7:
      digitalWrite(LED, HIGH);
      digitalWrite(twoLED, HIGH);
      digitalWrite(threeLED, HIGH);
      digitalWrite(fourLED, HIGH);
      digitalWrite(fiveLED, HIGH);
      digitalWrite(sixLED, HIGH);
      digitalWrite(sevenLED, HIGH);
      digitalWrite(eightLED, LOW);
      digitalWrite(nineLED, LOW);
      digitalWrite(tenLED, LOW);
      old = state;
      break;

    case 8:
      digitalWrite(LED, HIGH);
      digitalWrite(twoLED, HIGH);
      digitalWrite(threeLED, HIGH);
      digitalWrite(fourLED, HIGH);
      digitalWrite(fiveLED, HIGH);
      digitalWrite(sixLED, HIGH);
      digitalWrite(sevenLED, HIGH);
      digitalWrite(eightLED, HIGH);
      digitalWrite(nineLED, LOW);
      digitalWrite(tenLED, LOW);
      old = state;
      break;

    case 9:
      digitalWrite(LED, HIGH);
      digitalWrite(twoLED, HIGH);
      digitalWrite(threeLED, HIGH);
      digitalWrite(fourLED, HIGH);
      digitalWrite(fiveLED, HIGH);
      digitalWrite(sixLED, HIGH);
      digitalWrite(sevenLED, HIGH);
      digitalWrite(eightLED, HIGH);
      digitalWrite(nineLED, HIGH);
      digitalWrite(tenLED, LOW);
      old = state;
      break;

    case 10:
      digitalWrite(LED, HIGH);
      digitalWrite(twoLED, HIGH);
      digitalWrite(threeLED, HIGH);
      digitalWrite(fourLED, HIGH);
      digitalWrite(fiveLED, HIGH);
      digitalWrite(sixLED, HIGH);
      digitalWrite(sevenLED, HIGH);
      digitalWrite(eightLED, HIGH);
      digitalWrite(nineLED, HIGH);
      digitalWrite(tenLED, HIGH);
      old = 0;
      state = 0;


      tone (11, 261);
      delay(250);

      tone(11, 330);
      delay(250);

      tone(11, 391.99);
      delay(250);

      tone(11, 440);
      delay(250);

      tone(11, 391.995);
      delay(250);

      tone(11, 330);
      delay(250);
      noTone(11);


      break;

    default:
      digitalWrite(LED, LOW);
      digitalWrite(twoLED, LOW);
      digitalWrite(threeLED, LOW);
      digitalWrite(fourLED, LOW);
      digitalWrite(fiveLED, LOW);
      digitalWrite(sixLED, LOW);
      digitalWrite(sevenLED, LOW);
      digitalWrite(eightLED, LOW);
      digitalWrite(nineLED, LOW);
      digitalWrite(tenLED, LOW);

      old = 0;

      break;

  }
}

Here is the preview :)
enter image description here

Here is the Arduino simulator link: https://wokwi.com/arduino/projects/297098737606459913

1
  • Wow, thanks so much!!! – Éemia Apr 29 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.