Page 1 of 1
Nested if/then/else
Posted: Wed May 29, 2013 6:09 am
by Smotz
Hi -
I want my code to say:
If in feeding mode, my wave should be set to 35%
else, set normally, unless between hours x and y then set slightly lower. The coding doesn't seem to like the nested 'Else' statements..
Code: Select all
if (ReefAngel.DisplayedMenu==FEEDING_MODE) {
ReefAngel.PWM.SetDaylight(35);
ReefAngel.PWM.SetActinic(35);
}
else {
if ( (hour() >= 5) && (hour() < 23) ) // from 5a - 11p
ReefAngel.PWM.SetDaylight( SineMode(35,45,60,false) );
}
else {
////ReefAngel.PWM.SetDaylight(50);
ReefAngel.PWM.SetDaylight( SineMode(35,65,45,false) );
ReefAngel.PWM.SetActinic( ReefCrestMode(50,25,false) ); // ReefCrest at 50% +/- 25% on anti-sync mode
}
Re: Nested if/then/else
Posted: Wed May 29, 2013 6:12 am
by Smotz
I think I got it - too many '{}' 's
hopefully this will work - thoughts?
Code: Select all
if (ReefAngel.DisplayedMenu==FEEDING_MODE) {
ReefAngel.PWM.SetDaylight(35);
ReefAngel.PWM.SetActinic(35);
}
else {
if ( (hour() >= 5) && (hour() < 23) ) // from 5a - 11p
ReefAngel.PWM.SetDaylight( SineMode(35,45,60,false) );
else
////ReefAngel.PWM.SetDaylight(50);
ReefAngel.PWM.SetDaylight( SineMode(35,65,45,false) );
ReefAngel.PWM.SetActinic( ReefCrestMode(50,25,false) ); // ReefCrest at 50% +/- 25% on anti-sync mode
}
Re: Nested if/then/else
Posted: Wed May 29, 2013 6:19 am
by lnevo
You actually have not enough...
Try this
Code: Select all
if (ReefAngel.DisplayedMenu==FEEDING_MODE) {
ReefAngel.PWM.SetDaylight(35);
ReefAngel.PWM.SetActinic(35);
}
else {
if ( (hour() >= 5) && (hour() < 23) ) // from 5a - 11p
ReefAngel.PWM.SetDaylight( SineMode(35,45,60,false) );
else {
////ReefAngel.PWM.SetDaylight(50);
ReefAngel.PWM.SetDaylight( SineMode(35,65,45,false) );
ReefAngel.PWM.SetActinic( ReefCrestMode(50,25,false) ); // ReefCrest at 50% +/- 25% on anti-sync mode
}
}
[/quote]
Re: Nested if/then/else
Posted: Wed May 29, 2013 6:32 am
by Smotz
lnevo wrote:You actually have not enough...
Try this
Code: Select all
if (ReefAngel.DisplayedMenu==FEEDING_MODE) {
ReefAngel.PWM.SetDaylight(35);
ReefAngel.PWM.SetActinic(35);
}
else {
if ( (hour() >= 5) && (hour() < 23) ) // from 5a - 11p
ReefAngel.PWM.SetDaylight( SineMode(35,45,60,false) );
else {
////ReefAngel.PWM.SetDaylight(50);
ReefAngel.PWM.SetDaylight( SineMode(35,65,45,false) );
ReefAngel.PWM.SetActinic( ReefCrestMode(50,25,false) ); // ReefCrest at 50% +/- 25% on anti-sync mode
}
}
[/quote]
thx man - you're the best - couldn't figure out why it wasn't working
Re: Nested if/then/else
Posted: Wed May 29, 2013 6:37 am
by Smotz
Actually - still not working.
Can you confirm my time schedule? It's 9:36 am and my wave is running on the night schedule..?
it is backwards..fixing..
Re: Nested if/then/else
Posted: Wed May 29, 2013 7:06 am
by lnevo
Yeah, looking at your logic... if the hour is greater than 5 and less then 23 go at SineMode 35%.-45%, else 35-65 and Reefcrest at 50...
You can just switch the actions or, you can make the conditional
if ( (hour() < 5) || (hour() >=23) )
Re: Nested if/then/else
Posted: Wed May 29, 2013 7:26 am
by Smotz
Sorry for the basic stuff but can you explain the conditions?
Sent from my SCH-I605 using Tapatalk 4 Beta
Re: Nested if/then/else
Posted: Wed May 29, 2013 7:35 am
by binder
&& - means AND. both sides of the && must be TRUE in order for the whole expression to be true.
if ( (x > 1) && (x < 10) ) -- this means X must be between 1 AND 10 (or a value of 2 - 9) for it to be true.
|| - means OR. one of the sides must be TRUE in order for the whole expression to be true.
if ( (x < 10) || (x > 20) ) -- this means X must be less than 10 (0-9) OR greater than 20 (21 and higher) in order for the expression to be true.
Re: Nested if/then/else
Posted: Wed May 29, 2013 7:38 am
by lnevo
Sorry my code wasn't correct so don't copy directly... I forgot the () for hour... (going to update it now...)
Anyway, you had said that IF the hour is greater then 5 AND the hour is less than 23 ... then be in Night mode which was the opposite of what you wanted... so your choice was to move the actions around... or change to this logic...
The code I posted says this... IF the hour is less than 5 OR the hour is greater than 23 then be in Night mode...
does that help?
Re: Nested if/then/else
Posted: Wed May 29, 2013 7:48 am
by Smotz
Gotcha. At work now but I definitely will have to review my code.
Sent from my SCH-I605 using Tapatalk 4 Beta