Variables in an if statement

I’ve been working on this Arduino project for almost 8 months now, I came into a snag with an if statement. I am using variables to try and structure lcd messages, but it’s not working and i have idea why, this is my first project and I’ve only been taking and modifying other peoples code. Please help my brain is dying. (full code bellow).

// Unsigned longs. Extended funtions
unsigned long start = 5000; // 5 seconds for lcd start up message
 
// Variables.
int secretCode[maximumKnocks] = {50, 25, 25, 50, 100, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};  // Initial setup: "Shave and a Hair Cut, two bits."
int knockReadings[maximumKnocks];   // When someone knocks this array fills with delays between knocks.
int knockSensorValue = 0;           // Last reading of the knock sensor.
int programButtonPressed = false;   // Flag so we remember the programming button setting at the end of the cycle.
int lcdState = 0;  // lcd controller

#include<LiquidCrystal.h>

LiquidCrystal lcd(12,11,7,6,5,4);

void setup() {
  pinMode(lockMotor, OUTPUT);
  pinMode(gunMotor, OUTPUT);
  
  lcd.begin(16,2);
  lcd.print("Geniouse progwam");
  lcd.setCursor(0,1);
  lcd.print("has startid   ");
  
  Serial.begin(9600);            // Uncomment the Serial.bla lines for debugging.
  Serial.println(" This highly intelegent program has started.");       // but feel free to comment them out after it's working right.

  if(millis() >= start)
  {
    int lcdState = 1;
  }
}
void loop() {
  if(lcdState = 1)
  {
    lcd.clear();
  }

I_am_zEE_HACKERMANNNNN.ino (8.26 KB)

I am using variables to try and structure lcd messages, but it's not working

There are only 2 lcd.print() statements in your sketch and neither of them use variables

What do you mean by "not working" ?

  if (lcdState = 1)

Fix this first

= for setting the value of a variable
== for comparing two values

You will have to give a proper description of what you expect the code to do and what it actually does; "it's not working" doesn't mean anything.

Below stood out in setup()
1)
When setup() starts, millis() is 0. By the time it reaches the if statement in setup, it more than likely still is 0. So the body of the if will be skipped.
2)
You have two variables lcdState, the global one and the one inside the if body; they are different variables.

I just fixed it to be

// Unsigned longs. Extended funtions
unsigned long start = 5000; // 5 seconds for lcd start up message
 
// Variables.
int secretCode[maximumKnocks] = {50, 25, 25, 50, 100, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};  // Initial setup: "Shave and a Hair Cut, two bits."
int knockReadings[maximumKnocks];   // When someone knocks this array fills with delays between knocks.
int knockSensorValue = 0;           // Last reading of the knock sensor.
int programButtonPressed = false;   // Flag so we remember the programming button setting at the end of the cycle.
int lcdState = 0; // controller for lcd
#include<LiquidCrystal.h>

LiquidCrystal lcd(12,11,7,6,5,4);

