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


hello there, i'm new arduino , use project of mine. code posted below supposed write data dht22 sensor serial every 1000 milliseconds. has check if of conditions in pullupin() function met , if trigger general alarm function if not has turn off general alarm, problem i'm having output of general alarm not consistent , flickers alot.
any appreciated.


code: [select]
#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 read 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 same previousmillis in getalarmon , main loop() should have separate ones.


Arduino Forum > Using Arduino > Programming Questions > I need some help with timers that are interfering with each other.


arduino

Comments

Popular posts from this blog

Valutazione Template - Joomla! Forum - community, help and support

SD Datastring Convention

First use of Arduino Uno : avrdude error on Blink uploading