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
}
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.
#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)
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
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?
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.