Measure two low frequency digital signals while talking to BMP180 and DHT22

Hi all,

I’m planning to write some code that will run a weather station - reporting temperature, pressure, humidity, wind speed, wind direction and rainfall via USB (serial data to labview).

Here’s the circuit diagram for reference:

So far, I have modified the example code from the BMP180 pressure sensor and the DHT22 temp/humidity sensor to join them into a single program:

#include "DHT.h"
#include "SFE_BMP180.h"
#include <Wire.h>

#define ALTITUDE 70.0
#define DHTPIN 2
#define DHTTYPE DHT22

SFE_BMP180 pressure; // create an SFE_BMP180 object called "pressure":
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino

void setup() {
  Serial.begin(9600); 
  Serial.println("REBOOT");
  
  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    Serial.println("BMP180 init fail\n\n");
    while(1); // Pause forever.
  }
  
  dht.begin();
}

void loop()
{
  char status;
  double T,P,p0,a;

  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);
    
    status = pressure.getTemperature(T); // Measure temp for pressure reading
    if (status != 0)
    {
      status = pressure.startPressure(3); // Measure pressure (x3 accuracy)
      if (status != 0)
      {
        delay(status); // Wait for the measurement to complete:

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          p0 = pressure.sealevel(P,ALTITUDE);
          Serial.print("p");
          Serial.println(p0,2);
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
  
  // Start DHT22 readings (takes about 250 milliseconds each)
  float h = dht.readHumidity(); // Humidity
  float t = dht.readTemperature(); // Temperature
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  // Print humidity and temp data
  Serial.print("h"); 
  Serial.println(h);
  Serial.print("t"); 
  Serial.println(t);
  
  delay(2000);  // Pause for 2 seconds.
}

This gives the following output:

This is good. However now I would like to add the readings for the wind speed sensor and the rainfall sensor. These are reed switches, which will simply give a high or low voltage, switching at each ‘tip’ of the tipping bucket rain sensor, and each rotation of the wind speed sensor. Thus the rainfall signal will be very slow (probably less than 1Hz even when raining) and the wind speed signal will be roughly 0-5Hz. Measuring the analogue output of the wind direction sensor should be trivial.

What would be the best way of adding in the rainfall and wind speed signals?

Thanks :smiley:

What would be the best way of adding in the rainfall and wind speed signals?

Get rid of the calls to "delay()".
Have a look at Robin2's example code, or blink without delay.

EDIT 2: See lower edit to find where my code below is incorrect.

Here’s the code I’m currently working on to get the wind pulses per 10 seconds:

// ---------CONSTANTS---------
#define WINDPIN 4

// ---------VARIABLES---------
int windCounter = 0;
int windState = 1;
int lastWindState = 1;
int start;

void setup() 
{
  // initialize the button pin as a input:
  pinMode(WINDPIN, INPUT);
  // initialize the LED as an output:
  Serial.begin(9600);
}

void loop() 
{
  start = millis(); // take the start time
  windCounter = 0;
  // do the following loop for 10000ms = 10s
  while (millis() - start < 10000)
  {
    // read the pushbutton input pin:
    windState = digitalRead(WINDPIN);

    // compare the windState to its previous state
    if (windState != lastWindState) 
    {
      // if the state has changed, increment the counter
      if (windState == HIGH) 
      {
        windCounter++;
      }
    }
    // save the current state as the last state, 
    //for next time through the loop
    lastWindState = windState;   
  }
  Serial.print("w");
  Serial.println(windCounter);
}

This runs very well for a few loops, then fails - after four loops it completely skips the while loop. Any ideas? I don’t think that the milis is overflowing, that shouldn’t happen so quickly.

Here’s an example output (first four lines are correct then it infinitely spams w0 as fast as it can)

w25
w36
w10
w4
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0
w0

EDIT 2:
I fixed the code above by changing the line “int start;” to “unsigned long start;” - the “start” variable was overflowing.