Making 2 Codes Run Simultaneously [Loop()]

How can I run two codes simultaneously like they are both in loop statements but separate ones. Because you can't make two loops run together, how would this code be able to run both, the speaker/led and rgb at the same time?

int redLed1 = 8;
int redLed2 = 9;
int whiteLed = 10;
int blueLed1 = 11;
int blueLed2 = 12;
int i=0;

void setup() {
  pinMode(4, OUTPUT);
  pinMode(redLed1, OUTPUT);
  pinMode(redLed2, OUTPUT);
  pinMode(whiteLed, OUTPUT);
  pinMode(A0, INPUT);
 
}

  

void loop() 
{
  	for(i=600;i<800;i++){ //WEEEEEE
  	tone(4,i);
  	delay(10);
  	}
  	for(i=800;i>600;i--){ //WOOOOOOOO
  	tone(4,i);
  	delay(10);
  	}
  	for(i=600;i<800;i++){ //WEEEEEE
  	tone(4,i);
  	delay(10);
  	}
  	for(i=800;i>600;i--){ //WOOOOOOOO
  	tone(4,i);
  	delay(10);
  	}
  	tone(4,800,300); //D
  
  	
  
  	digitalWrite(redLed1, HIGH);
  	digitalWrite(redLed2, HIGH);
  	digitalWrite(whiteLed, HIGH);
  	digitalWrite(blueLed1, LOW);
  	digitalWrite(blueLed2, LOW);
  	delay(300);
  	tone(4,650,300); //
  	digitalWrite(blueLed1, HIGH);
   	digitalWrite(blueLed2, HIGH);
  	digitalWrite(redLed1, LOW);
  	digitalWrite(redLed2, LOW);
  	delay(300);
  	tone(4,800,300); //D
  	digitalWrite(redLed2, HIGH);
  	digitalWrite(redLed1, HIGH);
  	digitalWrite(blueLed1, LOW);
 	digitalWrite(blueLed2, LOW);
 	delay(300);
 	tone(4,650,300);
   	digitalWrite(blueLed1, HIGH);
   	digitalWrite(blueLed2, HIGH);
    digitalWrite(redLed2, LOW);
   	digitalWrite(redLed1, LOW);//
  	delay(300);
  	tone(4,800,300); //D
  	digitalWrite(redLed2, HIGH);
  	digitalWrite(redLed1, HIGH);
  	digitalWrite(blueLed1, LOW);
  	digitalWrite(blueLed2, LOW);
  	delay(300);
  	tone(4,650,300); //
   	digitalWrite(blueLed1, HIGH);
   	digitalWrite(blueLed2, HIGH);
    digitalWrite(redLed2, LOW);
   	digitalWrite(redLed1, LOW);//
  	delay(300);
  	tone(4,800,300); //D
  	digitalWrite(redLed2, HIGH);
   	digitalWrite(redLed1, HIGH);
  	digitalWrite(blueLed1, LOW);
  	digitalWrite(blueLed2, LOW);
  	delay(300);
  	tone(4,650,300); //
   digitalWrite(blueLed1, HIGH);
   digitalWrite(blueLed2, HIGH);
   digitalWrite(redLed1, LOW);//
  digitalWrite(redLed2, LOW);
  delay(300);
  tone(4,800,300); //D
  digitalWrite(redLed2, HIGH);
  digitalWrite(redLed1, HIGH);
  digitalWrite(blueLed1, LOW);
  digitalWrite(blueLed2, LOW);
  delay(300);
  tone(4,650,300); //
   digitalWrite(blueLed1, HIGH);
   digitalWrite(blueLed2, HIGH);
   digitalWrite(redLed2, LOW);
  digitalWrite(redLed1, LOW);//
  delay(300);
  tone(4,800,300); //D
  digitalWrite(redLed2, HIGH);
   digitalWrite(redLed1, HIGH);
  digitalWrite(blueLed1, LOW);
  digitalWrite(blueLed2, LOW);
  delay(300);
  tone(4,650,300); //
   digitalWrite(blueLed1, HIGH);
   digitalWrite(blueLed2, HIGH);
   digitalWrite(redLed2, LOW);
  digitalWrite(redLed1, LOW);//
  delay(300);
  tone(4,800,300); //D
  digitalWrite(redLed2, HIGH);
  digitalWrite(redLed1, HIGH);
  digitalWrite(blueLed1, LOW);
  digitalWrite(blueLed2, LOW);
  delay(300);
  tone(4,650,300); //
   digitalWrite(blueLed1, HIGH);
   digitalWrite(blueLed2, HIGH);
   digitalWrite(redLed2, LOW);
  digitalWrite(redLed1, LOW);//
  delay(300);
  tone(4,800,300); //D
  digitalWrite(redLed2, HIGH);
  digitalWrite(redLed1, HIGH);
  digitalWrite(blueLed1, LOW);
  digitalWrite(blueLed2, LOW);
  delay(300);
  tone(4,800,300); //
   digitalWrite(blueLed1, HIGH);
   digitalWrite(blueLed2, HIGH);
   digitalWrite(redLed2, LOW);
  digitalWrite(redLed1, LOW);//
  delay(300);

}

