If-Then Statements looping incorrectly

Hey! I am having a problem with the code below as for some reason, my arduino leonardo is looping code in the void loop without regard to the if then statements. If follows them for a few seconds and then goes erratic. Here is the code. I want it to tell me when the fan is on or not by the input pin 1. Thanks! If i need to share more information, let me know and i will be happy to.

I am using a 14-2 lcd display to tell me whether the cooling fan is OFF or ON


#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
const int PIN_RED   = 7;
const int PIN_GREEN = 8;
const int PIN_BLUE  = 9;
const int buttonPin = A1;
const int VOL_PIN = 0;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(14, 2);
  lcd.print("Hello Aaron");
  lcd.setCursor(0, 1);
  lcd.print("KM6RNV");
  delay(2000);
  lcd.clear();
  pinMode(PIN_RED,   OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE,  OUTPUT);
  analogWrite(PIN_RED,   0);
  analogWrite(PIN_GREEN, 256);
  analogWrite(PIN_BLUE,  0);
  delay(2000);


}

void loop() {
  int buttonState;
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    lcd.clear();
    lcd.print("Cooling Fan ON");
    digitalWrite(PIN_RED, LOW);
    digitalWrite(PIN_GREEN, HIGH);
    digitalWrite(PIN_BLUE, LOW);
    delay(1000);

  } else{
    lcd.clear();
    lcd.print("Cooling Fan OFF");
    digitalWrite(PIN_RED, HIGH);
    digitalWrite(PIN_GREEN, LOW);
    digitalWrite(PIN_BLUE, LOW);
    delay(1000);

  }

}

Welcome to the forum, and thanks for using code tags on your first post (a rare event)!

Please explain what you expect to happen and what happens instead. Post examples of the erratic behavior.

Also post a wiring diagram, showing how the button is wired. You may have a floating input pin.

I didn't see ...

pinMode(buttonPin,   INPUT); // or INPUT_PULLUP

anywhere...?

Pins default to INPUT but recommend always use INPUT_PULLUP.

Do you want things to happen while the button is pressed or when the button becomes pressed. I mean is the level or the transition important? Check out the state change detection tutorial and the state change for active low switches tutorial.

Hello
Check this line of code.

Your button pin is not using the internal pull-up resistor (pinMode(pin, INPUT_PULLUP);) so you are required to have an external pull-up or pull-down resistor. Which did you use?

I didnt use either of them, i forgot to include that in my code. thanks so much! I will try it now and let you know

You'll probably find issues with button bounce using the code you have there, so you might get lots of super-fast toggles on/off. You can solve this with code.

If you're using INPUT_PULLUP then you'll also need to flip your button logic so you're checking for 'LOW' when the button is pressed.