PushButton Help

Hi. I’m new to arduino and have just written my first simple program with YouTube help and other for blower controll.

Now I have problem with pushbuttons.

If I press first one (left) - temp. goes UP, but in the code it must go DOWN. (buttonPin1)
If I press second (right) - temp. goes DOWN in the code must go UP. (buttonPin2)

How could I solve that problem??

Thanks

code for pushbutton;

/*********************************************************************************/

void TempUp()
{
int value2 = debouncer2.read();
value2 = digitalRead(buttonPin2);
if (value2 == HIGH) {
delay(500);
value2 = digitalRead(buttonPin2);
if (value2 == HIGH) {
a = a + 1;
}
}
}

/********************************************************************************/

void TempDown()
{
int value1 = debouncer1.read();
value1 = digitalRead(buttonPin1);
if (value1 == HIGH) {
delay(500);
value1 = digitalRead(buttonPin1);
if (value1 == HIGH) {
a = a - 1;
}
}
}

pushbutton.jpg

To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.

Note:
int value2 = debouncer2.read() ; // you do this then
value2 = digitalRead(buttonPin2) ; // you throw it away then you do this, why?

.

Please post your whole program and use code tags when you do by using the icon on the left above the editor.

You appear to be using a debounce library but ignore what it returns. How are your pushbuttons wired ? With the code as it is what do you expect to happen if one of the buttons is held down ?

My code. Just want to select the temp. point when should blower shutdown. The code is working.

[code]
#include <Bounce2.h>
#include <max6675.h>
#include <LiquidCrystal_I2C.h>
#include <LCD.h>
#include <Wire.h>

int relay = 8;
int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
int buttonPin1 = 10;
int buttonPin2 = 12;
int a = 40.00;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 3;
int gndPin = 2;

#define I2C_ADDR    0x27    //Naslov za LCD zaslon (0x27) 
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C  lcd(0x27,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

uint8_t degree[8]  = {140,146,146,140,128,128,128,128}; // ustvari simbol (°)

Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();

/*********************************************************************************/

void setup() 
{
  // definicija pinov za MAX6675 
  pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);  //določil izhod kot +5V
  pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);   //določil izhod kot GND
  pinMode(relay, OUTPUT);  //rele kot izhod
  digitalWrite(relay, HIGH); //rele je izklopljen
  pinMode(buttonPin1, INPUT_PULLUP); //tipka 1 je temp. DOL
  debouncer1.attach(buttonPin1);
  debouncer1.interval(5);
  pinMode(buttonPin2, INPUT_PULLUP); //tipka 2 je temp GOR
  debouncer2.attach(buttonPin2);
  debouncer2.interval(5);
  
  delay(1000);
  
  lcd.begin(20, 4);
  lcd.createChar(0, degree);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); // osvetlitev zaslona ON
  lcd.setBacklight(HIGH); 
}

/*********************************************************************************/

void loop() 
{
  debouncer1.update();
  debouncer2.update();
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("BBQ Temp.:");
  lcd.setCursor(11,0);
  lcd.print(thermocouple.readCelsius());
#if ARDUINO >=100
  lcd.write((byte)0);
#else
  lcd.print(0, BYTE);
#endif
  lcd.print("C ");
  lcd.setCursor(0,1);
  lcd.print("Set Temp.:");
  lcd.setCursor(11, 1);
  lcd.print(a);
#if ARDUINO >= 100
  lcd.write((byte)0);
#else
  lcd.print(0, BYTE);
#endif
  lcd.print("C ");
  lcd.setCursor(0,2);
  lcd.print("Blower:");
  
  if (thermocouple.readCelsius() < a)
  {
  digitalWrite(relay, LOW);
  lcd.setCursor(8,2);
  lcd.print("ON");
  }
  else 
  {
  digitalWrite(relay, HIGH);
  lcd.setCursor(8,2);
  lcd.print("OFF");
  }
  TempDown();
  TempUp();
 }
 
/*********************************************************************************/
 
 void TempUp()
 {
  int value2 = debouncer2.read();
  value2 = digitalRead(buttonPin2);
   if (value2 == HIGH) {
     delay(500);
     value2 = digitalRead(buttonPin2);
     if (value2 == HIGH) {
         a = a + 1; 
     }
   }
  }

/********************************************************************************/

  void TempDown()
{
  int value1 = debouncer1.read();
  value1 = digitalRead(buttonPin1);
   if (value1 == HIGH) {
      delay(500);
      value1 = digitalRead(buttonPin1);
      if (value1 == HIGH) {
         a = a - 1; 
     }
   }
}

[/code]

Buttons:

|500x282

  pinMode(buttonPin1, INPUT_PULLUP); //tipka 1 je temp. DOL

Why have you got resistors wired to the buttons when you use the internal pullup resistors

void TempUp()
{
  int value2 = debouncer2.read();  //read a debounced input
  value2 = digitalRead(buttonPin2);  //overwrite the value just read
  if (value2 == HIGH) {  //if the button is not pressed - remember the INPUT_PULLUP ?
    delay(500);  //waste some time
    value2 = digitalRead(buttonPin2);  //read the value again
    if (value2 == HIGH) {  //if the button is still not pressed
      a = a + 1;  //add 1 to the variable
    }
  }
}

These functions are nonsense.

It is impossible to tell how your buttons are wired from your picture. Are the input pins takem HIGH or LOW when the button is pressed and are they normally held HIGH or LOW ?

OK. This is my first time I'm using arduino and everything else. Could you help me to correct things? I'm learning :)

Wire the buttons so that they take the input pins LOW when pressed. Remove the resistor and other wiring to the buttons. Now the pin will be normally held HIGH and will go LOW when pressed.

Change your functions

void TempUp()
{
  int value2 = digitalRead(buttonPin2);
  if (value2 == LOW)  //button is pressed
  {
    a = a + 1;
  }
}

This will still not do what you want because the counter will be incremented when the button [u]is[/u] pressed and what you really need is to detect when it [u]becomes[/u] pressed but try it anyway. You can add the state change detection and debouncing later.

I'm learning :)

If you aren't then you are dead ;)

In #4 move the left side of the resistors to the yellow and blue wires. Or use INPUT_PULLUP pin pinMode.

.

Or use INPUT_PULLUP pin pinMode.

He already is which is probably adding to the problem.

  int value2 = digitalRead(buttonPin2);
  if (value2 == LOW)  //button is pressed

How about some useful names, like upPin and upState, downPin and downState? You see a name like buttonPin2 and all that you know is that it contains the number of a pin that a button is sewn onto. There is NOTHING that assigns a meaning to that "button". upPin contains a pin number, and the meaning of the switch is obvious.