and

int brightness = 0;
int brightness2 = 0;

void setup()
{
  pinMode(3, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
}

void loop()
{
  for (brightness = 0; brightness <= 50; brightness += 5) {
    analogWrite(6, brightness);
    delay(150); 
  }
  for (brightness = 50; brightness >= 0; brightness -= 50) {
    analogWrite(6, brightness);
    delay(150); 
  }
  for (brightness2 = 0; brightness2 <= 50; brightness2 += 5) {
    analogWrite(3, brightness2);
    delay(150); 
  }
  for (brightness2 = 50; brightness2 >= 0; brightness2 -= 50) {
    analogWrite(3, brightness2);
    delay(150); 
  }
}

Your use of delay is preventing you from doing this. You will need to adapt to using millis for your delays instead. See the blink without delay example or Robin2's Doing several things at once thread.

I suggest that you get each program working using millis and then combine them.

This may seem obvious but you cannot make two loops or tasks run at the same time. You only have one processor so only one task can be accomplished at once.

So to accomplish what you want you need to run the code for both tasks intermixed is such a way that it seems like both are running at the same time but really the processor is bouncing between the two tasks.

The use of millis is the key to accomplishing this task;

The millis functions counts in 1 millisecond steps.

So you might do something like this:

If millis is 100 higher than the last one, then run the code for task one.
If millis is 120 higher than the last one, then run the code for task two,

and so on...

Hope this helps.

Could someone show me what it means to use millis? I still don't understand.

https://forum.arduino.cc/index.php?topic=223286.0

Here's an example of the 2nd of your codes (the easier) mixed with an LED blink to show the idea of doing multiple things at once. (Compiles, not tested...)

#define LED_TIME        250ul   //mS
#define BRIGHTNESS_TIME 150ul   //mS

const byte pinBright6 = 6;
const byte pinBright3 = 3;

void setup()
{
    pinMode(pinBright3, OUTPUT);
    pinMode(pinBright6, OUTPUT);
    pinMode(LED_BUILTIN, OUTPUT);
    
}//setup

void loop()
{
    BlinkLED();
    Brightness();
    
}//loop

void BlinkLED( void )
{
    unsigned long
        tNow;
    static unsigned long
        tLED=0;
    static bool
        bLED = false;

    //get the current value of the millis counter
    tNow = millis();
    //is it time to change the state of the LED?
    if( tNow - tLED < LED_TIME )
        //no, just return (processor is free to look at other things that might need doing)
        return;

    //yes; save the current counter value for the next compare
    tLED = tNow;

    //and toggle the state of the LED to give blinking effect
    if( bLED == true )
    {
        digitalWrite( LED_BUILTIN, LOW );
        bLED = false;
        
    }//if
    else
    {
        digitalWrite( LED_BUILTIN, HIGH );
        bLED = true;
        
    }//else
    
}//BlinkLED

//brightness state names
#define ST_0        0
#define ST_1        1
//
void Brightness( void )
{
    unsigned long
        tNow;
    static unsigned long
        tBrightness;
    static byte
        brightness=0,
        stateBright = ST_0;

    //same as in BlinkLED: Is it time to twiddle the brightness?
    tNow = millis();
    if( tNow - tBrightness < BRIGHTNESS_TIME )
        //not yet; return so the processor can check other stuff
        return;

    //save count for next time
    tBrightness = tNow;

    //we use static (persistent) variables to track where we left off last time. 
    switch( stateBright )
    {
        case    ST_0:
            if( brightness >= 50 )
            {
                //when max brightness seen, turn off this LED and set up to handle next one
                analogWrite( pinBright6, 0 );
                brightness = 0;
                stateBright = ST_1;
                
            }//if
            else
            {
                //bumping brightness next step
                analogWrite( pinBright6, brightness );
                brightness+=5;
                
            }//else
        break;

        //this just repeats the above but for pinBright3
        case    ST_1:
            if( brightness >= 50 )
            {
                analogWrite( pinBright3, 0 );
                brightness = 0;
                stateBright = ST_0;     //when done this one, we go back to the first LED again
                
            }//if
            else
            {
                analogWrite( pinBright3, brightness );
                brightness+=5;
                
            }//else
            
        break;            
        
    }//switch
        
}//Brightness

Taosti:
Could someone show me what it means to use millis? I still don't understand.

As well as the earlier links you were given this may help Using millis() for timing. A beginners guide

...R

For the second loop you could use a 150ms timer and attach an interrupt. Use a little state machine and counter to execute each iteration of individual brightness loops in the interrupt. Note: Not the entire loop in the interrupt but a single iteration.