I need some help with timers that are interfering with each other.

Hello there, I'm very new to Arduino and I could really use some help with this project of mine. The code posted below is supposed to write the data from a DHT22 sensor to the serial every 1000 milliseconds. It also has to check if any of the conditions in the pullupIn() function are met and if so to trigger the general alarm function if not it has to turn off the general alarm, the problem I'm having is that the output of the general alarm is not consistent and flickers alot.
Any help is appreciated.

#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
unsigned long previousMillis = 0;
const long interval = 1000;

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  Serial.begin(9600);
  dht.begin();

}

void loop() {
  pullupIn();
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
  previousMillis = currentMillis;
  dhtSerial(); 
  }
}

void dhtSerial() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  Serial.print("Hum: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temp: ");
  Serial.print(t);
  Serial.println(" *C");
}

void pullupIn(){
  
  int fireDetect = digitalRead(2);
  int overheatStat = digitalRead(3);
  int airflowSwitch = digitalRead(4);
  
  if (fireDetect == LOW){
    gAlarmOn();
    }else{
      gAlarmOff();
      }
  if (overheatStat == HIGH){
    gAlarmOn();
    }else{
      gAlarmOff();
      }
  if (airflowSwitch == HIGH){
    gAlarmOn();
    }else{
      gAlarmOff();
      }
}

void gAlarmOn(){
  digitalWrite(13, HIGH);
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
  previousMillis = currentMillis;
  Serial.println("General Alarm");
  } 
}
  
void gAlarmOff(){
  digitalWrite(13, LOW);   
}

you use the same previousMillis in getAlarmOn and the main loop() you probably should have separate ones.

Ok, so I tried that but unfortunately it did not work, the blinking is not random anymore but it still blinks.
Thanks for the quick reply.

One reason might be that you have three conditions in pullupIn.

If e.g. fireDetect is LOW, it will switch the alarm on. If at the same time e.g. overheatStat is HIGH, it will switch it off.

// if any alarm condition
if(fireDetect == LOW || overheatStat == LOW || airflowSwitch == LOW)
{
  // alarm on
}

// if no alarm condition
if(fireDetect == HIGH && overheatStat == HIGH && airflowSwitch == HIGH)
{
  // alarm off
}

This is the code at the moment, the LED is not flickering but turns on and stays on indefinitely.

#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
const long interval = 1000;

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  dht.begin();

}

void loop() {
  pullupIn();
  unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >= interval) {
  previousMillis2 = currentMillis2;
  dhtSerial();
  }
}

void pullupIn(){
  
  int fireDetect = digitalRead(2);
  int overheatStat = digitalRead(3);
  int airflowSwitch = digitalRead(4);
  
  if (fireDetect == LOW || overheatStat == HIGH || airflowSwitch == HIGH){
     gAlarmOn();     
  }
  if (fireDetect == LOW && overheatStat == HIGH && airflowSwitch == HIGH){
    gAlarmOff();
  }
}


void dhtSerial() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  Serial.print("Hum: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temp: ");
  Serial.print(t);
  Serial.println(" *C");
}

void gAlarmOn(){  
  unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >= interval) {
  previousMillis2 = currentMillis2;
  digitalWrite(13, HIGH);
  Serial.println("General Alarm!");
  } 
}
  
void gAlarmOff(){
  unsigned long currentMillis3 = millis();
  if (currentMillis3 - previousMillis3 >= interval) {
  previousMillis3 = currentMillis3;
  digitalWrite(13, LOW);
  }
}

I just saw that did this

  if (fireDetect == LOW || overheatStat == HIGH || airflowSwitch == HIGH){
     gAlarmOn();     
  }
  if (fireDetect == HIGH && overheatStat == LOW && airflowSwitch == LOW){
    gAlarmOff();
  }
}

So if fixed that but now the serial stops showing the temperature as soon as the general alarm is triggered.

Untitled.png

Please post your new code (complete) so we can see every change that you made.

This is not how currentMillis is intended to be used

void gAlarmOn(){ 
  unsigned long currentMillis2 = millis();
  if (currentMillis2

The idea of using currentMillis is to get a single value of millis() that is used for every test in that iteration of loop(). And for that reason you should have currentMillis = millis() in loop() and nowhere else. And currentMillis should be a global variable.

If you don't want to do that then just use millis() in your functions

if (millis() ....

...R

Thanks everyone, code is working quite nicely :slight_smile:

#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
unsigned long previousMillis = 0;
const long interval = 1000;


void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  dht.begin();

}

void loop() {
unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    int fireDetect = digitalRead(2);
    int overheatStat = digitalRead(3);
    int airflowSwitch = digitalRead(4);
  
  if (fireDetect == LOW || overheatStat == HIGH || airflowSwitch == HIGH){
    digitalWrite(13, HIGH);
    Serial.println("gAlarm");
  }
  if (fireDetect == HIGH && overheatStat == LOW && airflowSwitch == LOW)
    digitalWrite(13, LOW);
    
float h = dht.readHumidity();
float t = dht.readTemperature();
    
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;  
  }  
  Serial.print("Hum: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temp: ");
  Serial.print(t);
  Serial.println(" *C");
  }
}