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”);
}
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...
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");
*/
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.