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