checking two variables at once (SOLVED)

Hi all,

I've googled an searched the forum but couldn't find an answer for my problem.

In a program i'm writing to make sunset and sinrise posiblle for my tropical aquarium I stumbled upon a problem I can't solve. I have two variables, wich represent the brightness of my two fluorescent lights. These variables have a value between 170 and 0. Because I can change the brightness of the lights manualy and individualy during the day, the values of the variables are not the same at the time the sunset (dimming) routine kicks in. Let's say TL1brightness = 80, and TL2brightness = 170 at the beginning of the dimming routine. I would like that TL2brightness decreases until it has the same value as TL1brightness, and then they should go down together until they are both 0.

I tried the following:

void SunSet() { // TL's dimmen vanaf brigntness naar 0 while(TL1brightness > 0 && TL2brightness > 0) { TL2brightness = TL2brightness - fadeAmount; analogWrite(TL2, TL2brightness); TL1brightness = TL1brightness - fadeAmount; analogWrite(TL1, TL1brightness); delay(100); } }

FadeAmount is the stepsize, assume that it is 1 for now. The result of the above code is that TL1brightness reduces to 0, and TL2brightness reduces to 90 at the same time, wich is not what i'm after ofcouse. :P

I imagine the answer is simple, but I just don't see it. Maybe there is someone out there that can put me on the right track.

Thanx for now.

You should have an OR in your while loop not an AND, so continue looping until both are zero.

You may also want to make sure the value does not drop below 0 when you leave the loop.

Thanx, That solved part of my problem.
The countdown works, but I want TL1brightness to start decreasing when TL2brightness has the same value. So TL1brightness stays at 80 until TL2brightness has reached the same value.
(the other way arround should also be possible.)

This is what i have so far.

void SunSet()
{
// TL’s dimmen vanaf brigntness naar 0
while(TL1brightness > 0 || TL2brightness > 0)
{
TL2brightness = TL2brightness - fadeAmount;
if (TL2brightness < 0) {
TL2brightness = 0;}
analogWrite(TL2, TL2brightness);
TL1brightness = TL1brightness - fadeAmount;
if (TL1brightness < 0) {
TL1brightness = 0;}
analogWrite(TL1, TL1brightness);
delay(500);
}
}

More ideas? :stuck_out_tongue:

So write an if-statement that only decrements when you want it to.

Figure out how you would do it manually then turn that into code.

Thanx!

I've solved my problem. It was simple. 8) Stupid me!