how to make setpoint with push button

I want to create a setpoint for the temperature sensor by using two push buttons, 1 push button for increment, another push button for decrement. For example, now the temperature is 24 Celsius degree. I want to set the temperature to 20 Celsius degree, where after reaching the desired temperature the control valve will receive an order. the question is how to make a program to change the value with pushbutton? or create a setpoint with a pushbutton?

Hello
You may take a view into the IDE examples as start point.

Once you have understood what's required to handle a button and how to wire it to your arduino, there are many button libraries to make your life easier.

Look at the state change detection example and my state change with active low switch tutorial.

thankyouu, Previously I understood the wiring, and the system I made, but I'm just confused about changing the constant to something that can be changed at any time

Here is example code for changing a setpoint with up and a down pushbutton switches.

/*
   The circuit:
   - pushbutton attached to pin 2 from ground
   - the internal pullup on pin 2 is enabled
   - pushbutton attached to pin 3 from ground
   - the internal pullup on pin 3 is enabled  
*/

// these constants won't change:
const byte  upButtonPin = 2;
const byte  downButtonPin = 3;

// Variables will change:
int setpoint = 0;
bool setpointChanged = false;

void setup()
{
   // initialize the button pins as a input with internal pullup:
   pinMode(upButtonPin, INPUT_PULLUP);
   pinMode(downButtonPin, INPUT_PULLUP);
   // initialize serial communication:
   Serial.begin(9600);
   Serial.println("setpoint change with 2 buttons example\n\n");
}


void loop()
{
   checkUpButton();
   checkDownButton();
   if (setpointChanged == true)
   {
      Serial.print("new setpoint = ");
      Serial.println(setpoint);
      setpointChanged = false;
   }
}

void checkUpButton()
{
   boolean buttonState = 0;         // current state of the button
   static boolean lastButtonState = 0;     // previous state of the button
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check 20 times a second
   if (millis() - timer >= interval)
   {
      timer = millis();

      // read the pushbutton input pin:
      buttonState = digitalRead(upButtonPin); 

      // compare the buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         // if the state has changed, increment the counter
         if (buttonState == LOW)
         {
            // if the current state is LOW then the button went from off to on:
            setpoint++;
            setpointChanged = true;
         }
         // save the current state as the last state, for next time through the loop
         lastButtonState = buttonState;
      }
   }
}

void checkDownButton()
{
   boolean buttonState = 0;         // current state of the button
   static boolean lastButtonState = 0;     // previous state of the button
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check 20 times a second
   if (millis() - timer >= interval)
   {
      timer = millis();

      // read the pushbutton input pin:
      buttonState = digitalRead(downButtonPin);

      // compare the buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         // if the state has changed, increment the counter
         if (buttonState == LOW)
         {
            // if the current state is LOW then the button went from off to on:
            setpoint--;
            setpointChanged = true;
         }
         // save the current state as the last state, for next time through the loop
         lastButtonState = buttonState;
      }
   }
}

Start by not declaring the setpoint variable as a const

Hello
I´m a great friend of array´s and objects.

//BLOCK COMMENT
// https://forum.arduino.cc/t/how-to-make-setpoint-with-push-button/893568
#define ProjectName "how to make setpoint with push button"
// CONSTANT DEFINITION
// you may need to change these constants to your hardware
const byte Input_[] {A0, A1};
const byte Output_[] {2};
// VARIABLE DECLARATION
enum {One, Two, Three, Four};
enum {Value, Min, Max, Trigger};
int setPoint[] {0, 0, 30, 24};
enum {Up, Down};
struct BUTTON { // declare an object 
  int   name;   // member name  
  byte  pin;	 // pin address		
  bool  state;	 // and state 
} buttons [] {  // make two button objects
  {Up, Input_[One], 1}, // state =1 due to pinMode = INPUT_PULLUP
  {Down, Input_[Two], 1},
};
// FUNCTIONS
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);
  for (auto Input : Input_) pinMode(Input, INPUT_PULLUP);
  for (auto Output : Output_) pinMode(Output, OUTPUT);
}
void loop () {
  unsigned long currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2); // make heartbeat 
  for (auto &button : buttons) { 
    bool stateNew = digitalRead(button.pin); // read state 
    if (button.state != stateNew) {			 // state changed ? 	
      button.state = stateNew;			// save new state 	
      if (!stateNew) {  			// is button pressed ?
        switch (button.name) {			// which button is pressed ?
          case Up:
            setPoint[Value]++;
            if (setPoint[Value] > setPoint[Max]) setPoint[Value] = setPoint[Max];
            break;
          case Down:
            setPoint[Value]--;
            if (setPoint[Value] < setPoint[Min]) setPoint[Value] = setPoint[Min];
            break;
        }
        digitalWrite(Output_[One], setPoint[Value] >= setPoint[Trigger]); // male output 
		Serial.println(setPoint[Value]);
      }
    }
  }
  delay (20); // bad delay to debounce :) 
}
Have fun and enjoy the  coding 

Thankyouuu!!!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.