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

hey, i never said that haha

i understand now. thanks for explaining!

i already changed it in favor of what the original post has in it, and i forget what the other code was, unfortunately.

yes, i would like to fix the code. unfortunately i do not know where to start.

You may know this, but under the tools menu the Arduino IDE has an "autoformat" feature that will format the code according to "good practice", that is it will indent the code inside if statements and align the curly brackets.

The benefit of this is that it makes looking at the code easier which benefits both you as the code developer and anyone else who might look at it. This, in addition to getting rid of excessive blank lines makes it easier to see what's going on in the code.

In your code you have several if statements that test if the button has been pressed and then and embedded if statement that checks the random number. In addition there are delay statements that may be executed if the button is pressed whether or not the random number matches. This may or may not be the behavior you want, but my guess is that you intended to detect the button press and then do one of 3 things depending upon the state of the random number. That may not happen if the button is momentarily depressed before the code has found the "right" random number match.

i'm not kidding, i'm mentally challenged, and i do not understand what to do to fix them, as i am a total beginner to this coding language.

edit: got rid of the "and not low"s

The way to fix that is to study programming language tutorials on line (there are countless examples), and study other people's code, starting with the simple examples in the Arduino IDE. Learn to crawl before trying to run.

A good place to start is always with a plain language description of what the code is supposed to do.

Your code has a number of curious structures, as others have noted above, including stuff that is clearly unnecessary, but even fixing those may not give the behavior desired and we don't have that description.

In this forum, you, as a beginner, can get (mostly) helpful guidance from some talented people, but the help you get is largely dependent upon how well you can communicate your problem.

such is why i have ordered a book called "arduino book for beginners". once it arrives, i will study it like crazy.

the goal for my code is to generate a random number, one to 3, and display the placeholders 1, 2, or 3 on my LCD screen based off of what the random number is.

i did this, uploaded the new code, and still no change. im sorry if im annoying you.

wait maybe it worked hold up

it works now. thank you all for your help, i appreciate it!

turns out im just freakin' stupid

My suggestion would be to restructure the "loop" portion of the code with one "if statement" to test for button pressed. Under that statement (inside the curly brackets) consolidate your various tests for the random number. If there's still a problem achieving the desired behavior, format and repost the code you have at that point and describe what behavior is lacking.

Repeat as necessary.

P.S. A problem I see with this forum is that a beginner coder gets a lot of (valid comments) from different "experts" and it becomes difficult to sort the various suggestions, so I'm going to stand down for a bit and let you interact with fewer mentors.

i had no idea i was supposed to do that. i lack some common knowledge. regardless, it works now, so thank you for your assistance!

hmm. thinking. something I lack.

oh my lord stop being so angry and leave me alone
you solved the darn problem just be happy you helped a newbie instead of telling them that their disability is an excuse and if they cant think they should give up

Yeah none of us ever did anything that dumb. :wink:

Boolean Logic is pretty cool, though, and it is a shame it rarely gets the attention it might deserve before anyone starts to try programming.

It was fully a thing before the first relay or tube or transistor ever fired off in service of digital computation.

My path went through mathematics and philosophy, where all that stuff comes up in a different context.

Even so, when I started programming I did the same kinda thing, if this is true, but what about if isn't true, foolish looking back on it.

I think it's the Law of the Excluded Middle or something like that. Also Venn Diagrams. And there are tools for organizing multiple logical conditions and crafting logical expressions.

But certainly we can all agree if something is equal to one, it is automatically not equal to all things that are not one.

At least in the side of the brain where that stuff happens. My strong brain side is the one that can't even remember which side is that side. :expressionless:

Good Luck on the learning curve.

a7

thank you and have a nice day :slight_smile:

thank you! good luck on your future endeavors as well!

Hi @giac427 ,

buying a book about C++ or at least reading some tutorials and testing some examples first is always a good idea when starting with Arduino ... Learning from examples should not be so difficult as the Arduino IDE provides really a lot of them, from simple to more complex ones.

E.g. here https://docs.arduino.cc/built-in-examples/

Feel free to look at this example and read the comments based on your sketch:

// include the library code:
#include <LiquidCrystal.h>
const byte 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, 115200 is usually the state-of-the-art baudrate
  Serial.begin(115200);
  // initialize the pushbutton pin as an input; this requires to
  // connect it to HIGH level and the buttonPin connection to GND via a resistor
  // ... usually using INPUT_PULLUP is easier to realize ...
  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 randNumber to Zero; if the button is pressed randNumber will
  // get a value between 1 and 3 anyway
  // So we can check later if randNumber is greater than zero
  // to take action, if randNumber is zero we do nothing!
  randNumber = 0;
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    // turn LED on:
    randNumber = random(1, 4);   // generate a random number
    Serial.println(randNumber);
    lcd.setCursor(15, 0); // set the cursor to column 0, line 2
    lcd.print(randNumber);
    delay(20);//delay of 0.02sec to "debounce" the button
  }
  // switch evaluates the value of randNumber is uses the according case 
  // "break;" at the end of each case avoids that the program runs into the
  // next case 
  switch (randNumber) {
    case 0 :
      // We do nothing!!!
      break;
    case 1:
         Serial.println("Do the stuff here for 1");
         delay(1000); // Give some time to release the button!
         // Reset randNumber so that nothing happens in the next loop()! 
         randNumber = 0;
      break;
    case 2:
         Serial.println("Do the stuff here for 2");
         delay(1000); // Give some time to release the button!
         // Reset randNumber so that nothing happens in the next loop()! 
         randNumber = 0;
      break;
    case 3:
         Serial.println("Do the stuff here for 3");
         delay(1000); // Give some time to release the button!
         // Reset randNumber so that nothing happens in the next loop()! 
         randNumber = 0;
      break;
  }
}

You can test it on Wokwi: https://wokwi.com/projects/388097069591716865

Actually there is much more to do with a button than I did here but I did not want to add too much in the beginning. What is behind "debouncing a button" can be read here https://docs.arduino.cc/built-in-examples/digital/Debounce/

Good luck!