Why wont this work?

i have another very similar code for song and it interrupts and stops fine it wont for this and im not sure why.

id guess it has to do with the wait variable ive tried removing it or working around it but the other has a wait so?

quick side not i did get the song code from another source.

 if ( count != 8 ) return;
  if( count == 8)

  {
 const int totalNotes = sizeof(notes) / sizeof(int);
  // Loop through each note
  for (int i = 0; i < totalNotes; i++)
  {    
    const int currentNote = notes[i];
    float wait = durations[i] / songSpeed;
    // Play tone if currentNote is not 0 frequency, otherwise pause (noTone)
 
    if (currentNote != 0)
    {
      tone(buzzerPin, notes[i], wait); // tone(pin, frequency, duration)
    
     if ( count != 8 ) 
    {

      // Break from loop once button is released    
      break;        
      lcd.print(count);
      }
    }
    else
    {
      noTone(buzzerPin);
    }
    // delay is used to wait for tone to finish playing before moving to next loop
    delay(wait);

Hi,
You may have to explain more.
What is your hardware, what Arduino controller you are using.
A full listing of you code would possibly be better, using code tags.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hey Tom

Im using arduino uno, a rotary encoder is my controller. i haven really noted up all the rest but im basicly making a menu system. One section is LEDS i have working, i had the second song working fine but the code above is the isue in the //***** box. Its quite big i apologize, i also only have 36 hrs experience so you might cringe looking at it :D.

Thanks in advance

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

#include "pitches.h"



// Defining frequency of each music note
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_D5 587
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_G5 784
#define NOTE_A5 880
#define NOTE_B5 988

// Music notes of the song, 0 is a rest/pulse
int notes[] = {
    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
    NOTE_A4, NOTE_G4, NOTE_A4, 0,

    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
    NOTE_A4, NOTE_G4, NOTE_A4, 0,

    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
    NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
    NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,

    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_D5, NOTE_E5, NOTE_A4, 0,
    NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
    NOTE_C5, NOTE_A4, NOTE_B4, 0,

    NOTE_A4, NOTE_A4,
    //Repeat of first part
    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
    NOTE_A4, NOTE_G4, NOTE_A4, 0,

    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
    NOTE_A4, NOTE_G4, NOTE_A4, 0,

    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
    NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
    NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,

    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_D5, NOTE_E5, NOTE_A4, 0,
    NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
    NOTE_C5, NOTE_A4, NOTE_B4, 0,
    //End of Repeat

    NOTE_E5, 0, 0, NOTE_F5, 0, 0,
    NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
    NOTE_D5, 0, 0, NOTE_C5, 0, 0,
    NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4,

    NOTE_E5, 0, 0, NOTE_F5, 0, 0,
    NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
    NOTE_D5, 0, 0, NOTE_C5, 0, 0,
    NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4};

// Durations (in ms) of each music note of the song
// Quarter Note is 250 ms when songSpeed = 1.0
int durations[] = {
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 125,

    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 125,

    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 125, 250, 125,

    125, 125, 250, 125, 125,
    250, 125, 250, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 375,

    250, 125,
    //Rpeat of First Part
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 125,

    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 125,

    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 125, 250, 125,

    125, 125, 250, 125, 125,
    250, 125, 250, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 375,
    //End of Repeat

    250, 125, 375, 250, 125, 375,
    125, 125, 125, 125, 125, 125, 125, 125, 375,
    250, 125, 375, 250, 125, 375,
    125, 125, 125, 125, 125, 500,

    250, 125, 375, 250, 125, 375,
    125, 125, 125, 125, 125, 125, 125, 125, 375,
    250, 125, 375, 250, 125, 375,
    125, 125, 125, 125, 125, 500};


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
};

const float songSpeed = 1.0;

#define redLED 6
#define greenLED 5
#define blueLED 13

int red = 6; //select the pin for the red LED
int green =5;// select the pin for the green LED
int blue =13; // select the pin for the  blue LED


int CLK = 2;//CLK->D2
int DT = 3;//DT->D3
int SW = 4;//SW->D4
const int interrupt0 = 0;// Interrupt 0 在 pin 2 上
int count = 0;//Define the count
int lastCLK = 0;//CLK initial value

int btn = 1;
int btncount;

int buzzerPin = A3;

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

   pinMode(btn,INPUT);
 //RGB LED set as output
 pinMode(redLED,OUTPUT);
 pinMode(greenLED,OUTPUT);
 pinMode(blueLED,OUTPUT);

 digitalWrite(redLED,LOW);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);


   pinMode(btn, INPUT);
 pinMode(buzzerPin, OUTPUT);
 Serial.begin(9600);
  noTone(buzzerPin);

  
   pinMode(SW, INPUT);
  analogWrite(SW, HIGH);
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  attachInterrupt(interrupt0, ClockChanged, CHANGE);//Set the interrupt 0 handler, trigger level change
  pinMode(btn, INPUT);

  
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(5, 1);
  lcd.print("start");
  delay(5000); 
  lcd.clear();
    lcd.setCursor(4, 1);
    lcd.print("welcome");
    delay(2000);
   count = 15;
}

