[SOLVED] If statement running regardless of whether condition is met

I'm quite new to Arduino as a whole, so please forgive me if this is something simple I don't know about. I've been wracking my brain for days.

After searching through google like 8 times, and searching through more forums than that, I came here to ask "Why are my if statements running even though the conditions are false?"
Here's the full code dump of my sketch (i hope i used the formatting tool properly)

#include <LiquidCrystal.h>

// include the library code:
#include <LiquidCrystal.h>


#define buttonPin 12


int buttonState = 0;  // variable for reading the pushbutton status



int randNumber;        // the variable which is supposed to hold the random number

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(6, 7, 8, 9, 10, 11); /// REGISTER SELECT PIN,ENABLE PIN,D4 PIN,D5 PIN, D6 PIN, D7 PIN

void setup()

{

 // initialize the serial port
  Serial.begin(9600);

  
// initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
 
 
  // initialize the pseudo-random number generator
 randomSeed(analogRead(0));


// set up the LCD’s number of columns and rows:

lcd.begin(16, 2);

}

 

void loop()

{







 
// set the cursor to column 0, line 1
lcd.setCursor(16, 0); // set the cursor to column 0, line 2





// read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
        randNumber = random(1, 4);   // generate a random number
Serial.println(randNumber);
  delay(1000);//delay of 0.75sec      
        

  


  }






if (buttonState == HIGH and not LOW) {
  delay(1000);//delay of 0.75sec
if (randNumber == 1 and not 2 or 3) {
lcd.setCursor(15, 0); // set the cursor to column 0, line 2
lcd.print("1");

delay(1000);//delay of 0.75sec
// do stuff if the condition is true
}


    }





  

if (buttonState == HIGH and not LOW) {
  delay(1000);//delay of 0.75sec
if (randNumber == 2 and not 1 or 3) {
lcd.setCursor(15, 0); // set the cursor to column 0, line 2
lcd.print("2");

delay(1000);//delay of 0.75sec
// do stuff if the condition is true
}


    }

    


if (buttonState == HIGH and not LOW) {
  delay(1000);//delay of 0.75sec
if (randNumber == 3 and not 1 or 2) {
lcd.setCursor(15, 0); // set the cursor to column 0, line 2
lcd.print("3");

delay(1000);//delay of 0.75sec
// do stuff if the condition is true
}


    }


       
    

    













}

somebody asked for me to format the code, so i did so, here's what the IDE spewed out after i hit auto format:

#include <LiquidCrystal.h>

// include the library code:
#include <LiquidCrystal.h>


#define buttonPin 12


int buttonState = 0;  // variable for reading the pushbutton status



int randNumber;        // the variable which is supposed to hold the random number

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(6, 7, 8, 9, 10, 11); /// REGISTER SELECT PIN,ENABLE PIN,D4 PIN,D5 PIN, D6 PIN, D7 PIN

void setup()

{

  // initialize the serial port
  Serial.begin(9600);


  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);


  // initialize the pseudo-random number generator
  randomSeed(analogRead(0));


  // set up the LCD’s number of columns and rows:

  lcd.begin(16, 2);

}



void loop()

{








  // set the cursor to column 0, line 1
  lcd.setCursor(16, 0); // set the cursor to column 0, line 2





  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    randNumber = random(1, 4);   // generate a random number
    Serial.println(randNumber);
    delay(1000);//delay of 0.75sec





  }






  if (buttonState == HIGH and not LOW) {
    delay(1000);//delay of 0.75sec
    if (randNumber == 1 and not 2 or 3) {
      lcd.setCursor(15, 0); // set the cursor to column 0, line 2
      lcd.print("1");

      delay(1000);//delay of 0.75sec
      // do stuff if the condition is true
    }


  }







  if (buttonState == HIGH and not LOW) {
    delay(1000);//delay of 0.75sec
    if (randNumber == 2 and not 1 or 3) {
      lcd.setCursor(15, 0); // set the cursor to column 0, line 2
      lcd.print("2");

      delay(1000);//delay of 0.75sec
      // do stuff if the condition is true
    }


  }




  if (buttonState == HIGH and not LOW) {
    delay(1000);//delay of 0.75sec
    if (randNumber == 3 and not 1 or 2) {
      lcd.setCursor(15, 0); // set the cursor to column 0, line 2
      lcd.print("3");

      delay(1000);//delay of 0.75sec
      // do stuff if the condition is true
    }


  }



















}

This, and other conditions like it, are most likely faulty logic conditions.

What, exactly, do you intend this to do?

If randNumber is equal to 1, then it certainly is not equal to 2 or 3.

To select actions based on a numerical value, read up on the switch/case language construct.

2 Likes

Can you show how the button is wired?

I intend for that statement to run some code if the previously generated random number (stored in the variable randNumber) equals 1 and only 1. i tried just (randNumber == 1) and it did not change anything.

My first guess is that you don't have a pull-up or pull-down on your pushbutton, or it's wired wrong or is otherwise not working.

Try the Digital Read Serial Example (modified for the pin you're using) to make sure the button is working.

if (buttonState == HIGH and not LOW) {

I didn't think C++ understands the English words
and, or, or not

P.S.
I know C++ is not broken and "if" does work.

1 Like

wait you might be right hold on

nevermind i replaced the words with their symbols and it didnt change anything

sorry for terrible quality, my webcam sucks

I'm surprised this even compiles.

Here are some basics of boolean arithmetic in C.

AND operator in C is &&, OR operator in C is ||, NOT operator is !

Everything different from 0 is true an everything equal to 0 is false.

if (buttonState == HIGH and not LOW)

was probably meant as if (buttonState == HIGH && buttonState != LOW)

which is double checking the same thing. if (buttonState == HIGH) is enough or even only if (buttonState) since HIGH is different from 0 and hence true.

Taking a look at if (randNumber == 1 and not 2 or 3): 3 is different from 0 and hence always true, so the whole expression is always true as well. Again, checking if (randNumber == 1) should be completely enough.

and, or, not are all valid keywords in C++, and have the same functions in logical conditions as above.

2 Likes

thank you for the little lesson. I've never touched C, and i thought Arduino was its own version of another coding language. not sure where i got that from.

Standard C++.

Do the best thing and draw an annotated schematic showing how it is wired.

ah. thanks for clearing that up. so if i were to find and use regular c++ code it (might) work?

my apologies. how would i go about that?

Is a good idea, but standard C is fine too.

understood. thank you!

my lord, man, no need to be so rude. i'm a beginner trying my best. just autoformatted my code btw, gonna edit the original post.

also also, I DID EARLIER:

Change it to remove the useless extra conditions and it will work like you want.

i took it as rude, possibly becuase i'm a little sensitive. my apologies. i am interested in having your help fixing it. i'll answer the questions now.

i had tried it without the extra stuff earlier, and unfortunately no difference.