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;
}
}
}
}