Problem with switching between loops with toggle switch

I have two loops that I want to switch between using an on-off toggle switch. Right now if the toggle switch is set to ON, loop1 runs only - perfect. If I switch it to OFF, loop1 continues running but goes in slow-mo and loop2 runs at normal speed.

Is there something I can add to completely stop loop1 when switched off? I feel like I'm so close :confused:

const int BigCloud =  2; 
const int MedCloud_1 =  3;  
const int SmallCloud_1 = 4;
const int MedCloud_2 = 5;
const int SmallCloud_2 = 6;
const int Outlet = 7;
int pinSwitch = 22;
int random1 = 0;
int random2 = 0;
int random3 = 0;
#define whitePin 8
#define redPin 9
#define greenPin 10
#define bluePin 11
#define daytimePin A0 //analog input for potentiometer.
float dayTime=0;
long currentTime=0;
double x1Blue = 0;
double y1Blue = 0;
double x2Blue = 0;
double y2Blue = 0;
double slopeBlue = 0;
double interceptBlue = 0;
double x1Red = 0;
double y1Red = 0;
double x2Red = 0;
double y2Red = 0;
double slopeRed = 0;
double interceptRed = 0;
long transitionTime=86400000; //this variable stores the cycle time in milliseconds. It determines how long your day cycle is (good for testing a full day in say 10 sec!)
int whiteVal=0; //These values are the 8 bit (0-255) values PWM values to write to your MOSFETS/Lights
int redVal=0;
int greenVal=0;
int blueVal=0;
float potTime = 0;
double adjustTime = 0; //variable for changes to the potentiometer

