If statement executes but should not

Hi.
I'm new to arduino and trying to write arduino code to control ventilation at home. Will add relays and humidity sensors etc later on but I'm having trouble with if statement.

If i run my code serial monitor all the time prints two lines "vent ON" and vent "OFF" that part of code should not run at all because it's behind several if statements and by my understanding should not execute because criteria is not met without pushing any button. What am I doing wrong?

int vent_on = 0;
int vent_off = 0;

const int vent_on_led =  12;
const int vent_off_led =  8;
const int vent_on_btn = 2;
const int vent_off_btn = 4;

int vent_on_btn_state = 0;
int vent_off_btn_state = 0;
int vent_on_led_state = 0;
int vent_off_led_state = 0;
int vent_on_period = 5000;
int vent_off_period = 5000;

unsigned long time_now_on = 0;
unsigned long time_now_off = 0;


void setup()
{
  pinMode(vent_on_btn, INPUT);
  pinMode(vent_on_btn, INPUT);
  pinMode(vent_on_led, OUTPUT);
  pinMode(vent_off_led, OUTPUT);
  Serial.begin(9600);
}

void loop()
{ 
  //VENT ON================================================= 
  vent_on_btn_state = digitalRead(vent_on_btn);
  if (vent_on_btn_state == HIGH) {
    vent_on_led_state = 1;
    vent_off_led_state = 0;
    vent_on = 1;
    vent_off = 0;
    digitalWrite(vent_on_led, HIGH);
    digitalWrite(vent_off_led, LOW);
  } else {
    if(vent_on = 1){
    if(millis() > time_now_on + vent_on_period){
    time_now_on = millis();
    Serial.println("vent ON");
    digitalWrite(vent_on_led, LOW);
    vent_on = 0;
    }
  } 
  }
  //VENT OFF=================================================
  vent_off_btn_state = digitalRead(vent_off_btn);
  if (vent_off_btn_state == HIGH) {
    vent_on_led_state = 0;
    vent_off_led_state = 1;
    vent_on = 0;
    vent_off = 1;
    digitalWrite(vent_off_led, HIGH);
    digitalWrite(vent_on_led, LOW);
  } else {
    if(vent_off = 1){
    if(millis() > time_now_off + vent_on_period){
    time_now_off = millis();
    Serial.println("vent OFF");
    digitalWrite(vent_off_led, LOW);
    vent_off = 0;
    }
  } 
  }
}

Your post was MOVED to its current location as it is more suitable.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

    if (vent_on = 1)

This is wrong

    if (vent_off = 1)

and this

if (vent_on == 1)

if (vent_off == 1)

@UKHeliBob @red_car almost feel embarrassed about this. thanks

Don't be. We have all done it

As to the code, it would be simpler if the vent_on/off variables were combined into a single boolean variable, then you could write statements like

if (vent_on)  //vent is on
{
  //code here
}
else  //vent is off
{
  //code here
}

Every single time. There should be a forum bot that offers solution if topic contains ‘if’, ‘not’ and ‘work’

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.