# What is happening inside this IF

Hey guys so I am currently going through some code I found trying to understand it. Most of it I am grasping but having a hard time with this part.

if(((brightnesswht(time, 9*HOUR+30*MINUTE,                 17*HOUR+30*MINUTE))*100/255) < 10)

Im just trying to understant the value that is being calculated. Specifically the 9hour and the 30minute? Is it saying 9 multiplied by the hour or what.)

We can't know that without seeing the rest of the code. Or at least the parts where brightnesswht, time, HOUR, MINUTE gets declared.

Jtroutt19:
Specifically the 9hour and the 30minute? Is it saying 9 multiplied by the hour or what.)

Yes.

I don’t like that style of programming where values are calculated inside IF statements because it is impossible to check what values the IF statement is actually using.

I much prefer to calculate the values and put the answers into a variable so I can print it before the IF statement uses it. Something like (but not certain I have captured the logic correctly)

timeA = 9*HOUR+30*MINUTE;
timeB = 17*HOUR+30*MINUTE;
brightVal = brightnesswht(time, timeA, timeB) * 100 / 255;

if(brightVal < 10)

…R

Specifically the 9hour and the 30minute? Is it saying 9 multiplied by the hour or what.)

Without the complete context, it is impossible to be certain, but it appears to be calculating a time since midnight as a number of minutes or seconds. HOUR is probably #defined as 60 or 3600 or 3600000, so that 9 times that value is the number of minutes or seconds or milliseconds since midnight when 9:00 rolls around. The second part is adding the 30 minutes worth of minutes/seconds/milliseconds to that amount.

Sorry I am still new to all this didn’t know the rest of the code would be needed. Here is it.

I have changed a bit of it to work more the way I need it to work. Just trying to understand all of it.

#include <Wire.h>
#include <LiquidCrystal.h>

// SainSmart LCD 16x2
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

// Arduino Mega 2560
const int Blue1Pin = 2;
const int Blue2Pin = 3;
const int White3Pin = 11;
const int White4Pin = 12;
const int Night1Pin = 13;
const unsigned long HOUR = 60 * 60;
const unsigned long MINUTE = 60;
const int TARGET_BRIGHTNESSBLU = (255 * 4) / 10;  // Target brightness blue
const int TARGET_BRIGHTNESSWHT = 255 / 10;  // Target Brightness white

void setup()
{
lcd.begin(16, 2);

Wire.begin();

}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}

void loop()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(F("***"));
lcd.setCursor(4,0);

/////  Get time from RTC into RTCHour, RTCMinute, RTCSecond

// Set the register pointer to 0 (Second)
Wire.write((byte)0);
Wire.endTransmission();

//  Read Second, Minute, and Hour

int RTCHour = bcdToDec(Wire.read() & 0b111111); //24 hour time

// write Time to LCD XX:XX:XX
if(RTCHour < 10) {
lcd.print(F("0"));
}
lcd.print(RTCHour);
lcd.print(F(":"));
if(RTCMinute < 10) {
lcd.print(F("0"));
}
lcd.print(RTCMinute);
lcd.print(F(":"));
if(RTCSecond < 10) {
lcd.print(F("0"));
}
lcd.print(RTCSecond);
lcd.setCursor(13,0);
lcd.print(F("***"));
unsigned long time = RTCHour * HOUR + RTCMinute * MINUTE + RTCSecond;  // Time in seconds

// write value within the timeframe to PWM
analogWrite(Blue1Pin,   brightnessblu(time, 7*HOUR,                 19*HOUR));
analogWrite(Blue2Pin,   brightnessblu(time, 7*HOUR+30*MINUTE,                 19*HOUR+30*MINUTE));
analogWrite(White3Pin, brightnesswht(time, 8*HOUR+30*MINUTE,                 16*HOUR+30*MINUTE));
analogWrite(White4Pin, brightnesswht(time, 9*HOUR+30*MINUTE,                 17*HOUR+30*MINUTE));

// write actual value in % to LCD (max. 255 = 100%)
lcd.setCursor(0,1); //White3Pin
if(((brightnesswht(time,  8*HOUR+30*MINUTE,                 16*HOUR+30*MINUTE))*100/255) < 10) {
lcd.print(F("0"));
}
lcd.print((brightnesswht(time,  8*HOUR+30*MINUTE,                 16*HOUR+30*MINUTE))*100/255);
lcd.print(F("%"));

