Coding issues using for loops

Hi, I'm trying to make a voice activated swing. I needed the music and the swing commands to run on a for loop. How can i stop the for loop by saying the command "Swing Off" or "Music Off."
Right now the loop will run until all the iterations have completed. I need to stop the loop in the middle.
I am using an arduino Mega.
Below is my code:

#include <HardwareSerial.h> 
#include <Arduino.h> 
#include <Wire.h> 
#include "pitches.h"
extern HardwareSerial Serial1; 
extern HardwareSerial Serial2; 
char incomingByte; 
char incomingByte1; 
int NUMswings = 100;
//Speaker 
const int Speaker = 11; 
int numTones1 = 2;
int numTones2 = 6;
int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440};
//Speaker
int TimeSongs=100;
int lullaby[] = { NOTE_G6, NOTE_G6,  NOTE_AS6,  NOTE_G6, NOTE_G6,  NOTE_AS6,  NOTE_G6, NOTE_AS6, NOTE_DS7,  NOTE_D7, NOTE_C7, NOTE_C7,   NOTE_AS6,  NOTE_F6,  NOTE_G6,  NOTE_GS6, NOTE_F6,  NOTE_F6, NOTE_G6, NOTE_GS6, NOTE_F6,  NOTE_GS6,  NOTE_D7, NOTE_C7, NOTE_AS6,   NOTE_D7,  NOTE_DS7};
int noteDurations[] = {      6,       8,         2.5,        8,       4,         2,        6,        10,        4,        3,       8,       4,       4,        6,       8,         4,       4,        6,       8,       2.5,       6,         8,        8,       8,        4,         4,         2};
//L293D 
//Swing Motor (Big motor) 
const int motorPin1 = 5; // Pin 14 of L293 
const int motorPin2 = 6; // Pin 10 of L293 
//Toy Motor (Small Motor) 
const int motorPin3 = 10; // Pin 7 of L293 
const int motorPin4 = 9; // Pin 2 of L293 
//int EN1 = 3; // Pin 1 of L293D IC, D6 Pin of NodeMCU (controls the speed) 
void setup() { 
 //Set motor pins as outputs 
 pinMode(motorPin1, OUTPUT); 
 pinMode(motorPin2, OUTPUT); 
 pinMode(motorPin3, OUTPUT); 
 pinMode(motorPin4, OUTPUT); 
 //pinMode(EN1, OUTPUT); //Where the motor is connected to 
pinMode(Speaker, OUTPUT);

 Serial.begin(115200); 
 Serial2.begin(115200); 
 Serial.println("Ready for commands"); 

 
} 
void loop() { 
 ///// 
 if (Serial2.available() > 1) { 
 // read the incoming byte: 
 incomingByte = Serial2.read(); 
 incomingByte1 = Serial2.read(); 
 Serial.print ("Received data = "); 
 Serial.print(int(incomingByte) , DEC); 
 Serial.println(int(incomingByte1) , DEC); 

if(incomingByte == 0)
 {
  Serial.print("Power On");
    for (int i = 0; i < numTones1; i++)
  {
    tone(Speaker, tones[i]);
    delay(500);
  }
  noTone(Speaker);
    Serial.println("I received: ");
    Serial.println(incomingByte1, DEC);
 }
  if(incomingByte == 1)
 {
  Serial.print("Power Off");
    for (int i = 4; i < numTones2; i++)
  {
    tone(Speaker, tones[i]);
    delay(500);
  }
  noTone(Speaker);
    incomingByte = 0;
    Serial.println("I received: ");
    Serial.println(incomingByte1, DEC);
    
 } 
 if (incomingByte == 2)
 {
  Serial.print("Swing On");
  while(int i=0 <NUMswings){
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    delay(1000);
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    delay(1000);
    incomingByte = 0;
    Serial.println("I received: ");
    Serial.println(incomingByte1, DEC);
 }
  if(incomingByte == 3)
 {
  Serial.print("Swing Off");
   digitalWrite(motorPin1, LOW);
   digitalWrite(motorPin2, LOW);
    incomingByte = 0;
    Serial.println("I received: ");
    Serial.println(incomingByte1, DEC);}
 if(incomingByte == 4)
 {
  Serial.print("Toys On");
    digitalWrite(motorPin3, HIGH);
    digitalWrite(motorPin4, LOW);
    incomingByte = 0;
    Serial.println("I received: ");
    Serial.println(incomingByte1, DEC);
 }
  if(incomingByte == 5)
 {
  Serial.print("Toys Off");
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    incomingByte = 0;
    Serial.println("I received: ");
    Serial.println(incomingByte1, DEC);
 }    
if(incomingByte == 6)
 {
  Serial.print("Music On");
  digitalWrite(Speaker, HIGH);
    for(int i=0; i<TimeSongs; i++){
    for (int thisNote = 0; thisNote < 27; thisNote++){ 
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(Speaker, lullaby[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);}}
    incomingByte = 0;
    Serial.println("I received: ");
    Serial.println(incomingByte1, DEC);
 }
if(incomingByte == 7)
 {
  Serial.print("Music Off");
    digitalWrite(Speaker, LOW);
    noTone(Speaker);
    incomingByte = 0;
    Serial.println("I received: ");
    Serial.println(incomingByte1, DEC);    
 }    }}}

An expression of an opinion. For voice recognition, Machine Learning( ML) is the way to go.

Do a search on "arduino nano 33 ble machine learning".

Arduino Machine Learning: Speech Recognition | Arrow.com | Arrow.com

Thanks but i don't have time for that my project is due tomorrow. And everything works i just need two commands to keep running over and over.

Within the if statement i need the commands to run on a loop

I figured it out by using a for loop but now the "Music off" command isn't being processed

It's not our problem. Also, you posted in the wrong forum. That cost you time.

what is the correct forum.

Programming Questions. Ask a moderator to move it.

How?

figured it out

for() loops, and while() loops with delay() calls are almost guaranteed to slow down & block timely execution of functionality.

If you can ‘escape’ those loops, or rewrite to use millis() timing, you have a decent chance of making things happen as you want.

The best programs are planned, and rarely executed from end to end in one pass.
Learning about millis(), and how to use it to make your programs ‘appear’ smarter will be the best day you ever spent learning about coding.

The code will look a little more complex at first, but good structure and logical thinking will make that pale into insignificance… tomorrow may be a bit soon, unless you stay up all night.

In general, for loops and the like will run to completion unless you use "break" or return to exit them. To do that you'd need to check all possible conditions to exit such loop for every instance where a loop is run. And you can't check things like that during a delay (e.g. delay(1000) is one second of life you'll never get back...)

You need to think about non-blocking ways to do things and, as @lastchancename notes, structure to allow things to change on the fly.

Here's an example of an attempt to modify your code to use millis() and other non-blocking techniques. I did some rudimentary tests on a couple of functions. Even if it doesn't work, perhaps you'll gain some idea about how this type of programming might be done.

#include <HardwareSerial.h> 
#include <Arduino.h> 
#include <Wire.h> 
#include "pitches.h"

extern HardwareSerial Serial1; 
extern HardwareSerial Serial2; 

//prototypes
bool PowerOn();
bool PowerOff();
bool SwingOn();
bool SwingOff();
bool ToysOn();
bool ToysOff();
bool MusicOn();
bool MusicOff();
bool IdleFunc();

typedef bool (*pFunc_t)();

pFunc_t pFunctions[] = 
{    
    &PowerOn,
    &PowerOff,
    &SwingOn,
    &SwingOff,
    &ToysOn,
    &ToysOff,
    &MusicOn,
    &MusicOff,
    &IdleFunc
        
};

const int NUMswings = 100;
const int numTones1 = 2;
const int numTones2 = 6;
const int TimeSongs = 100;

//Swing Motor (Big motor) 
const int motorPin1 = 5; // Pin 14 of L293 
const int motorPin2 = 6; // Pin 10 of L293 
//Toy Motor (Small Motor) 
const int motorPin3 = 10; // Pin 7 of L293 
const int motorPin4 = 9; // Pin 2 of L293 
//Speaker
const uint8_t Speaker = 11; 

uint8_t funcIndex;
char incomingByte; 
char incomingByte1; 

int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440};
//Speaker
int lullaby[] = 
{ 
    NOTE_G6, NOTE_G6, NOTE_AS6, NOTE_G6, NOTE_G6, NOTE_AS6,  
    NOTE_G6, NOTE_AS6, NOTE_DS7, NOTE_D7, NOTE_C7, NOTE_C7,
    NOTE_AS6, NOTE_F6, NOTE_G6, NOTE_GS6, NOTE_F6, NOTE_F6, 
    NOTE_G6, NOTE_GS6, NOTE_F6, NOTE_GS6, NOTE_D7, NOTE_C7, 
    NOTE_AS6, NOTE_D7, NOTE_DS7
};

float noteDurations[] = 
{      
    6.0, 8.0, 2.5, 8.0, 4.0, 2.0, 6.0, 10.0, 4.0, 3.0, 8.0, 
    4.0, 4.0, 6.0, 8.0, 4.0, 4.0, 6.0, 8.0, 2.5, 6.0, 8.0, 
    8.0, 8.0, 4.0, 4.0, 2.0
    
};

//L293D 
//int EN1 = 3; // Pin 1 of L293D IC, D6 Pin of NodeMCU (controls the speed) 

void setup() 
{ 
    //Set motor pins as outputs 
    pinMode(motorPin1, OUTPUT); 
    pinMode(motorPin2, OUTPUT); 
    pinMode(motorPin3, OUTPUT); 
    pinMode(motorPin4, OUTPUT); 
    //pinMode(EN1, OUTPUT); //Where the motor is connected to 
    pinMode(Speaker, OUTPUT);

    Serial.begin(115200); 
    Serial2.begin(115200); 
    Serial.println("Ready for commands"); 

    funcIndex = 8;  //point to idle function
    
}//setup
 
void loop() 
{ 
    //execute the active function
    if( pFunctions[funcIndex]() == false )
        funcIndex = 8;
    
    if (Serial2.available() > 1)     
    { 
        // read the incoming byte: 
        incomingByte = Serial2.read(); 
        incomingByte1 = Serial2.read(); 
        Serial.print ("Received data = "); 
        Serial.print( int(incomingByte), DEC);
        Serial.write( ' ' );
        Serial.println( int(incomingByte1), DEC); 

/*
 *      if using the serial monitor and typing '0' you need to subtract '0' from the result
 *      if your source is actual binary, this can remain commented
        //for testing purposes
        if( incomingByte < 0x30 )
            incomingByte = 0x30;
        if( incomingByte > 0x38 )
            incomingByte = 0x38;
        incomingByte -= 0x30;
 */        
        if( incomingByte > 8 )
            funcIndex = 8;
        else
            funcIndex = incomingByte;
        
    }//if
    
}//loop

bool PowerOn( void )
{
    static uint8_t
        i,
        state = 0;
    static uint32_t
        tDelay;
    uint32_t
        tNow = millis();

    switch( state )
    {
        case    0:
            Serial.println("Power On");
            Serial.print("I received: ");
            Serial.println(incomingByte1, DEC);            
            i = 0;
            state++;
            
        break;

        case    1:
            tone(Speaker, tones[i]);
            tDelay = tNow;
            state++;
            
        break;

        case    2:
            if( (tNow - tDelay) >= 500ul )
            {
                i++;
                if( i<numTones1 )
                    state = 1;
                else
                {
                    noTone(Speaker);
                    state = 0;
                    return false;
                    
                }//else
                
            }//if
            
        break;
            
    }//switch

    return true;

}//PowerOn

bool PowerOff( void )
{
    static uint8_t
        i,
        state = 0;
    static uint32_t
        tDelay;
    uint32_t
        tNow = millis();
    
    switch( state )
    {
        case    0:
            Serial.println("Power Off");
            Serial.print("I received: ");
            Serial.println(incomingByte1, DEC);            
            i = 4;
            state++;
            
        break;

        case    1:
            tone(Speaker, tones[i]);
            tDelay = tNow;
            state++;
            
        break;

        case    2:
            if( (tNow - tDelay) >= 500ul )
            {
                i++;
                if( i<numTones2 )
                    state = 1;
                else
                {
                    noTone(Speaker);
                    state = 0;
                    return false;
                    
                }//else
                
            }//if
            
        break;
            
    }//switch

    return true;

}//PowerOff

bool SwingOn( void )
{
    static uint8_t        
        i,
        state = 0;
    static bool
        bSwing;
    static uint32_t
        tDelay;
    uint32_t
        tNow = millis();

    switch( state )
    {
        case    0:
            Serial.println("Swing On");    
            Serial.print("I received: ");
            Serial.println(incomingByte1, DEC);            
            
            i = 0;
            bSwing = false;
            digitalWrite(motorPin1, LOW);   //set things in motion
            digitalWrite(motorPin2, HIGH);
            tDelay = tNow;
            state++;
        break;

        case    1:
            if( (tNow - tDelay) >= 1000ul )
            {
                digitalWrite(motorPin1, digitalRead( motorPin1 ) ^ HIGH);
                digitalWrite(motorPin2, digitalRead( motorPin2 ) ^ HIGH);
                tDelay = tNow;
                bSwing ^= true;
                if( bSwing == false )
                {
                    i++;
                    if( i == NUMswings )
                    {
                        digitalWrite(motorPin1, LOW);
                        digitalWrite(motorPin2, LOW);
                        state = 0;
                        return false;
                        
                    }//if
                    
                }//if
                
            }//if
            
        break;

    }//switch

    return true;
        
}//SwingOn

bool SwingOff( void )
{
    Serial.println("Swing Off");
    Serial.print("I received: ");
    Serial.println(incomingByte1, DEC);            
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    
    return false;
        
}//SwingOff

bool ToysOn( void )
{
    Serial.println("Toys On");
    Serial.print("I received: ");
    Serial.println(incomingByte1, DEC);            
    digitalWrite(motorPin3, HIGH);
    digitalWrite(motorPin4, LOW);
    
    return false;

}//ToysOn

bool ToysOff( void )
{
    Serial.println("Toys Off");
    Serial.print("I received: ");
    Serial.println(incomingByte1, DEC);            
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    
    return false;

}//ToysOff

bool MusicOn( void )
{
    static uint8_t
        i, thisNote,
        state = 0;
    static uint32_t
        tStart,
        tDelay;
    static int
        noteDuration;
    uint32_t
        tNow = millis();
    
    switch( state )
    {
        case    0:
            Serial.println("Music On");
            Serial.print("I received: ");
            Serial.println(incomingByte1, DEC);            
            digitalWrite( Speaker, HIGH );
            i = 0;
            thisNote = 0;
            state++;
            
        break;

        case    1:
            noteDuration = (uint32_t)(1000.0 / noteDurations[thisNote]);
            tone(Speaker, lullaby[thisNote], noteDuration);            
            tDelay = (uint32_t)(noteDuration * 1.30);
            tStart = tNow;
            state++;
            
        break;

        case    2:
            if( (tNow - tDelay) >= tDelay )
            {
                thisNote++;
                if( thisNote == 27 )
                {
                    thisNote = 0;
                    i++;
                    if( i == TimeSongs )
                    {
                        noTone( Speaker );
                        state = 0;
                        return false;
                            
                    }//if
                    else
                    {
                        state = 1;
                        
                    }//else
                    
                }//if
                
            }//if

        break;

    }//switch

    return true;
                
}//MusicOn

bool MusicOff( void )
{
    Serial.println("Music Off");
    Serial.print("I received: ");
    Serial.println(incomingByte1, DEC);            
    digitalWrite(Speaker, LOW);
    noTone(Speaker);

    return false;
        
}//MusicOff

bool IdleFunc( void )
{
    //just an "idle" function that does nothing
    //used when no other command is running
    return true;
    
}//Idle