How do I add a button to my code?

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.

How is your button wired? If you are using INPUT_PULLUP that implies you have the button wired to GND, in which case if the button is NOT pressed you will read a HIGH from that pin. If you do not have your button wired that way then you have a wiring issue.

Look at the following code to detect a button press:

StateChangeDetection

Keep in mind that code uses a pull down so HIGH means Pressed. You can swap the detection state.

It is easier to help with code that does not work right if we can see it. Post the non working code, please. Describe what the code actually does and how that is different from what you want.

Also post a schematic of your wiring. And a photo of your wiring is often helpful. We need to know how the switch is wired.

This tutorial (by me) shows how to do the state change detection with a switch wired to ground (active LOW).

At 63 posts you should know to always:

Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components. Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

A couple of other things:

  1. Remove (or comment out) everything else in your code except checking the button. Just print to the serial console the state of the button to make sure the button and wiring are working. When that is confirmed add your other functionality.

  2. Any delays you have in your code may cause issues with reading the button often enough to detect a press. Look at the tutorial on replacing delay() with millis().

BlinkWithoutDelay

  1. You have the following line of code 2 times:
  analogRead(tempPin); //reading temp.

That line of code accomplishes nothing as it does not assign the value read to a variable (which you do in the next line of code). Just remove those lines.

groundFungus:
This tutorial (by me) shows how to do the state change detection with a switch wired to ground (active LOW).

Thanks groundFungus. I will use that link from now on since we all suggest using INPUT_PULLUP.

That line of code accomplishes nothing as it does not assign the value read to a variable (which you do in the next line of code).

I have seen it recommended (right or wrong, I don't know) to do a dummy read of an analog input especially when switching channels or when measuring from a high impecance to prevent bad readings.

You are welcome Todd. I hoped it would be useful.

groundFungus:
I have seen it recommended (right or wrong, I don't know) to do a dummy read of an analog input especially when switching channels or when measuring from a high impecance to prevent bad readings.

Ah. OK. Didn't know that. Thanks!

Sorry, I can't post images or any wiring, so here is a list:
Middle of the switch: 5v
The left side of the switch: Ground
The right side of the switch: D3

The bad 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:Rego");
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

void loop()
{

  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
  int  set = map(analogRead(A2), 0, 1023, 60, 20); //saving and conveting the pot.

  //----------------------------------------------------------------------------
   buttonState = digitalRead(buttonPin);
  //----------------------------------------------------------------------------
  //----------------------------------------------------------------------------
  //this is the bad code
    if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    lcd.print("manual")
  } else {
    // the code
      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
  }
  }
}

and also the switch: BK-09085YE / Billenő kapcsoló, 1ák., 6A, 250V, ON-OFF - HESTORE - Elektronikai alkatrész kis- és nagykereskedelem

“ Middle of the switch: 5v
The left side of the switch: Ground
The right side of the switch: D3”

What switch are you using show us a link.

What you are saying, with some switches, you will be shorting 5v to GND; a good idea if you want to blow up something ! :o


Try:

INPUT_PULLUP ——Input pin——[Switch]——GND

One terminal may be is a Yellow light connection.

Get out a DVM and ring out the terminals to find which ones are the contacts.

I know that the golden one goes to Gnd, I already tested that.
The middle one goes to 5v.

And also I tested with the original Arduino button code, only hanged the pin mode to INPUT_PULLUP, and it worked, so the switch is good. There is something wrong with my code. But I don't know what

rego0116:
I know that the golden one goes to Gnd, I already tested that.
The middle one goes to 5v.

And also I tested with the original Arduino button code, only hanged the pin mode to INPUT_PULLUP, and it worked, so the switch is good. There is something wrong with my code. But I don't know what

I couldn't get the code to compile. 'ledPin' is not defined and the line after that is missing a semicolon.

Hi everyone! I changed the middle pin to gnd instead of 5v, and now it works. And sorry ToddL1962, that's my bad. I didn't copy the rest. I have the ledpin on 13

2 ways to connect a SPDT switch to a digital input.