Problem in using button to select option

Hi, Im having some problem with my coding in using button to select between 3 sensors.

my project is that I want to build a system which will vary fan speed based on humidity and temperature sensors. So in the project, I want to make so that the user can choose what kind of variable(sensor) that they want to use.

my 1st option: only temperature sensor
2nd option: only humidity sensor
3rd option : both humidity and temperature sensor

the sensor that I used here is DHT22

#include "DHT.h"           // Including DHT22 library
#include "LiquidCrystal.h" // Including LCD library

#define DHTPIN 6      // Declaring pin 6 for communicating to DHT22 sensor
#define DHTTYPE DHT22 // Declaring the type of DHT sensor we are using (DHT22 or DHT11)

DHT dht(DHTPIN, DHTTYPE);          // Declaring DHT connection and type
LiquidCrystal lcd(12,11,10,9,8,7); // Declaring LCD connections

int Motor_Pin1 = 4;  // pin 2 on L293D
int Motor_Pin2 = 3;  // pin 7 on L293D
int Enable = 5;      // pin 1 on L293D
int val; 
int button = 2; //button input on arduino pin 2
int count=0;
int total=0; 

void setup() {
  pinMode(Motor_Pin1, OUTPUT);  
  pinMode(Motor_Pin2, OUTPUT);
  pinMode(Enable, OUTPUT);
  pinMode(button, INPUT);
  lcd.begin(16,2); // Initializes the 16x2 LCD
  dht.begin();     // Initializes DHT sensor
}

void loop() {
  lcd.clear();                        // Clear LCD

  float temp = dht.readTemperature(); // Reading the temperature in Celsius
  float hum = dht.readHumidity (); //Reading the humidity

  if (isnan(temp)) {                  // Validating received data
    lcd.print("Failed to read");
    delay(1000);
    return;
  }
  val=digitalRead(button);
  if (val==HIGH){
    total=count++;
    delay (250);
  }
  if(total>=3){
    total=0;  
  }
  
  lcd.setCursor(0,0);

  if(total=0){{
  lcd.print(total);
  lcd.print(" T:"); 
  lcd.print(temp);      // Writing temperature in the first row
  lcd.print("C");
  lcd.setCursor(0,1);}
  
    if(temp <25 ) {                 // If the temperature less than 25
    analogWrite(Enable,0);        // 0% PWM duty cycle
    lcd.print("Fan OFF ");      
    delay(100);
  }
    else if(temp>=25 & temp<30) {      // If the temperature is between 25 & 30
    analogWrite(Enable, 77);         // 30% of maximum duty cycle value (255). 
    lcd.print("Fan Speed: 30% ");
    delay(100);
  }
    else if(temp>=30 & temp<35) {      // If the temperature is between 30 & 35
    analogWrite(Enable, 153);        // 60% of maximum duty cycle value (255).
    lcd.print("Fan Speed: 60% ");
    delay(100);
  }
    else if(temp>=35) {                // If the temperature is above 35
    analogWrite(Enable, 255);        // 100% duty cycle
    lcd.print("Fan Speed: 100%   ");
    delay(100);
  }
  }
  else if(total=1){{
  lcd.print(total);
  lcd.print(" H:");
  lcd.print(hum);
  lcd.print("%");
  lcd.setCursor(0,1);}
  
    if(hum>=85) {                // If the temperature is above 35
    analogWrite(Enable, 255);        // 100% duty cycle
    lcd.print("Fan Speed: 100%   ");
    delay(100);
  }
    else if(hum>=80 & hum<85) {      // If the temperature is between 30 & 35
    analogWrite(Enable, 153);        // 60% of maximum duty cycle value (255).
    lcd.print("Fan Speed: 60% ");
    delay(100);
  }
    else if(hum<80) {                 // If the temperature less than 25
    analogWrite(Enable,0);        // 0% PWM duty cycle
    lcd.print("Fan OFF ");      
    delay(100);
  }
  }
  
  else if(total=2){{
  lcd.print(total);
  lcd.print("T:"); 
  lcd.print(temp);      // Writing temperature in the first row
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("H:");
  lcd.print(hum);}
  
   if((temp>=35)|(hum>=85)) {                // If the temperature is above 35
    analogWrite(Enable, 255);        // 100% duty cycle
    lcd.print(" F:100%");
    delay(100);
  }
  else if((temp>=30 & temp<35)|(hum>=80 & hum<85)) {      // If the temperature is between 30 & 35
    analogWrite(Enable, 153);        // 60% of maximum duty cycle value (255).
    lcd.print(" F:60%");
    delay(100);
  }
  else if(temp>=25 & temp<30) {      // If the temperature is between 25 & 30
    analogWrite(Enable, 77);         // 30% of maximum duty cycle value (255). 
    lcd.print("F:30%");
    delay(100);
  }
  else if(temp <25 | hum <80 ) {                 // If the temperature less than 25
    analogWrite(Enable,0);        // 0% PWM duty cycle
    lcd.print("F:OFF");      
    delay(100);
  }}

  digitalWrite(Motor_Pin1, LOW);  // To drive the motor in a particular direction
  digitalWrite(Motor_Pin2, HIGH); // To drive the motor in a particular direction

  delay(2000); // 2 seconds delay
}

