0

My code is:

if ((minute == 31) && (hour == 4, 6, 9, 11)) 
{
  digitalWrite(8, HIGH);
}

I want to turn on the LEDs when it is exactly

  • 04:31,
  • 06:31,
  • 09:31,
  • 11:31

However, the code above doesn't work.
Might be the multiple values the reason for this?

2 Answers 2

1

Yes, the list of values 4, 6, 9, 11 does not mean what you think it does.

if ((minute == 31) &&  ((hour == 4) || (hour == 6) || (hour == 9) || (hour == 11))) {
    digitalWrite(8, HIGH);
}
0
-1

Optionally, you could store the values in an array and go across all of them with some code like this:

int hours = [4,6,9,11];

function isInArray(inputValue, array) {
    boolean isIn = false;

    for ( int i = 0; I < array.length; i++) {
        // "isIn |= statement" is shorthand for "isIn = (isIn || statement)"
        isIn |= (array[I] == inputValue);
    }

    return isIn;
}

if (isInArray(variable, hours)) {
    //do whatever
}

//might be able to do
if (isInArray(variable, [4,6,9,11])) {
    //do whatever
}
1
  • 1
    Some language may allow you to write that, but not C++, which is the standard language for Arduino. Commented Feb 10, 2017 at 21:14

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.