[question] project to record and store temp.degree ,humidity,wind speed

Dear all
I have 2 sensors
1- Dht22 for temp. and humidity
2- Nrg 200 p for wind speed
First program to record and store temp degree and humidity work fine


#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <DHT.h> // for the DHT sensor
#include <RTClib.h> // for the RTC
//define DHT pin
#define DHTPIN 8 // what pin we're connected to
// uncomment whatever type you're using
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// change this to match your SD shield or module;
// Arduino Ethernet shield and modules: pin 4
// Data loggin SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 10;
// Create a file to store the data
File datalog;
// RTC
RTC_DS1307 rtc;
void setup() {
//initializing the DHT sensor
dht.begin();
//initializing Serial monitor
Serial.begin(9600);
// setup for the RTC
while(!Serial); // for Leonardo/Micro/Zero
if(! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
else {
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
if(! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
// setup for the SD card
Serial.print("Initializing SD card...");
if(!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
//open file
datalog=SD.open("DATA.txt", FILE_WRITE);
// if the file opened ok, write to it:
if (datalog) {
Serial.println("File opened ok");
// print the headings for our data
datalog.println(" DATE | TIME | TEMPERATURE| HUMIDITY");
}
datalog.close();
}
void loggingTime() {
DateTime now = rtc.now();
datalog = SD.open("DATA.txt", FILE_WRITE);
if (datalog) {
datalog.print(now.year(), DEC);
datalog.print('/');
if (now.month() < 10)
datalog.print('0');
datalog.print(now.month(), DEC);
datalog.print('/');
if (now.day() < 10)
datalog.print('0');
datalog.print(now.day(), DEC);
datalog.print(" | ");
if (now.hour() < 10)
datalog.print(' ');
datalog.print(now.hour(), DEC);
datalog.print(':');
if (now.minute() < 10)
datalog.print('0');
datalog.print(now.minute(), DEC);
datalog.print(':');
if (now.second() < 10)
datalog.print('0');
datalog.print(now.second(), DEC);
datalog.print(" | ");
}
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print("\t|");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print("\t|");
datalog.close();
delay(1000);
}
void loggingTemperature() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// Read temperature as Celsius
float t = dht.readTemperature();
float h=dht.readHumidity();
// Read temperature as Fahrenheit
//float f = dht.readTemperature(true);
// 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;
}
//debugging purposes
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print("\t|");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %");
//Serial.print(f);
//Serial.println(" *F\t");
datalog = SD.open("DATA.txt", FILE_WRITE);
if (datalog) {
Serial.println("open with success");
datalog.print(t);
datalog.print("°C");
datalog.print("\t|");
datalog.println(h);
}
datalog.close();
}
void loop() {
loggingTime();
loggingTemperature();
delay(9000);
}

And second program to only display wind speed also work fine

long tempo;
float freq, wind_speed;
int pulsos;
boolean pulso;
void setup() {
// put your setup code here, to run once:
pulso = HIGH;
pinMode(11, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
tempo = millis();
if (digitalRead(11) == HIGH)
{
if (pulso == HIGH)
{
pulsos = pulsos + 1;
}
pulso = LOW;
}
else {
pulso = HIGH;
}
if (tempo % 2000 == 0) {
freq = pulsos;
wind_speed = freq * 0.765 + 0.35;
Serial.print("wind_speed=");
Serial.print(wind_speed);
Serial.print(" m/s");
Serial.print("\t");
Serial.print("frequency= ");
Serial.print("\t");
Serial.print(freq);
Serial.print("\t");
Serial.println("Hz");
pulsos=0;
delay (5000);
}
}

Now I wrote third code to display and store temp degree and humidity and wind speed
Only display and record temp degree and humidity and ignore wind speed


#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <DHT.h> // for the DHT sensor
#include <RTClib.h> // for the RTC
//define DHT pin
#define DHTPIN 8 // what pin we're connected to
// uncomment whatever type you're using
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// change this to match your SD shield or module;
// Arduino Ethernet shield and modules: pin 4
// Data loggin SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 10;
// Create a file to store the data
File datalog;
// RTC
RTC_DS1307 rtc;
long tempo;
float freq, wind_speed;
int pulsos;
boolean pulso;

void setup() {

dht.begin();
// put your setup code here, to run once:
pulso = HIGH;
pinMode(11, INPUT);
Serial.begin(9600);
// setup for the RTC
while(!Serial); // for Leonardo/Micro/Zero
if(! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
else {
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
if(! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
// setup for the SD card
Serial.print("Initializing SD card...");
if(!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
//open file
datalog=SD.open("DATA.txt", FILE_WRITE);
// if the file opened ok, write to it:
if (datalog) {
Serial.println("File opened ok");
// print the headings for our data
datalog.println(" DATE | TIME | TEMPERATURE| HUMIDITY");
}
datalog.close();
}
void loggingTime() {
DateTime now = rtc.now();
datalog = SD.open("DATA.txt", FILE_WRITE);
if (datalog) {
datalog.print(now.year(), DEC);
datalog.print('/');
if (now.month() < 10)
datalog.print('0');
datalog.print(now.month(), DEC);
datalog.print('/');
if (now.day() < 10)
datalog.print('0');
datalog.print(now.day(), DEC);
datalog.print(" | ");
if (now.hour() < 10)
datalog.print(' ');
datalog.print(now.hour(), DEC);
datalog.print(':');
if (now.minute() < 10)
datalog.print('0');
datalog.print(now.minute(), DEC);
datalog.print(':');
if (now.second() < 10)
datalog.print('0');
datalog.print(now.second(), DEC);
datalog.print(" | ");
}
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print("\t|");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print("\t|");
datalog.close();
delay(1000);
}
void loggingTemperature() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// Read temperature as Celsius
float t = dht.readTemperature();
float h=dht.readHumidity();
// Read temperature as Fahrenheit
//float f = dht.readTemperature(true);
// 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;
}
//debugging purposes
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print("\t|");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %");
//Serial.print(f);
//Serial.println(" *F\t");
datalog = SD.open("DATA.txt", FILE_WRITE);
if (datalog) {
Serial.println("open with success");
datalog.print(t);
datalog.print("°C");
datalog.print("\t|");
datalog.println(h);
}
datalog.close();
}

void Wind_Speed(){
// put your main code here, to run repeatedly:
tempo = millis();
if (digitalRead(11) == HIGH)
{
if (pulso == HIGH)
{
pulsos = pulsos + 1;
}
pulso = LOW;
}
else {
pulso = HIGH;
}
if (tempo % 2000 == 0) {
freq = pulsos;
wind_speed = freq * 0.765 + 0.35;
//debugging purposes
Serial.print("wind speed= ");
Serial.print(wind_speed);
Serial.print(" m/s");
Serial.print("\t");
Serial.print("frequency= ");
Serial.print("\t");
Serial.print(freq);
Serial.print("\t");
Serial.println("Hz");
datalog = SD.open("DATA.txt", FILE_WRITE);
if (datalog) {
Serial.println("open with success");
datalog.print(wind_speed);
datalog.print("m/s");
datalog.print("\t|");
datalog.println(freq);
}
pulsos=0;
}
datalog.close();
}
void loop() {
loggingTime();
loggingTemperature();
Wind_Speed();
delay(9000);
}

so any one can help me with this issue

1- wind sensors is NRG 200p

2- datalogger logger

with a delay(9000); in the code, you'll have a hard time tracking the wind...

in the other code the loop was running as fast as possible until 2s have elapsed and print the data, then pause for 5s and go again for 2s at full speed.

ok thank u do you recomanded to change
dealy (9000) to delay(3000)

No I recommend getting rid of it. the loop needs to run as fast as possible to check the state changes on the wind sensor you have on pin 2 (you could use interrupts)

please can you explain me how
exactly any part of code need modification

Just try by removing the delay (I’m on my iPhone)

Pin 11 is one of the four pins of the SPI interface used to talk to the SD card. Pick a different pin.

ok i will try
thank u