Post topics, source code that relate to the Arduino Platform

User avatar
By André van Stijn
#85967 Hey all,

I have a question, I am a starter at Arduino and need some help to simplify my code. Underneath code I will use to turn on and of my leds. I do it with the code underneath but that because I don't have the skills to make a more quick/clean and simplified code. Can someone help me?

Thanks in advance,

André

I own a Wemos D1 R1


###Code

if ((minute == 1)
| (minute == 21)
| (minute == 31)
| (minute == 59)) {
lightup(WordMinEen, Gold);
}
else {
if ((minute == 2)
| (minute == 3 )
| (minute == 4 )
| (minute == 5 )
| (minute == 6 )
| (minute == 7 )
| (minute == 8 )
| (minute == 9 )
| (minute == 10 )
| (minute == 11 )
| (minute == 12 )
| (minute == 13 )
| (minute == 14 )
| (minute == 15 )
| (minute == 16 )
| (minute == 17 )
| (minute == 18 )
| (minute == 19 )
| (minute == 20 )
| (minute == 21 )
| (minute == 22 )
| (minute == 23 )
| (minute == 24 )
| (minute == 25 )
| (minute == 26 )
| (minute == 27 )
| (minute == 28 )
| (minute == 29 )
| (minute == 30 )
| (minute == 32 )
| (minute == 33 )
| (minute == 34 )
| (minute == 35 )
| (minute == 36 )
| (minute == 37 )
| (minute == 38 )
| (minute == 39 )
| (minute == 40 )
| (minute == 41 )
| (minute == 42 )
| (minute == 43 )
| (minute == 44 )
| (minute == 45 )
| (minute == 46 )
| (minute == 47 )
| (minute == 48 )
| (minute == 49 )
| (minute == 50 )
| (minute == 51 )
| (minute == 52 )
| (minute == 53 )
| (minute == 54 )
| (minute == 55 )
| (minute == 56 )
| (minute == 57 )
| (minute == 58 )
| (minute == 00 )
) {
lightup(WordMinEen, Black);
}
User avatar
By ToodleLew
#86105 At first glance, you could simplify this to

Code: Select all  if ((minute == 1) | (minute == 21) | (minute == 31) | (minute == 59))
    lightup(WordMinEen, Gold);
  else
    lightup(WordMinEen, Black);


This assumes that <<minute>> is an integer variable, such as
Code: Select all short minute;

or
Code: Select allint minute;


If <<minute>> is a float or double, as in
Code: Select all double minute;

then the logic gets a little more complicated...

Code: Select all  if ((minute == 1) | (minute == 21) | (minute == 31) | (minute == 59))
    lightup(WordMinEen, Gold);
  else
    if ( (int) minute == minute)
      lightup(WordMinEen, Black);


This ensures that the logic does not call lightup() when minute contains a fractional part, as in (minute == 30.0)

Hope this helps.