void loop() {

if(count <= 29 && count >= 15)
{
digitalWrite(redLED,LOW);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);
lcd.setCursor(6, 0);
  lcd.print("Menu");
    lcd.setCursor(0, 1);
  lcd.print("30= LED 13=Song");
    delay(500);
    lcd.clear();
}


 
 if(count <= 14 && count >=8)
 {
lcd.setCursor(6, 0);
  lcd.print("Menu");
    lcd.setCursor(0, 1);
  lcd.print("6=cccp 8=pirate");
  //lcd.clear();
 // delay(500);

 
 

//*******************************

 if ( count != 8 ) return;
  if( count == 8)

  {
 const int totalNotes = sizeof(notes) / sizeof(int);
  // Loop through each note
  for (int i = 0; i < totalNotes; i++)
  {    
    const int currentNote = notes[i];
    float wait = durations[i] / songSpeed;
    // Play tone if currentNote is not 0 frequency, otherwise pause (noTone)
 
    if (currentNote != 0)
    {
      tone(buzzerPin, notes[i], wait); // tone(pin, frequency, duration)
    
     if ( count != 8 ) 
    {

      // Break from loop once button is released    
      break;        
      lcd.print(count);
      }
    }
    else
    {
      noTone(buzzerPin);
    }
    // delay is used to wait for tone to finish playing before moving to next loop
    delay(wait);

  
  }

  }
 
//**********************************

   
    
    
    // Skip rest of code if not truet pressed
  if ( count != 6 ) return;
  if( count == 6)
  {
  // Execute once button has been released
  for ( int thisNote = 0; thisNote < sizeof( melody ) / 2; thisNote++ ) 
  {
    int noteDuration = 2000 / noteDurations[thisNote];
    tone( buzzerPin, melody[thisNote], noteDuration );
    
  digitalWrite(redLED,HIGH);
digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);
    
    int pauseBetweenNotes = noteDuration * 1.30;
    delay( pauseBetweenNotes );

    // Check for a button press
    if ( count != 6 ) 
    {

      // Break from loop once button is released    
      break;        
      lcd.print(count);
      }
    }
   
  }
 

if(count >= 29){

digitalWrite(redLED,LOW);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);

  lcd.setCursor(3, 0);
  lcd.print("LED menu");
  delay(3000); 
  lcd.clear();
   
  lcd.setCursor(0, 1);
  lcd.print("39 for red");
   delay(3000); 
     lcd.clear(); 
  
     if (count == 39){
     while (count == 39){
    lcd.print("red");
    digitalWrite(redLED,HIGH);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);
  } 
}   
     
   lcd.setCursor(0, 0);
    lcd.print("49 for green");
    delay(3000);
      lcd.clear();

    if (count == 49) {
    while (count == 49){
    lcd.print("green");
     digitalWrite(redLED,LOW);
 digitalWrite(greenLED,HIGH);
 digitalWrite(blueLED,LOW);
  }  
}
  
  lcd.setCursor(1, 1);
  lcd.print("49 for blue");
   delay(3000);
      lcd.clear();

  
  if (count == 59 ) {
    while (count == 59){
    lcd.print("blue");
     digitalWrite(redLED,LOW);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,HIGH);
  } 
}      
   
   lcd.setCursor(0, 0);
    lcd.print("69 for purple");
     delay(3000);
      lcd.clear();


       if (count == 69 ) {
    while (count == 69){
    lcd.print("Purple");
     digitalWrite(redLED,HIGH);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,HIGH);
  }  
}   

    lcd.setCursor(1, 1);
  lcd.print("79 for white");
   delay(3000);
      lcd.clear();


      if (count == 79)  {
    while (count == 79){
    lcd.print("White");
     digitalWrite(redLED,HIGH);
 digitalWrite(greenLED,HIGH);
 digitalWrite(blueLED,HIGH);
  } 
}  


   lcd.setCursor(0, 0);
    lcd.print("99 to menu");
       delay(3000);
      lcd.clear();
  
 
      if (count >= 99) {
      while (count >= 99){
    lcd.print("going back");
    delay(4000);
   count = 0;
    lcd.clear();
    return;
      } 
    }   
   }     
}
}
//The interrupt handlers
void ClockChanged()
{
  int clkValue = digitalRead(CLK);//Read the CLK pin level
  int dtValue = digitalRead(DT);//Read the DT pin level
  if (lastCLK != clkValue)
  {
    lastCLK = clkValue;
    count += (clkValue != dtValue ? 1 : -1);//CLK and inconsistent DT + 1, otherwise - 1

   delay(500); 
  lcd.clear();
    lcd.print(count);
   
   
  }
}

