logo elektroda
logo elektroda
X
logo elektroda

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

maystero 864 25
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18390537
    maystero
    Level 24  
    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  
    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)
    .
  • #3 18392089
    maystero
    Level 24  
    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  
    I think you should publish all the code.
    It is possible that you have used inappropriate variable types somewhere.
  • #5 18393175
    maystero
    Level 24  
    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
    .
  • ADVERTISEMENT
  • #6 18393854
    szelus
    Level 34  
    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  
    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
    khoam
    Level 42  
    maystero wrote:
    if results.value == 800 and if output5State == "off"
    .
    Not really.
    If (results.value == 800) and (output5 == HIGH && output5State == "on") then output5 sets to HIGH.
    If (results.value == 800) and (output5 != HIGH || output5State != "on") then output5 sets to LOW.
    In any other case it does nothing with output5.
  • #9 18394313
    maystero
    Level 24  
    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
    khoam
    Level 42  
    Make a simple table and write what you want to get depending on the states of the variables: results.value, output5State and output5.
  • #11 18394358
    maystero
    Level 24  
    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? .
  • #12 18394392
    krzysiek_krm
    Level 40  
    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.
  • ADVERTISEMENT
  • #13 18394463
    maystero
    Level 24  
    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  
    In my opinion - yes.
    Besides, I think that the study of two conditions (logical product) is redundant.
  • ADVERTISEMENT
  • #15 18394685
    maystero
    Level 24  
    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  
    So at first glance you have a typo crept in Off vs. off.
  • #17 18394705
    maystero
    Level 24  
    krzysiek_krm wrote:
    Yes at first glance the typo Off vs. off has crept in.


    corrected, same thing :( .
  • #18 18394976
    szelus
    Level 34  
    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  
    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
    khoam
    Level 42  
    maystero wrote:
    I have, of course, changed serialPrintUint64(results.value, BIN) and checked the effect.
    .
    Just use serialPrintUint64(results.value)
  • #21 18396799
    maystero
    Level 24  
    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  
    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
    khoam
    Level 42  
    First of all, instead of:
    Code: C / C++
    Log in, to see the code
    it should be:
    Code: C / C++
    Log in, to see the code
    Additional checking of the state of the 'results.value' variable is unnecessary, since:
    Code: C / C++
    Log in, to see the code
    already does this.

    The switch..case usually also includes a 'default:' label in case none of the previous conditions have occurred and an action needs to be taken too - you have this described in the link you provided.
  • #24 18398421
    maystero
    Level 24  
    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
    khoam
    Level 42  
    maystero wrote:
    This is better. When you press a key on the remote that should be =2049 the chip detects it as =3268448703.
    .
    Where in the switch...case do you send the results.value to Serial? I am referring to the code above.

    maystero wrote:
    obviously the whole thing loops and doesn't come out of switch..case.
    .
    It comes out, otherwise there wouldn't be a series of "some error" messages.
  • #26 18401301
    maystero
    Level 24  
    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.
Summary generated by the language model.
ADVERTISEMENT