Im having some problem

And what is that problem?

+1 Karma for using code tags in your first post.

Please Auto-Format your code before you post it next time.

I saw some errors. You are using "&" and "|" when you should be using "&&" and "||". Read the reference section in the menu to understand the difference.

owh sorry... I forgot to put the question haha. When I run my coding, the LCD will only display option 2 which is the humidity sensor option. No matter how many times I push the button, it still the same. I want it to move to option 3 when I push the button and back to option 1 when I pushed the button again. Thanks in advance

PaulRB:
And what is that problem?

Not knowing what the problem is doesn't prevent a quick scan of the code, surely. Two things are evident, with just a cursory glance, even without knowing what your problem is.

  • Some of your "if"s have only an = but should have ==
  • To count button presses, you can't do it the way you have, since that will increase the count while the button is pressed, not when it becomes pressed. You might have a look at the State Change Detection example.

PaulRB:
I saw some errors. You are using "&" and "|" when you should be using "&&" and "||". Read the reference section in the menu to understand the difference.

Karma-- (this forum really nees a karma-- option) for editing something significant into your answer without a) marking it as an edit and/or b) noting in a later post that you had made such a change. Since OP's reply is timed before your edit, there's a chance that he or she might not see it.

And it didn't, as you can surely see from reply #1.

Yes but I posted before you edited.... Look at the times.

At the time I posted all you had said was "And what is that problem?"

edit: The quote here is from a post that was subsequently deleted. That's even worse than an un-marked edit. Really really bad behaviour on any forum.

Hal_Posoyan:
Karma-- (this forum really nees a karma-- option) for editing something significant into your answer without a) marking it as an edit and/or b) noting in a later post that you had made such a change. Since OP's reply is timed before your edit, there's a chance that he or she might not see it.

It really doesn't. That would just lead to petty Karma wars.

But I am guilty of a lot of editing. I try to remember to refresh the page before I do that, so I can see if any more posts have been made. And normally the forum warns you before you post it there have been, although that may not work with "quick edit".

The important thing is to try to help the OP, and keep our own egos under control as much as we are able.

the LCD will only display option 2

I think that may be something to do with this line:

    total=count++;

What was the idea behind having two variables "count" and "total"? Wouldn't a single variable with a name like "sensorOption" be clearer?

The problem could be that although "total" gets checked to see if it has hit 3 and if so gets reset to zero. But it is "count" that gets incremented, and that never gets checked or reset.

Also, "count++" increments count after its value had been assigned to total. Putting "++" after a variable increments that variable's value after is has been used ("post-increment"). Whereas "++count" would increment the variable and then use the updated value ("pre-increment"). But I think the problem here is using 2 variables where only one is needed.

  if(total=0){{

And as Hal_Posoyan pointed out, "=" means assignment, "==" means compare, so this line resets total to zero every time.

Hal_Posoyan:
Really really bad behaviour on any forum.

So is getting so far off-topic.

The next post will directly address the original poster or be from the original poster.

PaulRB:
The important thing is to try to help the OP, and keep our own egos under control as much as we are able.

Well said. Karma to you.