also the issue is just it wont interrupt and stop bar that everything is good :melting_face:

Looks like you've thrown yourself into the deep end of the kiddie pool, so to speak.

Please supply a link to the source(s) from which you borrowed or copied the code or the ideas informing your code.

And don't worry, everyone here has written cringe-worthy code; some still do. :wink:

a7

// Define pin 10 for buzzer, you can use any other digital pins (Pin 0-13)
const int buzzer = 10;

// Change to 0.5 for a slower version of the song, 1.25 for a faster version
const float songSpeed = 1.0;

// Defining frequency of each music note
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_D5 587
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_G5 784
#define NOTE_A5 880
#define NOTE_B5 988

// Music notes of the song, 0 is a rest/pulse
int notes[] = {
    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
    NOTE_A4, NOTE_G4, NOTE_A4, 0,

    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
    NOTE_A4, NOTE_G4, NOTE_A4, 0,

    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
    NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
    NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,

    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_D5, NOTE_E5, NOTE_A4, 0,
    NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
    NOTE_C5, NOTE_A4, NOTE_B4, 0,

    NOTE_A4, NOTE_A4,
    //Repeat of first part
    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
    NOTE_A4, NOTE_G4, NOTE_A4, 0,

    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
    NOTE_A4, NOTE_G4, NOTE_A4, 0,

    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
    NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
    NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,

    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_D5, NOTE_E5, NOTE_A4, 0,
    NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
    NOTE_C5, NOTE_A4, NOTE_B4, 0,
    //End of Repeat

    NOTE_E5, 0, 0, NOTE_F5, 0, 0,
    NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
    NOTE_D5, 0, 0, NOTE_C5, 0, 0,
    NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4,

    NOTE_E5, 0, 0, NOTE_F5, 0, 0,
    NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
    NOTE_D5, 0, 0, NOTE_C5, 0, 0,
    NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4};

// Durations (in ms) of each music note of the song
// Quarter Note is 250 ms when songSpeed = 1.0
int durations[] = {
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 125,

    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 125,

    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 125, 250, 125,

    125, 125, 250, 125, 125,
    250, 125, 250, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 375,

    250, 125,
    //Rpeat of First Part
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 125,

    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 125,

    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 125, 250, 125,

    125, 125, 250, 125, 125,
    250, 125, 250, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 375,
    //End of Repeat

    250, 125, 375, 250, 125, 375,
    125, 125, 125, 125, 125, 125, 125, 125, 375,
    250, 125, 375, 250, 125, 375,
    125, 125, 125, 125, 125, 500,

    250, 125, 375, 250, 125, 375,
    125, 125, 125, 125, 125, 125, 125, 125, 375,
    250, 125, 375, 250, 125, 375,
    125, 125, 125, 125, 125, 500};

void setup()
{
  const int totalNotes = sizeof(notes) / sizeof(int);
  // Loop through each note
  for (int i = 0; i < totalNotes; i++)
  {
    const int currentNote = notes[i];
    float wait = durations[i] / songSpeed;
    // Play tone if currentNote is not 0 frequency, otherwise pause (noTone)
    if (currentNote != 0)
    {
      tone(buzzer, notes[i], wait); // tone(pin, frequency, duration)
    }
    else
    {
      noTone(buzzer);
    }
    // delay is used to wait for tone to finish playing before moving to next loop
    delay(wait);
  }
}

void loop()
{
  // Nothing in loop. Music only plays once.
  // You can click reset on Arduino to replay the song.
}

haha thanks man

OK what I meant was a link to the web pages where you got the various pieces, not just the code.

And the new code you posted does not seem to use interrupts.

Explain the roll of the variable count in the first code you posted.

Interrupts are almost never the answer for simple programs. Using them requires care and an understanding it is hard to think you have, so tinkering with code that exploits them will be way harder than doing it "the hard way".

