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 ![]()
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();
}
}