IF statement is fulfilled, but code inside if is skipped to else anyway [SOLVED]

Hi there, so I got a strange bug (or my mistake somehow) where I read out data from SD card and based on if it's '0' or '1' I execute two different codes. So the problem is that on my serial monitor I can see that value that is being read from my .txt file on SD card is "1" which means my If statement should execute code, but then it skips to else.

I put Serial print to If and else and before if I print the value of variable I'm questioning inside "if", so I can see on my monitor that variable is "1", but after that next print is from else, not if.

here is that part of my code:

char sett;
  Serial.println(sett);

  //---------------------

  File DOC = SD.open("settings/welcome.txt");

  if (DOC) {
    sett = DOC.read();
    Serial.println(sett); //Here I see value of sett is '1'
    delay(10);
    DOC.close();
  }
  else
    Serial.println(F("error opening file"));


  if (sett == "1") { //it doesn't matter if I put '' or "", it's not working either way...
    Serial.println(F("sett == 1"));
    drawWelcomeScreen();
    SD.remove("settings/welcome.txt");
    File DOC = SD.open("settings/welcome.txt", FILE_WRITE);
    DOC.print("0");
    DOC.close();
    welcomescreen = false;
    ColourSettings();
    SettingsCheck();
  }
  else { //so with value of '1' I end up here even though I should be inside IF
    Serial.println(F("sett == else"));
    wpage = 5;
    welcomescreen = false;
    ColourSettings();
    SettingsCheck();
    drawHomeScreen();
  }

Do you have any ideas what could be the problem?

Thanks alot

(sett == "1") a single char can never equal a string.

char sett;
if (sett == "1") { //it doesn't matter if I put '' or "", it's not working either way...

For a single char you must use single quotes '1'.

Given what you have shown I don't understand why the comparison is not true, but if you use "1" it will never == sett.

Can you provide more complete code, or perhaps a smaller body of code which compiles, runs and demonstrates the problem. How is sett being stored?

Yeah, thanks for quick answers... :slight_smile:

I was using ' ' in every version of my code and it was working. And 2 days ago this bug came up, so I tried with " "... now I changed back to ' ' again and it's working. I don't know what happened in the meantime :o

Thank you guys anyway :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.