void setup()
{
pinMode(pinSwitch, INPUT_PULLUP);
pinMode(whitePin, OUTPUT); //set pin outputss.
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
analogWrite(bluePin, 0);
analogWrite(whitePin, 0);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
pinMode(BigCloud, OUTPUT);
pinMode(MedCloud_1, OUTPUT);
pinMode(SmallCloud_1, OUTPUT);
pinMode(MedCloud_2, OUTPUT);
pinMode(SmallCloud_2, OUTPUT);
pinMode(Outlet, OUTPUT);
Serial.begin (9600); // 9600 bps
}
void loop()
{
	if (digitalRead(pinSwitch) == HIGH)
	{ loop1();}
	else
	{ loop2();}
}  
void loop1()
{
	if (dayTime -10 > analogRead(daytimePin) || dayTime +10 < analogRead(daytimePin)) 
	{ //this if statement monitors the state of the potentiometer "daytime". There is a 10 count cushion on each side because I found that less could make the colors jump randomly.
	// spontaneously when the potentiometer appeared to be stuck between values.
		dayTime = analogRead(daytimePin);
		potTime = dayTime / 511.5; //turn the potentiometer value into a ratio of the total.
		adjustTime = potTime*transitionTime; //multiply the tranisition time by the ratio to get the new current time
		currentTime = adjustTime;
	}
//reset
  // Night Time!
   if (currentTime <= .15 * transitionTime)
	{ //set the values for night time moon lights
		analogWrite(bluePin, 7);
		analogWrite(whitePin, 2);
		analogWrite(redPin, 2);
		analogWrite(greenPin, 0);
	}
   // Sunrise! Purple-Orange-Yellow
	if (currentTime >= .15 * transitionTime && currentTime <= .225 * transitionTime)
	{
        blueVal = Fade(.15 * transitionTime, .225 * transitionTime, 10, 200, currentTime);
        redVal = Fade(.15 * transitionTime, .225 * transitionTime, 2, 100, currentTime);
        analogWrite(bluePin, blueVal);
        analogWrite(redPin, redVal);
	}
	if (currentTime >= .225 * transitionTime && currentTime <= .3 * transitionTime)
	{
        blueVal = Fade(.225 * transitionTime, .30 * transitionTime, 200, 0, currentTime);
        greenVal = Fade(.225 * transitionTime, .30 * transitionTime, 0, 104, currentTime);
        redVal = Fade(.225 * transitionTime, .30 * transitionTime, 100, 255, currentTime);
        analogWrite(bluePin, blueVal);
        analogWrite(greenPin, greenVal);
        analogWrite(redPin, redVal);
	}
	if (currentTime >= .15 * transitionTime && currentTime <= .3 * transitionTime)
	{
		whiteVal= Fade(.15 * transitionTime, .30 * transitionTime, 10, 200, currentTime);
        analogWrite(whitePin, whiteVal);
	}
   //Full Lights!
    if (currentTime >= .3 * transitionTime && currentTime <= .4 * transitionTime)
	{
        whiteVal= Fade(.30 * transitionTime, .40 * transitionTime, 200, 255, currentTime);
        greenVal = Fade(.30 * transitionTime, .40 * transitionTime, 104, 220, currentTime);
        blueVal = Fade(.3 * transitionTime, .40 * transitionTime, 0, 100, currentTime);
        analogWrite(whitePin, whiteVal);
        analogWrite(greenPin, greenVal);
        analogWrite(bluePin, blueVal);
	}
    //Evening Lights! Yellow
    if (currentTime >= .7 * transitionTime && currentTime <= .775 * transitionTime)
	{
		whiteVal= Fade(.7 * transitionTime, .775 * transitionTime, 255, 180, currentTime);
		blueVal = Fade(.7 * transitionTime, .775 * transitionTime, 100, 0, currentTime);
		greenVal = Fade(.7 * transitionTime, .775 * transitionTime, 220, 150, currentTime);
		analogWrite(whitePin, whiteVal);
		analogWrite(greenPin, greenVal);
		analogWrite(bluePin, blueVal);
	}
     if (currentTime >= .775 * transitionTime && currentTime <= .85 * transitionTime)
	{
		greenVal = Fade(.775 * transitionTime, .85 * transitionTime, 150, 100, currentTime);
		whiteVal= Fade(.775 * transitionTime, .85 * transitionTime, 180, 60, currentTime);
		analogWrite(greenPin, greenVal);
		analogWrite(whitePin, whiteVal);
	}
    if (currentTime >= .85 * transitionTime && currentTime <= .9 * transitionTime)
	{
		greenVal = Fade(.85 * transitionTime, .9 * transitionTime, 100, 0, currentTime);
		whiteVal= Fade(.85 * transitionTime, .9 * transitionTime, 60, 2, currentTime);
		blueVal = Fade(.85 * transitionTime, .9 * transitionTime, 0, 7, currentTime);
		redVal = Fade(.85 * transitionTime, .9 * transitionTime, 255, 2, currentTime);
		analogWrite(greenPin, greenVal);
		analogWrite(whitePin, whiteVal);
		analogWrite(bluePin, blueVal);
		analogWrite(redPin, redVal);
	}    
    delay(20);
    currentTime = currentTime + 20; //Every time the loop executes, add 20 ms to current time (from delay below). This is not a terribly precise means of keeping time.
  if (currentTime > transitionTime)
  {
    currentTime = 0; //Reset after cycle
  }
}
double Fade(double x1, double x2, double y1, double y2, double time)
{
double slope = (y2-y1)/(x2-x1);
double intercept = y1-slope*x1;
double result = slope*time + intercept;
return result;
}
void loop2() 
{
  digitalWrite(Outlet, HIGH);
  rolling();
}

