rw950431:
The code you posted doesnt appear to have any pushbutton functionality. It would be useful to include the code you had tried..The basic scheme is pretty simple :
// this doesnt take into account switch bounce
current_button=digitalRead(BUTTON_PIN);
if (current_button!=previous_button) {
if (current_button==HIGH) {
// button is pressed
if (sensor_state==HIGH) {
// was HIGH, going low so turn off sensor, LEDS etc
sensor_state = LOW;
} else {
// was LOW, going high so turn on sensor, LEDS ect
sensor_state = HIGH;
};
// update button state so we dont trigger until it changes again.
previous_button=current_button;
};
But you may need to account for [Switch Bounce](https://www.arduino.cc/en/Tutorial/Debounce)
Hey rw and thank for the answer!
Here is the code with push-button function included
// set variables
int sensorVal;
int heartBeat = 0;
int blinkingMIN = 0;
int blinkingMAX = 0;
unsigned long timer1;
unsigned long timer2;
unsigned long timer3;
unsigned long timer4;
const int sensorPin = A0; // the number of pin for the temperature sensor (analog input)
const float roomTemp = 21.0; // room temperature in Celsius
float temperature;
int button = 11;
int tempSensorEnabled = 0;
boolean previousButtonState = LOW;
boolean buttonState = LOW;
void setup() {
pinMode(button, INPUT);
pinMode(sensorPin, OUTPUT);
// open a serial connection to display values
Serial.begin(9600);
// set the LED pins as outputs
pinMode(13, OUTPUT);
for (int pinNumber = 2; pinNumber < 8; pinNumber++) {
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
//LED test at startup
digitalWrite(2,HIGH);
delay(100);
digitalWrite(3,HIGH);
delay(100);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,HIGH);
delay(100);
digitalWrite(6,HIGH);
delay(100);
digitalWrite(7,HIGH);
delay(500);
digitalWrite(7,LOW);
digitalWrite(6,LOW);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
digitalWrite(3,LOW);
digitalWrite(2,LOW);
delay(500);
}
void loop() {
buttonState = digitalRead(button);
if(previousButtonState != buttonState && buttonState == HIGH){
tempSensorEnabled = !tempSensorEnabled;
}
if(tempSensorEnabled == 1){
digitalWrite(sensorPin,HIGH);
}
else{
digitalWrite(sensorPin,LOW);
}
previousButtonState = buttonState;
// Make the LED blink
if (millis() - timer1 > 500){
heartBeat = !heartBeat;
digitalWrite (13, heartBeat);
timer1 = millis();
}
// update sensor value at 1Hz and print the values to serial monitor
if(millis() - timer2 > 1000) {
sensorVal = analogRead(sensorPin);
temperature = (5.0 * sensorVal * 100.0) / 1024;
Serial.print("degrees C: ");
Serial.println(temperature);
timer2 = millis();
}
// if the current temperature is lower than the roomTemp: turn off all LEDs and blink the LED in pin 2 -> temperature under minimum value
if (temperature < roomTemp + 2) {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
// Make the LED blink
if (millis() - timer3 > 500){
blinkingMIN = !blinkingMIN;
digitalWrite (2, blinkingMIN);
timer3 = millis();
}
}
// if the temperature rises 2-3 degrees: turn an LED on
else if (temperature >= roomTemp + 2 && temperature < roomTemp + 3) {
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
// if the temperature rises 3-4 degrees: turn a second LED on
else if (temperature >= roomTemp + 3 && temperature < roomTemp + 4) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(3, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
// if the temperature rises 4-5 degrees: turn a third LED on
else if (temperature >= roomTemp + 4 && temperature < roomTemp + 5) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
// if the temperature rises 5-6 degrees: turn a fourth LED on
else if (temperature >= roomTemp + 5 && temperature < roomTemp + 6) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
// if the temperature rises 6-7 degrees, turn a fifth LED on
else if (temperature >= roomTemp + 6 && temperature < roomTemp + 7) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
}
// if the temperature goes 7 degrees over the roomTemp: turn on all LEDs and blink the led in pin 7 -> maximum value has been crossed
else if (temperature >= roomTemp + 7) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
// Make the LED blink
if (millis() - timer4 > 500) {
blinkingMAX = !blinkingMAX;
digitalWrite (7, blinkingMAX);
timer4 = millis();
}
}
}
Problem is that when the button is not pressed, serial monitor reads the sensor value for 15 degrees C. When the button is pressed again the value is 499 degrees C until I press the button again and it jumps back to 15.
Any idea why that happens?