Why does the light turn ON?

Why does the light turn ON?
Is there something wrong with my understanding about how the code below works? Or am I just missing something somewhere else in my code?

bool a = true

void setup(){
  myFunction();
}

void myFunction(){
  if (a == false){
    turn on the light
  }
  else turn off the light
}

ethan_fraser:
Why does the light turn ON?

As you have not posted a working program and the wiring diagram that goes with it it is impossible to know.

...R

  if (a = false)

Whoops. This sets a to false. I assume that you meant to test whether it is false

= to assign values
== to test values

UKHeliBob:

  if (a = false)

Whoops. This sets a to false. I assume that you meant to test whether it is false

= to assign values
== to test values

yes sorry

Robin2:
As you have not posted a working program and the wiring diagram that goes with it it is impossible to know.

I only want to know if there is something I am misunderstanding about that code, because if there is then I will know that I should be looking elsewhere in my program for the issue.

It doesn't. That non-code won't ever turn anything on.

Steve

@OP, please read and deeply understand reply #1

#define ON_HIGHLIGHTED "<ON>        OFF "
#define OFF_HIGHLIGHTED " ON        <OFF>"

const int pages[] = {
  "set_time",
  "set_aot",
  "manual_switch",
  "auto_switch"
};
byte current_page = 2;

String manual_switch_state_visual = "ON";

void setup() {
  lcd.begin(16, 2);
  //other setup stuff

  showPage(); //the only function called from setup
}

void loop(){
  //doesn't begin until after showPage() has finished (I assume)
}

void showPage() {
  if (pages[current_page] == "set_time"){
    //(cut)
  }  
  else if (pages[current_page] == "auto_switch"){
    //(cut)
  }
  else if (pages[current_page] == "manual_switch"){
    lcd.clear();
    lcd.setCursor(0,1);
    if (manual_switch_state_visual == "ON"){
      lcd.print(ON_HIGHLIGHTED);
    }
    else if (manual_switch_state_visual == "OFF"){
      lcd.print(OFF_HIGHLIGHTED);
    }
  }
  else if (pages[current_page] == "set_aot"){
    //(cut)
  }
}

There is the actual code from my program cut down for relevancy. The actual problem is that when I run the program, OFF_HIGHLIGHTED is printed to the LCD.
(I apologise in advance if I had missed something in simplifying the code)

Perhaps if you actually printed the value in manual_switch_state_visual before using it, a clue by four would whack you.

The stripped down code is NOT the code that causes the problem, is it?

PaulS:
Perhaps if you actually printed the value in manual_switch_state_visual before using it, a clue by four would whack you.

It does return "OFF", I guess I'll just have to root around in the rest of the code for where that's happening

PaulS:
The stripped down code is NOT the code that causes the problem, is it?

I did hope it wasn't, which is why I posted here - in case there was something I was missing

const int pages[] = {
  "set_time",
  "set_aot",
  "manual_switch",
  "auto_switch"
};

I think the deep understanding needs some working-on

const int pages[] = {
  "set_time",
  "set_aot",
  "manual_switch",
  "auto_switch"
};

Interesting...

Can an int contain a value of "set_time" ?

Have you tried printing the value of pages[current_page] before testing its value ?

ethan_fraser:
I guess I'll just have to root around in the rest of the code for where that's happening

The only other place 'manual_switch_state_visual = "OFF";' is in all of my code is in loop().
I know that loop is definitely not being called from setup, so is there something about the order of things that means that it would/could go before showPage? And if so how can I remedy that?

UKHeliBob:
Can an int contain a value of "set_time" ?

Have you tried printing the value of pages[current_page] before testing its value ?

I know my code isn't perfect (I am far from having a 'good' understanding of programming) but that does work, for example I couldn't have set the page I wanted the program to start on by changing the initialisation of 'current_page' (which I have done, hence the starting at 2) if that was the case, let alone anything being being printed at all for that matter

Just out of interest insert this code in setup()

Serial.begin(9600);
  for (int current_page = 0; current_page < 4; current_page++)
  {
    Serial.print(current_page);
    Serial.print(" : ");
    Serial.println(pages[current_page]);
  }

What do you see printed in the Serial monitor ?

You cannot compare strings like this: pages[current_page] == "set_time"

UKHeliBob:
What do you see printed in the Serial monitor ?

0 : 331
1 : 453
2 : 422
3 : 359

Which If I had to assume would be the values of the letters (and underscore) added together, in what encoding and in what base I don't know.
I know that's not pretty coding either but since I'm not printing them out or doing arithmetic with them it has worked.
What data type should I use for an array of strings?

Danois90:
You cannot compare strings like this: pages[current_page] == "set_time"

if (pages[current_page] == "set_time"){
    Serial.print("yea");
  }

^^works

ethan_fraser:
^^works

Only by dumb luck, you do not know what is being compared. An array of "int" (const int pages[]) contains numbers, not strings. Why it compiles at all is weird since gcc won't have any of it.

Danois90:
you do not know what is being compared.

You're right, I don't know why it's letting me compare an integer to a string.
What data type should I be using for an array of strings?

Anyway, that is beside the point to my original issue, to which I still haven't had any proper answers.

It may "work" but it is wrong

Try this

const char * pages[] =
{
  "set_time",
  "set_aot",
  "manual_switch",
  "auto_switch"
};

void setup()
{
  Serial.begin(115200);
  for (int current_page = 0; current_page < 4; current_page++)
  {
    Serial.print(current_page);
    Serial.print(" : ");
    Serial.println(pages[current_page]);
    if (strcmp(pages[current_page], "manual_switch") == 0)
    {
      Serial.println("matched");
    }
  }
}

void loop()
{
}