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?

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);
}
-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
    Some language may allow you to write that, but not C++, which is the standard language for Arduino. – Edgar Bonet Feb 10 '17 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.