pushbutton setpoint for temp controller

Hi Folks

I’m working on a temp controller and want to select the desire temperature setpoint from 5 differents pushbutton. Each of them need to be a different temperature setpoint. How to make the Value ‘‘X’’ change depending of wich button have been push. Here the code. Thanks!:

#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!

//TMP36 Pin Variables
int tempPin = 1;
int tempReading;
int ledPin=8;
int buttonPin1 = 2;
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 5;
int buttonPin5 = 6;
int delayTime=500;

int X;
float desiredTempC = X; // which temperature to maintain

void setup(void) {
// We’ll send debugging information via the Serial monitor
Serial.begin(9600);
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}

void loop(void) {

tempReading = analogRead(tempPin);

// converting that reading to voltage, which is based off the reference voltage
float voltage = tempReading * aref_voltage;
voltage /= 1024.0;

// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ;
Serial.print(temperatureC); Serial.println(" degrees C");

/* now convert to Fahrenheight
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF); Serial.println(" degrees F");
*/

if (digitalRead(buttonPin1) == LOW) {
int(24);
Serial.println(“SELECTION 24”);
}
if (digitalRead(buttonPin2) == HIGH) {
float (25);
Serial.println(“SELECTION 25”);
}
if (digitalRead(buttonPin3) == HIGH) {
float (26);
Serial.println(“SELECTION 26”);
}
if (digitalRead(buttonPin4) == HIGH) {
float (27);
Serial.println(“SELECTION 27”);
}
if (digitalRead(buttonPin5) == HIGH) {
float (X=28);
Serial.println(“SELECTION 28”);
}

if(temperatureC < desiredTempC){
digitalWrite(ledPin, HIGH)
;Serial.println(“Heater ON”);
delay(delayTime);
}
if(temperatureC > desiredTempC+2){
digitalWrite(ledPin, LOW)
;Serial.println(“Heater OFF”);
delay(delayTime);
}
delay(1000);
}

Simply put it inside an if statement, when a specific button is pressed then change the value of X inside the statement.. And please use code tags so we can read your code easily...

    int(24);I assume that you meant to set a variable to a value with this and similar lines.

You nearly managed it with  float (X=28); but why the attempt to cast it to float and why does the value assigned to X never get used in the program ?

Thanks zaxarias! I modify my curly braces and that works!

Clodaille:
Thanks zaxarias! I modify my curly braces and that works!

Can we please see your working code ? There was much more than missing braces wrong with it.

Here’s the modified code. I will modify it again for capacitive touch interface instead of pushbutton… work in progress…

#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!

//TMP36 Pin Variables
int tempPin = 1;
int tempReading;
int ledPin=8;
int buttonPin1 = 2;
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 5;
int buttonPin5 = 6;
int delayTime=500;

float x=24; // which temperature to maintain

void setup(void) {
// We’ll send debugging information via the Serial monitor
Serial.begin(9600);
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}

void loop(void) {

tempReading = analogRead(tempPin);

// converting that reading to voltage, which is based off the reference voltage
float voltage = tempReading * aref_voltage;
voltage /= 1024.0;

// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ;
Serial.print(temperatureC); Serial.println(" degrees C");

/* now convert to Fahrenheight
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF); Serial.println(" degrees F");
*/

if (digitalRead(buttonPin1) == HIGH){
x=29; Serial.println(“SELECTION 29”);
}
if (digitalRead(buttonPin2) == HIGH){
x=28; Serial.println(“SELECTION 28”);
}
if (digitalRead(buttonPin3) == HIGH){
x=27; Serial.println(“SELECTION 27”);
}
if (digitalRead(buttonPin4) == HIGH){
x=26; Serial.println(“SELECTION 26”);
}
if (digitalRead(buttonPin5) == HIGH){
x=25; Serial.println(“SELECTION 25”);
}
if(temperatureC < x){
digitalWrite(ledPin, HIGH)
;Serial.println(“Heater ON”);
delay(delayTime);
}
if(temperatureC > x+2){
digitalWrite(ledPin, LOW)
;Serial.println(“Heater OFF”);
delay(delayTime);
}
delay(1000);
}

Thanks to you UKHeliBob too, I realised that I was using 2 differents value for the same things.

To make your code easier to read and maintain can I suggest that you format it differently.

For instance, instead of

if(temperatureC < x){
    digitalWrite(ledPin, HIGH)
    ;Serial.println("Heater ON");
    delay(delayTime);
   }

like this

if(temperatureC < x)
{
  digitalWrite(ledPin, HIGH);
  Serial.println("Heater ON");
  delay(delayTime);
}

The block of code executed when the condition is true is easier to identify and the extent of each statement is obvious because of the terminating semi-colon on the same line. Above all format your code with a consistent style.

Also consider using arrays to hold button pin numbers in your program. The reading of the buttons and setting the value of x could then be reduced to a few lines by using a for loop instead of multiple ifs.