lcd.setCursor(4,1); //Blue1Pin
if(((brightnessblu(time, 7*HOUR,                 19*HOUR))*100/255) < 10) {
lcd.print(F("0"));
}
lcd.print((brightnessblu(time, 7*HOUR,                 19*HOUR))*100/255);
lcd.print(F("%"));

lcd.setCursor(8,1); //Blue2Pin
if(((brightnessblu(time, 7*HOUR+30*MINUTE,                 19*HOUR+30*MINUTE))*100/255) < 10) {
lcd.print(F("0"));
}
lcd.print((brightnessblu(time, 7*HOUR+30*MINUTE,                 19*HOUR+30*MINUTE))*100/255);
lcd.print(F("%"));

lcd.setCursor(12,1); //White4Pin
if(((brightnesswht(time, 9*HOUR+30*MINUTE,                 17*HOUR+30*MINUTE))*100/255) < 10) {
lcd.print(F("0"));
}
lcd.print((brightnesswht(time, 9*HOUR+30*MINUTE,                 17*HOUR+30*MINUTE))*100/255);
lcd.print(F("%"));

delay(1000);
}

{
//  Mid day, light is at maximum brightness
return TARGET_BRIGHTNESSWHT;

// Dawn:  fade up the light
{
unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time
return TARGET_BRIGHTNESSWHT * seconds / (HOUR);  // Fade up based on portion of interval completed.
}

// Evening: Fade down the light
{
unsigned long seconds = (fadeDownStart + (HOUR)) - time;  // Number of seconds remaining in the fade time
return TARGET_BRIGHTNESSWHT * seconds / (HOUR);  // Fade down based on portion of interval left.
}

// The remaining times are night and the lights is off
return 0;  // Shouldn't get here
}

