Combining Arduino sketches

Hi!

I require your help regarding a problem I’m encountering.

I am building a weather station. 3 of 5 parameters I am measuring are wind speed, direction & Temperature/humidity. Since I have separate Arduino sketches for measuring these 3 parameters I wanted to combine those sketches to build one sketch. The resulting sketch compiles, but the problem is that I only see readings from the temperature/humidity sensor. It would be greatly appreciated if you can help me solve this error.

#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22   // DHT 22  (AM2302)

DHT dht(DHTPIN, DHTTYPE);

volatile byte revolutions;

float rpmilli;
float speed;


unsigned long timeold = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  dht.begin();

  attachInterrupt(digitalPinToInterrupt(2) , rpm_fun, RISING);

  revolutions = 0;
  rpmilli = 0;
  timeold = 0;
}

void loop() {
  // Wait a few seconds between measurements.
  delay(10000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 10 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));

  if (revolutions >= 10) {
    //Update RPM every 10 counts

    // calculate the revolutions per milli(second)
    rpmilli = ((float)revolutions) / (millis() - timeold);

    timeold = millis();
    revolutions = 0;

    // WHEELCIRC = 2 * PI * radius (in meters)
    // speed = rpmilli * WHEELCIRC * "milliseconds per hour" / "meters per kilometer"

    // speed = rpmilli * WHEELCIRC * 3600000 / 1000
    // speed = rpmilli * WHEELCIRC * 3600

    speed = rpmilli * 0.80425 * 3600;

    Serial.print("RPM:");
    Serial.print(rpmilli * 60000);
    Serial.print(" Speed:");
    Serial.print(speed);
    Serial.println(" kph");
  }
}

void rpm_fun()
{
  revolutions++;
}

Thank you so much in advance for your help!

Hello @janith!

Are the readings from the hall sensor not displayed or are they 0?

For debugging, try printing revolutions, millis and timeold separately.

Also, just a suggestion, it would be best if instead of waiting for a few seconds to make the temperature reading you used a median filter, that way you have a more realistic measurement that is not as affected by noise.

2 Likes