Hi. In a nut shell, this part of my code is for the led lighting of a fish tank with four channels running off of the PWM channels. I have a statement that will call void l_lsramp() when it is time to start ramping the lights up to a specified brightness on all four channels. However, it only adjusts one channel at a time and I can't seem to see why. They should trigger all relevant 'ifs' at the same time and therefore add the value of ch1pwmincr, or ch2pwmincr, etc at the same time but it sends ch1 up to the desired level, then starts on ch2 and so on.
if (lightEditing == 0)
{
if ((tm.Hour == lunartolowsunH) && (tm.Minute == lunartolowsunM) && (tm.Second == 0))
{
l_lsramp();
}
}
void l_lsramp() // Lunar to low sun
{
currentColor.ch1 == ledch1pin; // Read current colors
currentColor.ch2 == ledch2pin; // DONT KNOW IF THIS NEEDS TO BE HERE??
currentColor.ch3 == ledch3pin;
currentColor.ch4 == ledch4pin;
// last color is the starting point of the fade
lastColor.ch1 = currentColor.ch1;
lastColor.ch2 = currentColor.ch2;
lastColor.ch3 = currentColor.ch3;
lastColor.ch4 = currentColor.ch4;
// target color is low sun
targetColor.ch1 = lightLowSun.ch1;
targetColor.ch2 = lightLowSun.ch2;
targetColor.ch3 = lightLowSun.ch3;
targetColor.ch4 = lightLowSun.ch4;
// calculate how long to run the fade for
int fadeHours = lunartolowsundurH;
int fadeMins = lunartolowsundurM;
fadeDurationSeconds = ((fadeHours*60*60)+(fadeMins*60));
fadeInProgress = true;
currentLightMode=4;
previousMillis = millis();
}
//////// R A M P T R I G G E R S ////////
if(targetColor.ch1 != lastColor.ch1)
{
// Get the change per second for the current color to the target color
float ch1pwmincr = (float)(targetColor.ch1 - lastColor.ch1) / (float)fadeDurationSeconds;
Serial.print("ch1pwmincr - ");
Serial.println(ch1pwmincr);
Serial.print("currentColor.ch1 - ");
Serial.println(currentColor.ch1);
if(ch1pwmincr > 0) // If the change is positive
{
if(currentColor.ch1 < targetColor.ch1)
{
if ((millis() - previousMillis) > 1000)
{
currentColor.ch1 = currentColor.ch1 + ch1pwmincr; // Add the increment amount to the corrent color
analogWrite(ledch1pin, currentColor.ch1);
EEPROM.write(43, currentColor.ch1);
previousMillis = millis();
}
}
}
else // If the change is negative
{
if (ch1pwmincr < 0)
{
if(currentColor.ch1 > targetColor.ch1)
{
if ((millis() - previousMillis) > 1000)
{
// Decrease the current color
currentColor.ch1 = currentColor.ch1 + ch1pwmincr;
analogWrite(ledch1pin, currentColor.ch1);
EEPROM.write(43, currentColor.ch1);
previousMillis = millis();
}
}
}
}
}
if(targetColor.ch2 != lastColor.ch2)
{
// Get the change per second for the current color to the target color
float ch2pwmincr = (float)(targetColor.ch2 - lastColor.ch2) / (float)fadeDurationSeconds;
Serial.print("ch2pwmincr - ");
Serial.println(ch2pwmincr);
Serial.print("currentColor.ch2 - ");
Serial.println(currentColor.ch2);
if(ch2pwmincr > 0) // If the change is positive
{
if(currentColor.ch2 < targetColor.ch2)
{
if ((millis() - previousMillis) > 1000)
{
currentColor.ch2 = currentColor.ch2 + ch2pwmincr; // Add the increment amount to the corrent color
analogWrite(ledch2pin, currentColor.ch2);
EEPROM.write(44, currentColor.ch2);
previousMillis = millis();
}
}
}
else // If the change is negative
{
if (ch2pwmincr < 0)
{
if(currentColor.ch2 > targetColor.ch2)
{
if ((millis() - previousMillis) > 1000)
{
// Decrease the current color
currentColor.ch2 = currentColor.ch2 + ch2pwmincr;
analogWrite(ledch2pin, currentColor.ch2);
EEPROM.write(44, currentColor.ch2);
previousMillis = millis();
}
}
}
}
}
if(targetColor.ch3 != lastColor.ch3)
{
// Get the change per second for the current color to the target color
float ch3pwmincr = (float)(targetColor.ch3 - lastColor.ch3) / (float)fadeDurationSeconds;
Serial.print("ch3pwmincr - ");
Serial.println(ch3pwmincr);
Serial.print("currentColor.ch3 - ");
Serial.println(currentColor.ch3);
if(ch3pwmincr > 0) // If the change is positive
{
if(currentColor.ch3 < targetColor.ch3)
{
if ((millis() - previousMillis) > 1000)
{
currentColor.ch3 = currentColor.ch3 + ch3pwmincr; // Add the increment amount to the corrent color
analogWrite(ledch3pin, currentColor.ch3);
EEPROM.write(45, currentColor.ch3);
previousMillis = millis();
}
}
}
else // If the change is negative
{
if (ch3pwmincr < 0)
{
if(currentColor.ch3 > targetColor.ch3)
{
if ((millis() - previousMillis) > 1000)
{
// Decrease the current color
currentColor.ch3 = currentColor.ch3 + ch3pwmincr;
analogWrite(ledch3pin, currentColor.ch3);
EEPROM.write(45, currentColor.ch3);
previousMillis = millis();
}
}
}
}
}
if(targetColor.ch4 != lastColor.ch4)
{
// Get the change per second for the current color to the target color
float ch4pwmincr = (float)(targetColor.ch4 - lastColor.ch4) / (float)fadeDurationSeconds;
Serial.print("ch4pwmincr - ");
Serial.println(ch4pwmincr);
Serial.print("currentColor.ch4 - ");
Serial.println(currentColor.ch4);
if(ch4pwmincr > 0) // If the change is positive
{
if(currentColor.ch4 < targetColor.ch4)
{
if ((millis() - previousMillis) > 1000)
{
currentColor.ch4 = currentColor.ch4 + ch4pwmincr; // Add the increment amount to the corrent color
analogWrite(ledch4pin, currentColor.ch4);
EEPROM.write(46, currentColor.ch4);
previousMillis = millis();
}
}
}
else // If the change is negative
{
if (ch4pwmincr < 0)
{
if(currentColor.ch4 > targetColor.ch4)
{
if ((millis() - previousMillis) > 1000)
{
// Decrease the current color
currentColor.ch4 = currentColor.ch4 + ch4pwmincr;
analogWrite(ledch4pin, currentColor.ch4);
EEPROM.write(46, currentColor.ch4);
previousMillis = millis();
}
}
}
}
}
}