{
//  Mid day, light is at maximum brightness
return TARGET_BRIGHTNESSBLU;

// Dawn:  fade up the light
{
unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time
return TARGET_BRIGHTNESSBLU * seconds / (HOUR);  // Fade up based on portion of interval completed.
}

// Evening: Fade down the light
{
unsigned long seconds = (fadeDownStart + (HOUR)) - time;  // Number of seconds remaining in the fade time
return TARGET_BRIGHTNESSBLU * seconds / (HOUR);  // Fade down based on portion of interval left.
}

// The remaining times are night and the lights is off
return 0;  // Shouldn't get here
}
[/code\

Robin2:
Yes.

I don’t like that style of programming where values are calculated inside IF statements because it is impossible to check what values the IF statement is actually using.

I much prefer to calculate the values and put the answers into a variable so I can print it before the IF statement uses it. Something like (but not certain I have captured the logic correctly)

timeA = 9*HOUR+30*MINUTE;

timeB = 17HOUR+30MINUTE;
brightVal = brightnesswht(time, timeA, timeB) * 100 / 255;

if(brightVal < 10)

...R

Agree, and such programming style used to be subject of "obfuscated code " contests.
Fun, but if written for profit by employees it was also abused as “job security” by some.
As a software QA I met few of such characters. Not much changed over the years - they did not like my
direct style then, but I was being paid to be direct, now I just catch hell.

There is yet another negative in writing in such style - it hides the purpose . as demonstrated here , of the code / statement because it is almost impossible to document with a comment. Hence another “job security”.
Cheers

232:
As a software QA

Did you ever have a real job in programming?
I only ask, because when I see junk like this and this, I wonder about your level of experience and particular expertise.

what does the 9hour and the 30minute mean? Is it just a way of saying 9:30? so if I wanted to say 10:30 it would be 10hour + 30minute? Whats the purpose of the *?

Whats the purpose of the *?

Multiply (in this instance)

OK so from the first post it would be like writing it like this if it was 8:30am correct?

(8:30am, 972, 1036)*100/255>10

And then what does the comma mean that seperates 972 and 1036? I know this part of the code is dealing with displaying the percentage of brightness on the LCD. At least I think it is.

Sorry these may seem like simple questions but I am having trouble with understanding.

Look at the definition of the brightnesswht function:

This tells you that it returns a byte and takes three arguments. To call the function you pass three arguments separated by commas, which will be the values of time, fadeUpStart and fadeDownStart. So with the call

brightnesswht(time, 9*HOUR+30*MINUTE,17*HOUR+30*MINUTE)

time = time
fadeUpStart = 9HOUR+30MINUTE = 9 * 60 * 60 + 30 * 60
fadeDownStart = 17HOUR+30MINUTE = 17 * 60 * 60 + 30 * 60

It might be confusing you because of the complicated syntax due to taking the value returned by a function, performing math on it and comparing it to a value all in one statement. Think of the value returned by brightnesswht as a single variable and it might be a bit more clear.

If it was written as in #2 is would have been more clear.

Yes it totally makes sense now thank you. I feel like that I should of been able to figure that out and now that I know it seems so simple. Simple things seem to be the hardest for me...... Thank you again. Maybe I will try redoing it in a simple way. Like post #2 just to see if I am able. Be good practice I guess. Thank you again!

Hello again,

This time I am trying to understand how the length of the fade time is calculated for both fade up and fade down. I do not see it anywhere else except for the very bottom of the code. I’ve changed a few more things to suit my particular needs. I am going to try and rewrite the logic for the fading but I want to understand how this is being done before I even attempt that.

{
//  Mid day, light is at maximum brightness
return TARGET_BRIGHTNESSWHT;

// Dawn:  fade up the light
{
unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time
return TARGET_BRIGHTNESSWHT * seconds / (HOUR);  // Fade up based on portion of interval completed.
}

// Evening: Fade down the light
{
unsigned long seconds = (fadeDownStart + (HOUR)) - time;  // Number of seconds remaining in the fade time
return TARGET_BRIGHTNESSWHT * seconds / (HOUR);  // Fade down based on portion of interval left.
}

// The remaining times are night and the lights is off
return 0;  // Shouldn't get here
}

{
//  Mid day, light is at maximum brightness
return TARGET_BRIGHTNESSBLU;

// Dawn:  fade up the light
{
unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time
return TARGET_BRIGHTNESSBLU * seconds / (HOUR);  // Fade up based on portion of interval completed.
}

// Evening: Fade down the light
{
unsigned long seconds = (fadeDownStart + (HOUR)) - time;  // Number of seconds remaining in the fade time
return TARGET_BRIGHTNESSBLU * seconds / (HOUR);  // Fade down based on portion of interval left.
}

// The remaining times are night and the lights is off
return 0;  // Shouldn't get here
}

Here the the entire thing in case it is needed.

#include <Wire.h>
#include <LiquidCrystal.h>
#include "RTClib.h"

RTC_DS1307 rtc;

// SainSmart LCD 16x2
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

// Arduino Uno
const int Blue1Pin = 2;
const int White3Pin = 11;
const int Night1Pin = 13;
const unsigned long HOUR = 60 * 60;
const unsigned long MINUTE = 60;
const int TARGET_BRIGHTNESSBLU = (255 * 4) / 10;  // Target brightness blue
const int TARGET_BRIGHTNESSWHT = 255 / 10;  // Target Brightness white

void setup()
{
lcd.begin(16, 2);

Wire.begin();

rtc.begin();

//rtc.adjust(DateTime(2018, 5, 16, 12, 30, 0));

}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );

}

void loop()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(F("***"));
lcd.setCursor(4,0);

/////  Get time from RTC into RTCHour, RTCMinute, RTCSecond

// Set the register pointer to 0 (Second)
Wire.write((byte)0);
Wire.endTransmission();

//  Read Second, Minute, and Hour

int RTCHour = bcdToDec(Wire.read() & 0b111111); //24 hour time

// write Time to LCD XX:XX:XX
if(RTCHour < 10) {
lcd.print(F("0"));
}
lcd.print(RTCHour);
lcd.print(F(":"));
if(RTCMinute < 10) {
lcd.print(F("0"));
}
lcd.print(RTCMinute);
lcd.print(F(":"));
if(RTCSecond < 10) {
lcd.print(F("0"));
}
lcd.print(RTCSecond);
lcd.setCursor(13,0);
lcd.print(F("***"));
unsigned long time = RTCHour * HOUR + RTCMinute * MINUTE + RTCSecond;  // Time in seconds

