Integrating a on/off button into my project

hi,

i am looking to add a button into my project that will turn everything on or off.

i have 3 leds, fan and temperature sensor that runs the leds and fan. i have the code to run all of these but when trying to add a button to turn everything off, nothing happens.

all i want is to press a button and everything will stop working then when the button is pressed again everything will work again as before.

hope you can help
thanks

I have attached the button set up i have at the moment.

Here is my code where everything works apart from the button coding.

int temperaturePin = 0;
int led_red = 13;
int led_orange = 12;
int led_green = 10;
int fan = 9;
int button = 2;
int buttonValue = 0;

void setup()
{
  
   Serial.begin(9600);
  pinMode(led_red, OUTPUT);
  pinMode(led_orange, OUTPUT); 
  pinMode(led_green, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(button,INPUT);
  
}
void loop(){
  
  
  
  float temperature = getVoltage(temperaturePin);
 temperature = (temperature - .5) * 100;
  
  
  Serial.println( temperature );
  delay(1000);
  
  buttonValue = digitalRead(button);
  if(buttonValue==LOW)
  {
    digitalWrite(led_red,LOW);
    digitalWrite(led_orange,LOW);
    digitalWrite(led_green,LOW);
    analogWrite(fan, 0);
  }
  
  
  
  
  
  
  if (temperature <= 20.00)
  {
    digitalWrite(led_red, HIGH); 
    digitalWrite(led_orange, LOW); 
    digitalWrite(led_green, LOW);
    analogWrite(fan, 0);
   
}
  if (temperature > 20.00 && temperature < 25.00)
  {
    digitalWrite(led_red, LOW); 
    digitalWrite(led_orange, HIGH); 
    digitalWrite(led_green, LOW);
    analogWrite(fan, 150);
    
}
 if (temperature >= 25.00)
  {  
    digitalWrite(led_red, LOW); 
    digitalWrite(led_orange, LOW); 
    digitalWrite(led_green, HIGH);
    analogWrite(fan, 255);
  
}
   
  
  
}
float getVoltage(int pin){
  return (analogRead(pin) * .004882814);
}

button not working fritzing.pdf (155 KB)

if(buttonValue==LOW)
  {
    digitalWrite(led_red,LOW);
    digitalWrite(led_orange,LOW);
    digitalWrite(led_green,LOW);
    analogWrite(fan, 0);
  }

Your code runs in sequence. It runs the lines in the order you specify. Here you test that the button is LOW and turn things off. But what does the very next line of the program do? It checks that temperature variable and starts turning things back on.

Maybe put the rest of the program in an else block so it doesn't run if that if is true.