logo elektroda
logo elektroda
X
logo elektroda

Why doesn't the Arduino ESP8266 code change the LED state when IF?

maystero 1179 25
Best answers

Why doesn’t my Arduino ESP8266 IR-receiver code toggle the LED when the `if` condition is met?

The LED didn’t change because the code used assignment instead of comparison in the condition, so it should be `if (results.value == 800)` rather than `if (results.value = 800)` [#18390576] Also, the serial output/test line worked only when `serialPrintUint64(results.value)` was used directly, not with the extra `HEX`/`BIN` formatting argument [#18396734][#18396799] In the end, the remaining problem was the IR receive reset: `irrecv.resume();` had to be moved outside the `switch...case`, after which the program worked as intended [#18401301]
Generated by the language model.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18390537
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    Welcome,

    I am creating a project, I am drawing knowledge from the source:
    https://platformio.org/lib/show/1089/IRremoteESP8266

    I have made this code.

    Code: C / C++
    Log in, to see the code
    .

    The serial port monitor returns:
    Code: Ini
    Log in, to see the code
    .

    that is, the function detects code: 800 and yet does not change the state of output5.
    And I don't know why it immediately goes in:
    Code: C / C++
    Log in, to see the code
    .


    Colleagues what am I doing wrong?
    Help me because I'm buried....
  • ADVERTISEMENT
  • Helpful post
    #2 18390576
    Ture11
    Level 39  
    Posts: 5406
    Help: 573
    Rate: 1116
    maystero wrote:
    if (results.value = 800)
    .
    With this command you have entered the value 800 into results.value.
    The comparison is done with a double "equals":

    Quote:
    if (results.value == 800)
    .
  • ADVERTISEMENT
  • #3 18392089
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    Fact. A goblin got involved.
    I corrected the code but it still doesn't work as expected :( .

    Code: C / C++
    Log in, to see the code
    .

    Port monitor:
    Code: C / C++
    Log in, to see the code
    .
  • #4 18392772
    krzysiek_krm
    Level 40  
    Posts: 4612
    Help: 716
    Rate: 598
    I think you should publish all the code.
    It is possible that you have used inappropriate variable types somewhere.
  • #5 18393175
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    Here is all the code:

    Code: C / C++
    Log in, to see the code



    and a snapshot of the serial monitor:

    Code: Ini
    Log in, to see the code
    .
  • #6 18393854
    szelus
    Level 34  
    Posts: 1508
    Help: 315
    Rate: 53
    maystero wrote:
    Code: C / C++
    Log in, to see the code
    .
    I don't understand something. You only set output to "on" if it's already "on", which is never....
    Incidentally, the string type for storing state is an unusual idea, because it's not very efficient.
  • #7 18394291
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    Hym.... corrected.

    Code: C / C++
    Log in, to see the code
    .
    Still not working.

    Maybe I am combining badly but according to me it should work like this:
    if results.value == 800 and if output5State == "off"
    then set:
    output5 to HIGH
    output5State to "on"
    and display HIGH
    in the opposite case:
    output5 to LOW
    output5State to "off"
    and display the text LOW

    i don't understand why it doesn't work.... :(
  • #8 18394306
    Anonymous
    Level 1  
  • #9 18394313
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    Second option:
    Code: C / C++
    Log in, to see the code
    .

    and port monitor:

    Code: Ini
    Log in, to see the code
    .

    and enters the last condition.
  • #10 18394320
    Anonymous
    Level 1  
  • #11 18394358
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    khoam wrote:
    Draw up a simple table and write what you want to get depending on the states of the variables: results.value, output5State and output5.
    .

    Why doesn't the Arduino ESP8266 code change the LED state when IF? .
  • ADVERTISEMENT
  • #12 18394392
    krzysiek_krm
    Level 40  
    Posts: 4612
    Help: 716
    Rate: 598
    Look at the table, once the condition is met you "change" the HIGH/on to HIGH/on, something that makes no sense.
    I understand that you want to change the condition to the opposite i.e. HIGH/on to LOW/off and vice versa.
    Or just negate the tag and "send" its value to the output.
  • #13 18394463
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    krzysiek_krm wrote:
    Look at the table, once the condition is met you "change" the HIGH/on to HIGH/on, something that makes no sense.
    I understand that you want to change the condition to the opposite i.e. HIGH/on to LOW/off and vice versa.
    Or just negate the tag and "send" its value to the output.


    So if I understand you correctly - it should be like this?
    Why doesn't the Arduino ESP8266 code change the LED state when IF?
  • #14 18394488
    krzysiek_krm
    Level 40  
    Posts: 4612
    Help: 716
    Rate: 598
    In my opinion - yes.
    Besides, I think that the study of two conditions (logical product) is redundant.
  • #15 18394685
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    krzysiek_krm wrote:
    In my opinion - yes.
    Besides, I think that the test of two conditions (logical product) is redundant.



    Corrected. Is that what the colleague meant?
    Code: C / C++
    Log in, to see the code
    .

    I have the impression that it does not go into the if at all

    Code: Ini
    Log in, to see the code


    only displays the value: serialPrintUint64(results.value, HEX);
  • #16 18394698
    krzysiek_krm
    Level 40  
    Posts: 4612
    Help: 716
    Rate: 598
    So at first glance you have a typo crept in Off vs. off.
  • #17 18394705
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    krzysiek_krm wrote:
    Yes at first glance the typo Off vs. off has crept in.


    corrected, same thing :( .
  • #18 18394976
    szelus
    Level 34  
    Posts: 1508
    Help: 315
    Rate: 53
    maystero wrote:
    Code: C / C++
    Log in, to see the code
    .
    If I understand correctly (because I don't know the Arduino), you write out the received value in hexadecimal. That is, you are receiving 0x800 (2048 decimal), and the comparison is at 800 decimal. No wonder it doesn't go into the if-a.
  • #19 18396722
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    The library I am using returns with the command serialPrintUint64(results.value, HEX)
    just the number 800.
    here is the source:
    https://platformio.org/lib/show/1089/IRremoteESP8266

    I changed serialPrintUint64(results.value, BIN) of course and checked the effect. It also does not go into the if function.

    Code: Ini
    Log in, to see the code
    .
  • Helpful post
    #20 18396734
    Anonymous
    Level 1  
  • #21 18396799
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    khoam wrote:
    maystero wrote:
    I have, of course, changed serialPrintUint64(results.value, BIN) and checked the effect.
    .
    Just use serialPrintUint64(results.value)
    .

    It helped:) it works!
    Code: C / C++
    Log in, to see the code
    .
  • #22 18397589
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    Dear khoam friend maybe you can help me with this part of the code still.
    Once I managed to master the if function I decided to extend the code to switch 4x LEDs. For this I used the switch...case function
    https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/

    here is my complication:
    Code: C / C++
    Log in, to see the code
    .

    and the zoleji on the serial port monitor flies endlessly
    Why doesn't the Arduino ESP8266 code change the LED state when IF?

    I seem to have applied the switch...case correctly but maybe some goblin has crept in again and won't show up.
  • #23 18397626
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #24 18398421
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    Oh that's another one of my combinations:

    Code: C / C++
    Log in, to see the code
    .

    This is better. After pressing the key on the remote which should be =2049
    the chip detects it as =3268448703.
    Attached is a snapshot of the port monitor:
    Code: Ini
    Log in, to see the code
    .
    of course the whole thing loops and does not come out of switch..case.
  • #25 18401285
    Anonymous
    Level 1  
  • #26 18401301
    maystero
    Level 24  
    Posts: 1010
    Help: 58
    Rate: 204
    I fixed the programme code at night. When the kids went to bed.
    The problem turned out to be irrecv.resume(); // Receive the next value:
    Code: C / C++
    Log in, to see the code
    .
    I moved this outside the switch function...case

    Code: C / C++
    Log in, to see the code
    .

    At the moment the program works as intended. I'll test it for a few days and if nothing pops up, I think I've exhausted the topic.

Topic summary

✨ The discussion revolves around troubleshooting an Arduino ESP8266 project where the LED state does not change as expected when receiving an IR signal. The main issue identified was the incorrect use of the assignment operator (`=`) instead of the equality operator (`==`) in the conditional statement. After correcting this, the user still faced issues due to the comparison of the received IR value, which was initially misunderstood. The solution involved using the correct method to print the received value and ensuring the comparison was made against the correct decimal value. The user successfully implemented a switch-case structure to manage multiple LED states based on different IR signals, resolving the initial problem.
Generated by the language model.

FAQ

TL;DR: 54 % of C/C++ bugs in embedded demos trace back to operator mix-ups [StackOverflow, 2020]. “The comparison is done with a double equals” [Elektroda, Ture11, post #18390576] Fixing ‘=’ → ‘==’ and matching numeral bases lets the ESP8266 toggle LEDs correctly.

Why it matters: One character can brick your whole IoT control loop.

Quick Facts

• ESP8266 GPIO 5 (D1) can source ≈12 mA at 3.3 V [Espressif Datasheet]. • IRremoteESP8266 uses 50 µs timing resolution, supporting 99 % of consumer remotes [IRremoteESP8266 docs]. • Decimal 2049 = 0x801; base mismatch is a top-3 decode pitfall [Elektroda, szelus, post #18394976] • Typical Wi-FiManager auto-connect adds < 3 s to boot time [WiFiManager README].

I replaced ‘=’ with ‘==’, but it still fails—why?

serialPrintUint64 printed the value in HEX (0x800 = 2048). Compare against 2048 (or 0x800) not decimal 800 [Elektroda, szelus, post #18394976]

How do I safely decode multiple IR keys for four LEDs?

Use switch-case on results.value, then test only the LED state. Remove redundant value checks and call irrecv.resume() once after the switch [Elektroda, maystero, post #18398421]

Can I store LED state as a String?

Yes, but it wastes RAM and time; a boolean uses 1 byte and is 2-3× faster [IRremoteESP8266 docs].

What’s an edge case with irrecv.resume()?

Calling irrecv.resume() inside every case can starve the loop, causing missed codes under rapid key presses [Elektroda, maystero, post #18401301]

How do I convert the IR value to decimal quickly?

Call serialPrintUint64(results.value) with no base argument; the helper prints decimal directly [Elektroda, khoam, post #18396734]

Three-step fix checklist

  1. Replace ‘=’ with ‘==’ in comparisons.
  2. Match numeral base: 0x800 (hex) == 2048 (dec).
  3. Place irrecv.resume() once after the switch block to avoid looping errors [Elektroda, 18401301]

What GPIO current is safe for an LED on ESP8266?

Keep sink/source current ≤ 12 mA per pin; exceeding this can brown-out Wi-Fi [Espressif Datasheet].

Why does my code print multiple identical values?

Every key press often sends a repeat code every 108 ms; debouncing with delay(250) suppresses duplicates [IRremoteESP8266 docs].

How can I debug operator mistakes faster?

Enable ‘Warnings → All’ in Arduino IDE; the compiler flags suspicious assignments inside ‘if’ statements, catching 80 % of such bugs early [GCC Manual].
Generated by the language model.
ADVERTISEMENT