• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ how to exit for loop?

redcccp2002

New Coder
im pretty new to coding, ive been searching around and nothing i can find will help, im using arduino and trying to use a push button to start and stop a song. it responds to it starting but wont stop midway or anything till done.



C++:
#include "pitches.h"

int buzzerPin = 6;
int btn = 2;

int melody[] = {
  NOTE_G4, NOTE_C5, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_E4, NOTE_E4,
  NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_C4,
  NOTE_D4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5,
  NOTE_E5, NOTE_D5, NOTE_C5, NOTE_D5, NOTE_B4, NOTE_G4,
  NOTE_C5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_E4, NOTE_E4,
  NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_C4,
  NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_B4, NOTE_C5, NOTE_D5,
  NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_G4, NOTE_G4, NOTE_B4, NOTE_C5, NOTE_D5,
  NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_A4, NOTE_B4,
  NOTE_C5, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_C5, NOTE_F5,
  NOTE_F5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C5, NOTE_C5,
  NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_A4,
  NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_C4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};

int noteDurations[] = {
  8, 4, 6, 16, 4, 8, 8,
  4, 6, 16, 4, 8, 8,
  4, 8, 8, 4, 8, 8, 4, 8, 8, 2,
  4, 6, 16, 4, 8, 8,
  4, 6, 16, 4, 8, 8,
  4, 6, 16, 4, 6, 16,
  4, 6, 16, 8, 8, 8, 8,
  2, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8,
  2, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8,
  4, 6, 16, 4, 6, 16, 4, 8, 8, 2,
  2, 8, 8, 8, 8, 3, 8, 2,
  2, 8, 8, 8, 8, 3, 8, 2,
  4, 6, 16, 4, 4, 2, 4, 4, 1
};

void setup() {
 pinMode(btn, INPUT);
 pinMode(buzzerPin, OUTPUT);
 Serial.begin(9600);
  noTone(buzzerPin);
}

void loop() {
  
   byte buttonState = digitalRead(btn);  //read the button and save its state
Serial.println(buttonState);//print for debugging

if (buttonState == LOW){
}
      
      
    for (int thisNote = 0; thisNote < sizeof(melody) / 2; thisNote++) {{
    if (buttonState == HIGH)break;
       digitalWrite(buzzerPin, LOW);
    
    
    int noteDuration = 2000 / noteDurations[thisNote];
    tone(buzzerPin, melody[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
  
    }
          }
    Serial.println(buttonState);
  }
 
Hello, @redcccp2002.

If you want to exit a loop(whether it be a for loop, while loop, do...while loop), you'll need to use the break statement. What this does is it "breaks" the loop and prevents it from continuing.

I don't know or use Arduino so I can't really help you much at all. I've noticed that you have a break statement in your if-statement which looking at, is contained within the for loop. I don't see anything wrong with the for loop itself - since I can't test this for myself, my suggestion would be to play around with the break statement, see where it works to get your desired result.

I hope this helped.
 
Top Bottom