Code behaves strange. millis doing weird stuff?

Hi. I'm having some weird issues with code. Looking in serial monitor variables vent_on and vent_off does not get set after pressing button and time that is supposed to be 10 seconds sometimes is 7 seconds sometimes 1second... sometmes when i restart arduino button works ok for first time. Any ideas and suggestions?

#include <dht.h>
dht DHT;
#define DHT22_PIN 9
float hum;
float temp;
int vent_on = 0;
int vent_off = 0;
int vent_on_btn = 2;
int vent_off_btn = 4;
int vent_on_btn_state = 0;
int vent_off_btn_state = 0;
int vent_on_period = 10000;
int vent_off_period = 10000;
unsigned long time_now_on = 0;
unsigned long time_now_off = 0;

void setup()
{
  pinMode(vent_on_btn, INPUT);
  pinMode(vent_off_btn, INPUT);
  digitalWrite(vent_on_btn, HIGH);
  digitalWrite(vent_off_btn, HIGH);
  Serial.begin(9600);
}

void loop()
{ 
  vent_on_btn_state = digitalRead(vent_on_btn);
  if (vent_on_btn_state == LOW) {
    vent_on = 1;
    vent_off = 0;
    Serial.println("vent ON pressed");  
  } else {
    if(vent_on == 1){
      if(millis() >= time_now_on + vent_on_period){
        time_now_on += vent_on_period;
        vent_on = 0;
        Serial.println("vent ON period over");
      }
    } 
  }
  vent_off_btn_state = digitalRead(vent_off_btn);
  if (vent_off_btn_state == LOW) {
    vent_on = 0;
    vent_off = 1;
    Serial.println("vent OFF pressed");
  } else {
    if(vent_off == 1){
      if(millis() >= time_now_off + vent_off_period){
        time_now_off += vent_off_period;
        vent_off = 0;
        Serial.println("vent OFF period over");
      }
    } 
  } 
  
    int chk = DHT.read22(DHT22_PIN);
    hum = DHT.humidity;
    temp= DHT.temperature;
    Serial.print("H: ");
    Serial.print(hum);
    Serial.print(" %, T: ");
    Serial.print(temp);
    Serial.print(" C ; V_off: ");
    Serial.print(vent_off);
    Serial.print(" ; V_on: ");
    Serial.print(vent_on);
    Serial.println(" ");
    if(vent_off == 1 && hum > 75){
      Serial.println("VENT OFF! DO NOTHING");
    }else if(vent_on == 1 && hum > 75){
      Serial.println("VENT ALREADY ON");
    }else if (vent_on == 0 && vent_off == 0 && hum >75){
      Serial.println("TURN ON VENT");
    }
 }

I doubt this is the problem, but to avoid confusion and errors, all times associated with millis() and micros() should be declared unsigned long.

This approach will lead to errors in the long run, when millis() overflows. Always subtract the start time from millis().

      if(millis() >= time_now_on + vent_on_period){
        time_now_on += vent_on_period;

My head exploded right there. Why do you set the pins to input and then you write to them?

[edit]

Ok, this is perfectly ok, as explained below!

That sets INPUT_PULLUP mode, at least on AVR-based Arduinos. That may not work on others.

@OP: which Arduino do you have and how have you wired the buttons.

I'm using button without resistor like in this example Here

Original Arduino Uno r3 and buttons are wired like This

Hello

When the button is pressed, you have to do time_now_on = millis()

and this line time_now_on += vent_on_period; is useless (in this case)

will get back home in few days and test it. thanks

thanks for suggestions. moddified code and it seems to work correctly.... will finetune later but at the moment looks good

#include <dht.h>
dht DHT;
#define DHT22_PIN 11
float hum;
float hum2;
float temp;
int relay_pin= 10;
int vent_on = 0;
int vent_off = 0;
int vent_on_btn = 2;
int vent_off_btn = 4;
int vent_on_btn_state = 0;
int vent_off_btn_state = 0;
unsigned long vent_on_period = 1000000;
unsigned long vent_off_period = 1000000;
unsigned long time_now_on = 0;
unsigned long time_now_off = 0;
int vent_hum = 0;

void setup() {
  pinMode(relay_pin, OUTPUT);
  pinMode(vent_on_btn, INPUT);
  pinMode(vent_off_btn, INPUT);
  digitalWrite(vent_on_btn, HIGH);
  digitalWrite(vent_off_btn, HIGH);
  //Serial.begin(9600);
}

void loop() { 
  vent_on_btn_state = digitalRead(vent_on_btn);
  if (vent_on_btn_state == LOW) {
    time_now_on = millis();
    vent_on = 1;
    vent_off = 0;
    //Serial.println("VENT ON BUTTON PRESSED");  
  } else {
    if(vent_on == 1) {
      if(millis() >= time_now_on + vent_on_period){
        //time_now_on += vent_on_period;
        vent_on = 0;
        //Serial.println("VENT ON PERIOD EXPIRED");
      }
    } 
  }
  if (vent_on == 1) {
    digitalWrite(relay_pin, LOW);
  }
  vent_off_btn_state = digitalRead(vent_off_btn);
  if (vent_off_btn_state == LOW) {
    time_now_off = millis();
    vent_on = 0;
    vent_off = 1;
    //Serial.println("VENT OFF BUTTON PRESSED");
  } else {
    if(vent_off == 1){
      if(millis() >= time_now_off + vent_off_period){
        vent_off = 0;
        //Serial.println("VENT OFF PERIOD EXPIRED");
      }
    } 
  } 
    
    delay(300);
    int chk = DHT.read22(DHT22_PIN);
    hum = DHT.humidity;
    temp= DHT.temperature;
    /*Serial.print("H:");
    Serial.print(hum);
    Serial.print("%,T:");
    Serial.print(temp);
    Serial.print("C;V_off:");
    Serial.print(vent_off);
    Serial.print(";V_on:");
    Serial.print(vent_on);
    Serial.print(";V_hum:");
    Serial.print(vent_hum);
    Serial.println("");
    digitalWrite(relay_pin, HIGH);
    */
    if(vent_off == 1 && hum > 75){
      //Serial.println("VENT OFF PRESSED! DO NOTHING / TURN OFF EVERYTHING");
      digitalWrite(relay_pin, HIGH);
    }  
    if ( vent_on == 0 && vent_off == 0 && hum >75){
      while (hum >65 && vent_off_btn_state == HIGH) {
        vent_off_btn_state = digitalRead(vent_off_btn);
        int chk = DHT.read22(DHT22_PIN);
        hum = DHT.humidity;
        delay(300);
        digitalWrite(relay_pin, LOW);
        //Serial.println("HUMIDITY RELAY ON!");
        //Serial.println(hum);
        }
     } 
}