void rolling()
{
	// a simple method where we go through every LED with 1/10 chance
	// of being turned on, up to 10 times, with a random delay wbetween each time
	random1 = random(2,10);
	for(int r=0;r<random1;r++)
 {
    //iterate through every LED
    for(int i=0;i<2;i++)
    {
      random2 = random(0,100);
      if(random2>90)
      {
        //leds[i] = CHSV( 0, 0, 255);
        random3 = random(0,10);
        switch(random3)
        {
           case 0:
           {
            digitalWrite(BigCloud, HIGH);
           }
           break;
           case 1:
           {
            digitalWrite(MedCloud_1, HIGH);
           }
           break;
           case 2:
           {
            digitalWrite(SmallCloud_1, HIGH);
           }
           break;
           case 3:
           {
            digitalWrite(MedCloud_2, HIGH);
           }
           break;
           case 4:
           {
            digitalWrite(SmallCloud_2, HIGH);
           }
           break;
           case 6:
           {
            digitalWrite(BigCloud, HIGH); 
            digitalWrite(MedCloud_1, HIGH);
           }
           break;
           case 7:
           {
            digitalWrite(BigCloud, HIGH); 
            digitalWrite(SmallCloud_1, HIGH);
           }
           break;
           case 8:
           {
            digitalWrite(BigCloud, HIGH); 
            digitalWrite(MedCloud_2, HIGH);
           }
           break;
           case 9:
           {
            digitalWrite(BigCloud, HIGH); 
            digitalWrite(SmallCloud_2, HIGH);
           }
           break;
           case 10:
           {
            digitalWrite(MedCloud_1, HIGH); 
            digitalWrite(SmallCloud_1, HIGH);
           }
           break;
        }
        Serial.println (random2, DEC);
      }
      else
      {
        //dont need reset as we're blacking out other LEDs her 
        //leds[i] = CHSV(0,0,0);
        digitalWrite(BigCloud, LOW);
        digitalWrite(MedCloud_1, LOW);
        digitalWrite(SmallCloud_1, LOW);
        digitalWrite(MedCloud_2, LOW);
        digitalWrite(SmallCloud_2, LOW);
      }
    }
    //FastLED.show();
    //delay(random(5,100));
    delay(random(5,400));
    //reset();
 }
}

Have you got a pullup or pulldown resistor on pinSwitch ?

Consider using
  pinMode(pinSwitch, INPUT_PULLUP);to activate the built in pullup resistor and change the wiring so that the pin is taken LOW when the switch is closed. This will keep the pin at a known state at all times.

Hmm good question, I'm not totally sure...I think pullup? I added a 1k resistor to the 5v pin that goes to power of the switch, the other side goes to pin 22.

How would I wire it so the pin is taken LOW when closed?

Sorry if that is a silly question, I'm just starting out learning arduino still.

Edit;
Looks like is remove the resistor and send the switch to ground and pin 22 instead of of 5v with resistor and pin 22. I will give that a shot tomorrow and report back.

Thank you!

  void loop()
  {
  if (digitalRead(pinSwitch) == HIGH) {
  loop1();
    } else {
  loop2();
    }
  }

Is
there
some
not-obvious
reason
for
this
crappy
indenting?

Hah, copy and pasting between too many places/IMing /etc. I will clean it up once it's working!

I will clean it up once it's working!

Clean it up now. It may help debugging and/or understanding the code. All it takes is Ctrl/T in the IDE as a first step. Even better put each { and } on its own line to make the code blocks more obvious.

Ok, I will clean it up today!

UKHeliBob:
Have you got a pullup or pulldown resistor on pinSwitch ?

Consider using
  pinMode(pinSwitch, INPUT_PULLUP);to activate the built in pullup resistor and change the wiring so that the pin is taken LOW when the switch is closed. This will keep the pin at a known state at all times.

I tried doing this and the same thing happens - loop2 seems to work fine, but loop 1 gets "stuck" in slow motion.

OFF - loop1 works fine, loop2 is off
ON - loop1 is stuck in slow mo and loop2 is on.
Go back to OFF and loop1 resumes in normal speed and loop2 is off.

I wonder if I edit loop2 to reduce the brightness/turn off of the LEDs used in loop1 if that would be a workaround... I don't necessarily need it to resume where it left off on loop1, just need it to turn off.

Edit:
Cleaned up code a bit so it's much more consistent and hopefully not as rage inducing. It was a sloppy mess before :frowning:

Put a Serial.print() at the start of the loop1() and loop2() functions indicating which of them is starting and when.

I ended up turning the pins off in the opposite loops and it works!! I noticed I was able to get the lightning stuck on if I switched it during a strike and my outlet was stuck on all the time too. Since the day-night cycle is always on that is also why it was always getting stuck.

WOOT!