Hello, I am trying to replace part of my code with another code that is better for my project. I need to replace all the information about the DHT11 temperature sensor from the first code with the second code. Theoretically this should be easy, but I am complety new to Arduino.
Thank you for your time
here is the first code
#include "RTClib.h"
#include "DHT.h"
#include <SD.h>
#include <SPI.h>
//----------------------------------------------
RTC_DS1307 rtc; //--> Initialize RTC_DS1307
#define DHTPIN 2 //--> Arduino pin connected to DHT11 Pin Out
#define DHTTYPE DHT11 //--> The type of DHT sensor used
DHT dht(DHTPIN, DHTTYPE);
File myFile; //--> File Initialization
const int chipSelect = 4; //--> chipSelect (CS) Pin
unsigned long previousMillis = 0; //--> will store last time was updated
const long interval = 1000; //--> interval (1000 = 1 second)
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Initializing RTC DS1307...");
delay(100);
//----------------------------------------------Initializing RTC DS1307
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
//----------------------------------------------
Serial.println("Successfully Initializing the RTC DS1307");
Serial.println();
Serial.println("Starting to run RTC DS1307...");
delay(100);
//----------------------------------------------Checks whether the DS1307 RTC can be run or not
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled (Set the time and date based on your computer time and date)
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //-> If that doesn't work, use this line of code outside of "if (! rtc.isrunning())"
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
//----------------------------------------------
//----------------------------------------------Set the Date and Time on the DS1307 RTC
// following line sets the RTC to the date & time this sketch was compiled (Set the time and date based on your computer time and date)
//Serial.println("Setting the Date and Time...");
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //-> Use this line of code if it doesn't work in "if (! rtc.isrunning())"
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
//delay(100);
//Serial.println("Successfully Setting Date and Time");
//Serial.println();
// If the time and date are successfully set, then deactivate the code line (make the code a comment), then re-upload the code.
// if not done then the time and date will return to the beginning when it was set when arduino is reset or restarted.
//----------------------------------------------
Serial.println("Successfully running RTC DS1307");
delay(100);
//----------------------------------------------Starting up the DHT11 Sensor
Serial.println("Start DHT11");
dht.begin();
delay(100);
Serial.println();
//----------------------------------------------
//----------------------------------------------Initializing Micro SD Card
Serial.println("Initializing Micro SD Card...");
delay(100);
if (!SD.begin(chipSelect)) {
Serial.println("Initialization failed!");
while (1);
}
Serial.println("Successfully Initializing Micro SD Card");
Serial.println();
delay(100);
//----------------------------------------------
}
void loop() {
// put your main code here, to run repeatedly:
//___________________________________________________________________________________Millis to update the Date, Time, Temperature and Humidity
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
DateTime now = rtc.now(); //--> Get or update the Date and Time
//----------------------------------------------Get or update Temperature and Humidity Values
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
int h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
//----------------------------------------------
//----------------------------------------------Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read Data from DHT sensor!");
delay(500);
return;
}
//----------------------------------------------
//___________________________________________________________________________________Conditions for writing data to a Micro SD Card
// Data Date, Time, Temperature and Humidity will be written to the Micro SD Card if Seconds are worth 0,5,10,15,20 and so on
if (now.second() %5 == 0) {
Serial.println("----");
//----------------------------------------------Printing Date and Time to Serial Monitor
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
//----------------------------------------------
//----------------------------------------------Printing Temperature and Humidity Values to Serial Monitor
Serial.print("Temperature : ");
Serial.print(h);
Serial.print(" | Humidity : ");
Serial.println(t);
Serial.println();
//----------------------------------------------
//___________________________________________________________________________________Write to Micro SD Card
//----------------------------------------------Opening files that have been specified on the Micro SD Card
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("DHT11Log.txt", FILE_WRITE);
//----------------------------------------------
//----------------------------------------------Writing Data to the specified file on the Micro SD Card
// if the file opened okay, write to it:
if (myFile) {
Serial.println("Writing to DHT11Log.txt...");
//----------------------------------------------Writing Date Data to Micro SD Card
myFile.print(now.day());
myFile.print("-");
myFile.print(now.month());
myFile.print("-");
myFile.print(now.year());
//----------------------------------------------
myFile.print(","); //--> Write the delimiter between the data in the form of a comma character to the Micro SD Card
//----------------------------------------------Writing Time Data to Micro SD Card
myFile.print(now.hour());
myFile.print(":");
myFile.print(now.minute());
myFile.print(":");
myFile.print(now.second());
//----------------------------------------------
myFile.print(","); //--> Write the delimiter between the data in the form of a comma character to the Micro SD Card
myFile.print(t); //--> Writing Temperature Data to Micro SD Card
myFile.print(","); //--> Write the delimiter between the data in the form of a comma character to the Micro SD Card
myFile.println(h); //--> Writing Humidity Data to Micro SD Card
myFile.close(); //--> close the file
Serial.println("Successfully writing Data to Micro SD Card");
Serial.println("----");
Serial.println();
} else {
Serial.println("Error opening DHT11Log.txt"); //--> if the file didn't open, print an error
}
//----------------------------------------------
//___________________________________________________________________________________
}
//___________________________________________________________________________________
}
//___________________________________________________________________________________
}
//=========================================================================================
and here is the second
#include "DHT.h"
#define DHT1_PIN 2 // for first DHT module
#define DHT2_PIN 7 // for 2nd DHT module and do the same for 3rd and 4th etc.
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht1(DHT1_PIN, DHTTYPE);//for first DHT module
DHT dht2(DHT2_PIN, DHTTYPE);// for 2nd DHT module and do the same for 3rd and 4th etc.
void setup() {
Serial.begin(9600);
Serial.println("DHTxx Robojax test!");
dht1.begin();//for first DHT module
dht2.begin();//for 2nd DHT module and do the same for 3rd and 4th etc.
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Robojax.com test video
Serial.print("Temperature 1: ");
Serial.print(getTemp("c", 1));// get DHT1 temperature in C
Serial.print(" *C ");
Serial.print(getTemp("f", 1));// get DHT1 temperature in F
Serial.println (" *F");
Serial.println("-----------------");
Serial.print("Temperature 2: ");
Serial.print(getTemp("c", 2));// get DHT2 temperature in C
Serial.print(" *C ");
Serial.print(getTemp("f", 2));// get DHT2 temperature in F
Serial.println (" *F");
Serial.println("-----------------");
}
/*
* getTemp(String req, int dhtCount)
* returns the temprature related parameters
* req is string request
dhtCount is 1 or 2 or 3 as you wish
* This code can display temprature in:
* getTemp("c", 1) is used to get Celsius for first DHT
* getTemp("f", 2) is used to get fahrenheit for 2nd DHT
* getTemp("k"m 1) is used for Kelvin for first DHT
* getTemp("hif", 1) is used to get fahrenheit for first DHT
* getTemp("hic", 2) is used to get Celsius for 2nd DHT
* getTemp("f", 2) is used to get humidity for 2nd DHT
*/
float getTemp(String req, int dhtCount)
{
if(dhtCount ==1){
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h1 = dht1.readHumidity();
// Read temperature as Celsius (the default)
float t1 = dht1.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f1 = dht1.readTemperature(true);
// Compute heat index in Fahrenheit (the default)
float hif1 = dht1.computeHeatIndex(f1, h1);
// Compute heat index in Celsius (isFahreheit = false)
float hic1 = dht1.computeHeatIndex(t1, h1, false);
// Check if any reads failed and exit early (to try again).
if (isnan(h1) || isnan(t1) || isnan(f1)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Kelvin
float k1 = t1 + 273.15;
if(req =="c"){
return t1;//return Cilsus
}else if(req =="f"){
return f1;// return Fahrenheit
}else if(req =="h"){
return h1;// return humidity
}else if(req =="hif"){
return hif1;// return heat index in Fahrenheit
}else if(req =="hic"){
return hic1;// return heat index in Cilsus
}else if(req =="k"){
return k1;// return temprature in Kelvin
}else{
return 0.000;// if no reqest found, retun 0.000
}
}// DHT1 end
if(dhtCount ==2){
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h2 = dht2.readHumidity();
// Read temperature as Celsius (the default)
float t2 = dht2.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f2 = dht2.readTemperature(true);
// Compute heat index in Fahrenheit (the default)
float hif2 = dht2.computeHeatIndex(f2, h2);
// Compute heat index in Celsius (isFahreheit = false)
float hic2 = dht2.computeHeatIndex(t2, h2, false);
// Check if any reads failed and exit early (to try again).
if (isnan(h2) || isnan(t2) || isnan(f2)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Kelvin
float k2 = t2 + 273.15;
if(req =="c"){
return t2;//return Cilsus
}else if(req =="f"){
return f2;// return Fahrenheit
}else if(req =="h"){
return h2;// return humidity
}else if(req =="hif"){
return hif2;// return heat index in Fahrenheit
}else if(req =="hic"){
return hic2;// return heat index in Cilsus
}else if(req =="k"){
return k2;// return temprature in Kelvin
}else{
return 0.000;// if no reqest found, retun 0.000
}
}// DHT2 end
}//getTemp end