user58508: New to Arduino. I am trying to use a single button to light and turn off a single LED. I followed an online tutorial (youtube video) and changed the code just a bit. I know it is for two LEDs but I tried it for one. I have no idea why it is not working. I don't see why it doesn't make sense but it just does not work.
Here is the code:

float LengthPush = 0; // LengthPush is the amount of time the button has been pressed.

int ShortPush = 100; // 100 ms push. Supposed to turn on the LED.
int LPush = 1000; // 1000 ms push. Supposed to turn it off.

int ButtonPin = 2;
int RLED = 13;

void setup() {
  // put your setup code here, to run once:
  pinMode(ButtonPin, INPUT_PULLUP);
  pinMode(RLED, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  while (digitalRead(ButtonPin) == LOW) {
    delay(100);
    LengthPush = LengthPush + 100;

    Serial.print("ms = ");
    Serial.println(LengthPush);
  }

  if (LengthPush >= LPush) {
    digitalWrite(RLED, LOW);
  } else if (ShortPush <= LengthPush < LPush) {
    digitalWrite(RLED, HIGH);
  }
  LengthPush = 0;
}


user42015: I think your problem lies in the else-if-statement, where you try to compare 3 different values. While this doesn't thrown a compiling error in my Arduino IDE, it will probably not do, what you are trying to achieve.
Instead you need to compare LengthPush with one value and then with the other. You can even do this inside the same if statement:
if(ShortPush <= LengthPush && LengthPush < LPush)

The && is the boolean AND operator, which means, that the if statement will be executed, if both statements around it will evaluate as true.

I'm not sure, what actually happens with your original code, since it doesn't throw a compilation error. It might be (and that's my theory here), that the program will first evaluate ShortPush <= LengthPush and then compare the result to LPush. Since boolean values evaluate as 0 or 1 in integer calculations, that messes up the whole intent of this. Take the case, that LengthPush is really in the required interval. Then the first part will evaluate as true, which means 1 as integer. Then this 1 get's compared to LPush, which is always higher, so the overall result is true. But the same result comes, when LengthPush is smaller than ShortPush.

58508: <p>New to Arduino. I am trying to use a single button to light and turn off a single LED. I followed an <a href="https://www.youtube.com/watch?time_continue=913&amp;v=IsDzxtaZCoI" rel="nofollow noreferrer">online tutorial (youtube video)</a> and changed the code just a bit. I know it is for two LEDs but I tried it for one. I have no idea why it is not working. I don't see why it doesn't make sense but it just does not work.</p>

<p>Here is the code:
</p>

<pre class="lang-cpp prettyprint-override"><code>float LengthPush = 0; // LengthPush is the amount of time the button has been pressed.

int ShortPush = 100; // 100 ms push. Supposed to turn on the LED.
int LPush = 1000; // 1000 ms push. Supposed to turn it off.

int ButtonPin = 2;
int RLED = 13;

void setup() {
  // put your setup code here, to run once:
  pinMode(ButtonPin, INPUT_PULLUP);
  pinMode(RLED, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  while (digitalRead(ButtonPin) == LOW) {
    delay(100);
    LengthPush = LengthPush + 100;

    Serial.print("ms = ");
    Serial.println(LengthPush);
  }

  if (LengthPush &gt;= LPush) {
    digitalWrite(RLED, LOW);
  } else if (ShortPush &lt;= LengthPush &lt; LPush) {
    digitalWrite(RLED, HIGH);
  }
  LengthPush = 0;
}
</code></pre>
 42015: <p>I think your problem lies in the else-if-statement, where you try to compare 3 different values. While this doesn't thrown a compiling error in my Arduino IDE, it will probably not do, what you are trying to achieve.</p>

<p>Instead you need to compare <code>LengthPush</code> with one value and then with the other. You can even do this inside the same if statement:</p>

<pre><code>if(ShortPush &lt;= LengthPush &amp;&amp; LengthPush &lt; LPush)
</code></pre>

<p>The <code>&amp;&amp;</code> is the boolean AND operator, which means, that the if statement will be executed, if both statements around it will evaluate as <code>true</code>.</p>

<hr>

<p>I'm not sure, what actually happens with your original code, since it doesn't throw a compilation error. It might be (and that's my theory here), that the program will first evaluate <code>ShortPush &lt;= LengthPush</code> and then compare the result to <code>LPush</code>. Since boolean values evaluate as <code>0</code> or <code>1</code> in integer calculations, that messes up the whole intent of this. Take the case, that <code>LengthPush</code> is really in the required interval. Then the first part will evaluate as <code>true</code>, which means <code>1</code> as integer. Then this <code>1</code> get's compared to <code>LPush</code>, which is always higher, so the overall result is true. But the same result comes, when <code>LengthPush</code> is smaller than <code>ShortPush</code>.</p>
