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
}
}
}