Variables that don't make sense

As a side note, to force the compiler to do unsigned long math (not the default integer math) add the UL.


if(millis() - timer1 >= myVariable - 60000ul)

Exactly. Thank you for the help!

If the intention is to always specify the values in integer seconds, I define:

const unsigned long MS_PER_SEC = 1000;
...
int seconds;
...
if(millis() - timer1 >= MS_PER_SEC * seconds)

which has some versatility, for example to get 1.5 seconds:

if(millis() - timer1 >= MS_PER_SEC * 3/2)
1 Like

Thanks for the idea, I'll do it this way, it sounds better.

Remember, however, with this method all the multiplications should precede all the divisions. If not, you get unwanted data truncation.

1 Like

if(millis()>=timer1+1000seconds)
( ... )
while(millis()<timer2+10
1000)
( ... )
voltageraw=analogRead(A2);
( ... )
voltage=(voltageraw/1024.0)*12.5;
( ... )

Whileitmakesnodifferencetothefunctionalityofyourcode,it'softenagoodideatoaddlots ofspacesasitmakesthecodemucheasiertoreadandharderfortyypostocreepin.

Just as a little aside, anyway!

1 Like

It is my personal preference to not use spaces, in my opinion the code is much more readable this way (at least to me (and that's what's important)). I could never understand why people add spaces everywhere, to me it makes everything unreadable.

Yet you double line space your code...

Yes, I like to separate sections of code by either an empty line or two, depending on how much distant the blocks of code are by function. I suppose the explanation is I don't like spaces in formulas and arguments, etc, but I do like to separate the code in separate blocks of code, which is a totally different thing.

Maybetohelpthosewhoaretryingtohelpyouwithyourquestions.

I'll keep in mind for next time

but then someone else who is like me will come here and tell me to delete all the spaces, cuz he can't read a thing. Or someone else will tell me to format it in another way. You can't satisfy everyone

Say their ass is blowing wind.

That may be true, but you can to the extent that there are specific formatting standards that have evolved. Most programmers are happier to stick close to one of those, because it aligns with other code that they have to read. So if you're dissatisfied with one, you can choose another that has been time proven to satisfy more people.

If you doubt me, take a look around the source for a few good Arduino libraries or some other production code.

Then if someone questions your formatting which is really standard, point them to that code and ask why everyone else is doing it that way.

Many IDE's offer automatic formatting, and offer a menu choice of popular formatting styles, for this very purpose.

1 Like

But you don't :wink: There is no clear functional separation to my eye. Everybodyto their own though.

This would be my approach for the global variables and setup().

// timing related
unsigned long timer1 = 0;

// variables for voltage reading
int voltageraw = 0;
float voltage = 0;

// relay status
bool relay = 0;

void setup()
{
  // setup pins
  pinMode(2, OUTPUT);
  pinMode(A2, INPUT);

  // configure serial
  Serial.begin(57600);
  Serial.println("Begin...");
}

void loop()
{
  ...
  ...
}

Some notes:

  1. You will have a hard time when using tools -> auto format as it adds spaces :smiley:
  2. From long ago, a function should basically fit on an A4 paper so it can be read without wading through multiple pages. And that was something like 80 characters wide and 30 or 40 lines high.
2 Likes