void setup() {
  pinMode(lockMotor, OUTPUT);
  pinMode(gunMotor, OUTPUT);
  
  lcd.begin(16,2);
  lcd.print("Geniouse progwam");
  lcd.setCursor(0,1);
  lcd.print("has startid   ");
  
  Serial.begin(9600);            // Uncomment the Serial.bla lines for debugging.
  Serial.println(" This highly intelegent program has started.");       // but feel free to comment them out after it's working right.

  if(millis() >= start)
  {
    int lcdState = 1;
  }
}
void loop() {
  if (lcdState == 1)
  {
    lcd.clear();
  }

but, the text still doesn’t clear after 5 seconds. How do I make both variables the same

int lcdState = 1;Lose the “int”

setup runs ONE time.
When setup runs, millis() will be a very small number, possibly even 0.
When your "if(millis() >= start)", millis will most definitely NOT be >= 5000
So, lcdstate will NOT be set to 1.
setup will then return, and NOT be run again, until the board is reset.
When loop is run, lcdstate will ABSOLUTELY NOT be == 1
So lcd.clear will NEVER be run.

Regards,
Ray L.

  if (millis() >= start)
  {
    int lcdState = 1;
  }

This code is in setup() and is run once
Put it, or something similar, in loop() so that it is executed continuously

…but lose the “int”

The thing is, I'm adding on to this and if it's in loop it will keep on setting lcdState to 1 and it will keep on clearing the new lcd display text.

yertlethetechyturtle:
The thing is, I'm adding on to this and if it's in loop it will keep on setting lcdState to 1 and it will keep on clearing the new lcd display text.

The thing is, you didn't mention that before

Does anything need to happen in the 5 seconds ?
Now is the time to tell us

after the starting message there’s gonna be a loop of messages like

lcd.print("Listening for");
lcd.setCursor(0,1);
lcd.print("knock");
*some kind of millis()*
lcd.clear();
lcd.print("Listening for");
lcd.setCursor(0,1);
lcd.print("knock.");
*some kind of millis()*
lcd.clear();
lcd.print("Listening for");
lcd.setCursor(0,1);
lcd.print("knock..");
*some kind of millis()*
lcd.clear();
lcd.print("Listening for");
lcd.setCursor(0,1);
lcd.print("knock...");
*some kind of millis()*
lcd.clear();

for a … animation, when there is a doorUnforgiveness it will print a lcd message and triggerDoorUnlock will also print a lcd message

I_am_zEE_HACKERMANNNNN.ino (8.33 KB)

If the startup message only occurs once and nothing else is happening at the same time then print the message in setup(), delay() for the required period then clear the LCD

Thanks for the help everyone I’m almost done, but for some reason when I compile the code it says
‘LiquidCrystal’ does not name a type

attached new code below

I_am_zEE_HACKERMANNNNN.ino (11.3 KB)

Ooops....

int repeat = 0

...missing something perhaps?

Hint: ;

Edit also...

     int startTime = millis()
     int now = millis()
     unsigned long middle = 3000

...and many other places also.

Plus code blocks { } are not optional...

          if(repeat == 0)
            now = millis()
            lcd.print("Listenting for");
            lcd.setCursor(0,1);
            lcd.print("knock");

Brackets are not required...

            (repeat = 1);

You'll have to be lucky...

            if(now - startTime == middle) {

You really should learn to use the && operator...

                                if (firstHappened == true) { 
                                  if (unlockNotice == false) { 
                                    if (shotWarning == false) {
                                if (firstHappened == true && unlockNotice == false && shotWarning == false) {

ALL times should be unsigned long (not int)...

int now = millis()

So now it can upload but it doesn’t do the … animation. it’s supposed to write the program started message for 5 seconds and then go to the animation and it does the first part but it gets stuck on “listening for knock” with no dots. I think my millis() isn’t working there but I don’t know how to fix it. it’s supposed to wait 3 seconds then add a dot then another then another then restart at 0 dots.

I_am_zEE_HACKERMANNNNN.ino (10.8 KB)

int now = millis();

And others . . .

When saving the return value from millis() use ‘unsigned long’.

I made the startTime and now unsigned longs but it did nothing plus the other millis() in this code works as them being int

You have:

while ((now - startTime < knockComplete) && (currentKnockNumber < maximumKnocks));

How do you get out of this loop if the variables don’t change ?

‘int’ will work as long as your Arduino hasn’t been running for more than 32 seconds. After that, all bets are off.

yertlethetechyturtle:
So now it can upload but it doesn’t do the … animation. it’s supposed to write the program started message for 5 seconds and then go to the animation and it does the first part but it gets stuck on “listening for knock” with no dots. I think my millis() isn’t working there but I don’t know how to fix it. it’s supposed to wait 3 seconds then add a dot then another then another then restart at 0 dots.

Probably because in order to get to any of the code that prints any of the dots variable ‘repeat’ must be 2, 4 or 6.

And to get to repeat = 2 this condition must be true…

if (now - startTime == middle) {

But since you start loop with setting both variables to the current millis() value…

   int startTime = millis();
    int now = millis();
    unsigned long middle = 3000;

And then another somewhat superfluous…

now = millis();

… inside the first ‘if’ statement, that condition can never be met.

i.e. Since ‘now’ will only ever be either equal to (or perhaps a ms or so greater than) ‘startTime’.

Also, as I pointer out earlier, even with correctly structured code, you are chancing your arm with hoping for an exact equal match on millis() values. What happens if you are 1ms late in checking this time around loop? Then the difference will be 3001 and you won’t ever set repeat = 2.

Always use <= or >= comparisons for times to ensure that if you are late checking, you still meet the required condition.

Also you still have a couple of…

(repeat = 1);

…in your code. Please fix them.

Edit:
Also after loop executes the first time it will go into the first if statement…

    if (firstHappened == true && unlockNotice == false && shotWarning == false && repeat == 0) {
  ...
  repeat = 1;

Now ‘repeat’ is set to 1. So the code can never get back into that ‘if’ statement (because of ‘repeat == 0 condition).
Also note that all other ‘if’ statements for the repeat == 2, 4 and 6 are nested inside the first if. So those cant’ be reached either.