You have had some success, but programming by cafeteria selection of pieces from here and there will never be easy.

I suggest that you slow your roll and learn a bit more about the basics of programming. The IDE has many examp,es. The more you read and experiment with simple code the easier it will be to leverage off existing code you find and also begin to write mostly your own code.

When I can I take a closer look at what you are trying to do.

HTH

a7

1 Like

its just that bit ive taxed from online all the rest is mine, its not an interrupt per say idk what the word is but like if i change the dial it will stop the song. ive got it to start easily via input i just cant figure out how to stop it.

its this one bit ive boxed causing me all the problems .

The very simplest way to stop it is to poll a switch in here:

    // delay is used to wait for tone to finish playing before moving to next loop
    delay(wait);
   // check your "stop" switch here:
   if (stopSwitch == LOW) break;

Just an untested example and you have to roll your own stop switch and code.

that's meant to be what this is but it has no effect, ive tryed to move it around to different spots but nothing

 if ( count != 8 ) 
    {

      // Break from loop once button is released    
      break;        
      lcd.print(count);
      }

works here perfectly

  // Skip rest of code if not true pressed
  if ( count != 6 ) return;
  if( count == 6)
  {
  // Execute once button has been released
  for ( int thisNote = 0; thisNote < sizeof( melody ) / 2; thisNote++ ) 
  {
    int noteDuration = 2000 / noteDurations[thisNote];
    tone( buzzerPin, melody[thisNote], noteDuration );
    
  digitalWrite(redLED,HIGH);
digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);
    
    int pauseBetweenNotes = noteDuration * 1.30;
    delay( pauseBetweenNotes );

    // Check for a button press
    if ( count != 6 ) 
    {

      // Break from loop once button is released    
      break;        
      lcd.print(count);
      }
    }
   
  }

Confused. Is your problem solved?

No theres 2 songs one works with the stop and the other dosent. ive tryed to put the stop everywhere but it wont read it, its permanently stuck in the loop. CCCP works fine. Pirate wont.

 if(count <= 14 ) //&& count >=8
 {
lcd.setCursor(6, 0);
  lcd.print("Menu");
    lcd.setCursor(0, 1);
  lcd.print("7=cccp 6=pirate");
  delay(1000);
  lcd.clear();
  



/***************CCCP*********************/   
    
    
    // Skip rest of code if not truet pressed
  if ( count !=  7 ) return;
  if( count == 7 || count == 9)
  {
  // Execute once button has been released
  for ( int thisNote = 0; thisNote < sizeof( melody ) / 2; thisNote++ ) 
  {
    int noteDuration = 2000 / noteDurations[thisNote];
    tone( buzzerPin, melody[thisNote], noteDuration );
      
      digitalWrite(redLED,HIGH);
digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);
    lcd.clear();
    int pauseBetweenNotes = noteDuration * 1.30;
    delay( pauseBetweenNotes );

    // Check for a button press
    if ( count != 7 ) 
    {
      // Break from loop once button is released    
       lcd.print(count);
       delay(3000);
       count = 13;
      
      break;        
      
      }
    }
  }
  }

/***************CCCP*********************/   


 
/**************PIRATES*****************/

 if ( count != 6 ) return;
  if( count == 6)

  {
 const int totalNotes = sizeof(notes) / sizeof(int);
  // Loop through each note
  for (int i = 0; i < totalNotes; i++)
  {    
    const int currentNote = notes[i];
    float wait = durations[i] / songSpeed;
    // Play tone if currentNote is not 0 frequency, otherwise pause (noTone)
 
    if (currentNote != 0)
    {
      tone(buzzerPin, notes[i], wait); // tone(pin, frequency, duration)
    
     if ( count != 6 ) 
    {

      // Break from loop once button is released    
      break;        
      lcd.print(count);
      }
    }
    else
    {
      noTone(buzzerPin);
    }
    // delay is used to wait for tone to finish playing before moving to next loop
    delay(wait);

  
  }

  }
 
/***************PIRATES*******************/

would my return in CCCP effect it going to the next part mabey?

Please stop posting snippets. Only post complete sketches in separate posts, together with an explanation of how it behaves.

You never answered this question from post #8.

it reads the encoder

Not properly. You have broken all kinds of rules for passing a value from an ISR to the main program. Study up.
https://gammon.com.au/interrupts

Also, using delay() in an interrupt is a complete fail, and updating the LCD in an interrupt is not recommended, even if it works (does it?).

yeah everything works perfectly bar the one song not stopping when i change the count variable by rotating the encoder.