Hi! I'm trying to add a button to my code to control 3 if statements. I tried so many things, but it just thinks the switch is always on. Please help!
Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
float temp;
float cold;
float hot;
int tempPin = A1;
const int buttonPin = 3; // the number of the pushbutton pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
const byte switchPin = 3; //give the switch pin a name
//-------------------------------------------------- --------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void setup()
{
tone(11, 700 , 100);
delay(100);
tone(11, 800 , 100);
delay(100);
tone(11, 1000 , 100);
delay(100);
tone(11, 900 , 100);
delay(100);
tone(11, 1000 , 200);
delay(200);
Serial.begin(9600);
// Print a message to the LCD.
pinMode(2, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("3D print filter");
lcd.setCursor(0,1);
lcd.print("Coded by:notmyname");
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void loop()
{
analogRead(tempPin); //reading temp.
temp = analogRead(tempPin); // saving temp.
// read analog volt from sensor and save to variable temp
temp = temp * 0.48828125; //convert voltage to c.
// convert the analog volt to its temperature equivalent
analogRead(tempPin); // reading temp.
int set = map(analogRead(A2), 0, 1023, 60, 20); //saving and conveting the pot.
//----------------------------------------------------------------------------
buttonState = digitalRead(buttonPin);
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//These three ifs will be in a button
//These three ifs will be in a button
if (temp > (set + 2)) {
delay(100);
if (temp > (set + 2)) {
digitalWrite(2, HIGH);
lcd.setCursor(0, 0);
lcd.print(" Fan/ON ");
lcd.setCursor(0, 1);
lcd.print("Act temp: ");
lcd.print(temp - 1);
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
if (temp < (set - 1)) {
delay(100);
if (temp < (set - 1)) {
digitalWrite(2, LOW);
lcd.setCursor(0, 0);
lcd.print("Act Temp: ");
lcd.print(temp);
lcd.setCursor(0, 1);
lcd.print("Set Temp: ");
lcd.print(set);
lcd.print(" ");
delay(50);
}
}
if (temp > (set + 10)) {
digitalWrite(2, HIGH);
lcd.setCursor(0, 0);
lcd.print(" Fan/HOT ");
lcd.setCursor(0, 1);
lcd.print("Act temp: ");
tone(11, 1500, 100);
lcd.print(temp - 2);
lcd.backlight(); // turn on backlight.
delay(100);
lcd.noBacklight(); // turn off backlight
}
}
This code doesn't have any button code, because I don't know how to do it. I have a INPUT_PULLUP switch.