// write value within the timeframe to PWM
analogWrite(Blue1Pin,   brightnessblu(time, 7*HOUR,                 19*HOUR));

analogWrite(White3Pin, brightnesswht(time, 8*HOUR+30*MINUTE,                 16*HOUR+30*MINUTE));

// write actual value in % to LCD (max. 255 = 100%)
lcd.setCursor(0,1); //White3Pin
lcd.print(F("Wht:"));
if(((brightnesswht(time,  8*HOUR+30*MINUTE,                 16*HOUR+30*MINUTE))*100/255) < 10) {
lcd.print(F("0"));
}
lcd.print((brightnesswht(time,  8*HOUR+30*MINUTE,                 16*HOUR+30*MINUTE))*100/255);
lcd.print(F("%"));

lcd.setCursor(8,1); //Blue1Pin
lcd.print(F("Blu:"));
if(((brightnessblu(time, 7*HOUR,                 19*HOUR))*100/255) < 10) {
lcd.print(F("0"));
}
lcd.print((brightnessblu(time, 7*HOUR,                 19*HOUR))*100/255);
lcd.print(F("%"));

delay(1000);
}

{
//  Mid day, light is at maximum brightness
return TARGET_BRIGHTNESSWHT;

// Dawn:  fade up the light
{
unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time
return TARGET_BRIGHTNESSWHT * seconds / (HOUR);  // Fade up based on portion of interval completed.
}

// Evening: Fade down the light
{
unsigned long seconds = (fadeDownStart + (HOUR)) - time;  // Number of seconds remaining in the fade time
return TARGET_BRIGHTNESSWHT * seconds / (HOUR);  // Fade down based on portion of interval left.
}

// The remaining times are night and the lights is off
return 0;  // Shouldn't get here
}

{
//  Mid day, light is at maximum brightness
return TARGET_BRIGHTNESSBLU;

// Dawn:  fade up the light
{
unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time
return TARGET_BRIGHTNESSBLU * seconds / (HOUR);  // Fade up based on portion of interval completed.
}

// Evening: Fade down the light
{
unsigned long seconds = (fadeDownStart + (HOUR)) - time;  // Number of seconds remaining in the fade time
return TARGET_BRIGHTNESSBLU * seconds / (HOUR);  // Fade down based on portion of interval left.
}

// The remaining times are night and the lights is off
return 0;  // Shouldn't get here
}

Do not start a new thread on the same topic. Continue with the previous instead.

ok I am sorry I was questioning if to since it was a different part. I apologize.

So in your other thread it was explained what you’re actually passing to those functions; each of the arguments contains the number of seconds since midnight.

{

checks if the current time (time argument) is between the specified fadeUpStart and specified fadeUpStart plus one hour.

If so, it calculates the time difference in seconds between the current time and the fadeUpStart using

unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time

and calculates the ratio of the difference compared to the number of seconds in an hour

seconds / (HOUR)

So this will give you values from 0/3600 … 3600/3600 which are multiplied by TARGET_BRIGHTNESSWHT using below

TARGET_BRIGHTNESSBLU * seconds / (HOUR);

The result is a number between 0 and 25 which is returned back to the calling function.

Now you need to know / remember that the processor does this calculation in the given formula with whole numbers (integers), not fractions. And that in maths, multiplication takes precedence over division.

So multiply TARGET_BRIGHTNESSWHT by 0 … 3600 first will give you values from 0 to 90000 and those are divided by 3600 giving you a number range from 0 to 25. If you fill in the numbers, you will see that every 144 seconds the returned value will be 1 higher.

So this would be the reason why the gentleman said earlier that he doesn't like calculating values inside of if statements? Its confusing as hell.

I am having a really rough time with all of this. Sorry if my questions seem dumb.

I am having a really rough time with all of this

I suspect that is because you skipped working through the introductory tutorials, where you learn the programming language and how to do simple things, like blink an LED without using delay, etc.

Basically, learn how to walk before trying to run.

Fair enough. So what I should go back and work through the using millis() post? lol.

I know I jumped in to it. But I have a hard time learning anything without some structure. And I do not know enough to structure my own learning when it comes to coding.

I would love to learn python as well. But have no clue where to start or where to go once started.