Go Down

Topic: 15 minutes data logging delay with regulation (Read 132 times) previous topic - next topic

mateuszriou123

How would it be possible to save information to SD every 15 minutes but at the same time regulate temperature and humidity every second?

I believe the 15 mins would block this function to regulate every second.

Here is the code:

Code: [Select]
/*
 * File:   main.c
 * Company: RIOU Glass
 * Author: Mateusz WINTER
 * Project: This project is to monitor temperature and humidity
 * in a beehive by logging and saving data on SD card
 *
 */

// Configuration for DHT Sensor

 #include <DHT.h>

 #define DHTPIN 2

 #define DHTTYPE DHT11

 DHT dht(DHTPIN, DHTTYPE);

 // Configuration for RTC Module

// DS1302 RST  -> Arduino Digital 7
// DS1302 DATA -> Arduino Digital 6
// DS1302 CLK  -> Arduino Digital 5

#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(6, 7, 8);


// Configuration SD card module

// SD card attached to SPI bus as follows:

// MOSI - pin 11
// MISO - pin 12
// CLK - pin 13
// CS - pin 4

#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;

// Temperature sensors

float Temp_1;
float Temp_2;

// Delay

unsigned long seconds = 1000L; // 1000ms to 1s
unsigned long minutes = seconds * 900; // 900s in 15 minutes

// Fan and heater

const int HEATER = 10; //Red LED turn on heater
const int FAN = 9; //Blue LED turn on fan

void setup () {
  Serial.begin(9600); // Baud rate of 9600
  pinMode(chipSelect, OUTPUT);
// Heater and fan

  pinMode(HEATER,OUTPUT);
  pinMode(FAN,OUTPUT);
  digitalWrite(HEATER,LOW);
  digitalWrite(FAN,LOW);

// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year
  myRTC.setDS1302Time(00, 17, 11, 6, 25, 06, 2019);
 
  Serial.println(F("Beehive data logger"));
  Serial.println();
 
  while (!Serial) {
  }
  Serial.print("Starting SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed to start or not present");
    while (1);
  }
  Serial.println("   Card is now operational!");
  Serial.println();
  dht.begin();
 
  File dataFile = SD.open("testing.txt", FILE_WRITE);
// Printing column name for date, time, Temp_1, Temp_2, Temp_DHT and Humidity on serial terminal
 
  delay(100);
  Serial.print("Date      "); // Print header name for column
  Serial.print(" \t");        // Create tab/space
  Serial.print("Time      "); // Print header name for column
  Serial.print(" \t");        // Create tab/space
  Serial.print("Humidity in %"); // Print header name for column 
  Serial.print(" \t");  // Create tab/space   
  Serial.print("Temp_DHT in °C"); // Print header name for column 
  Serial.print(" \t"); // Create tab/space
  Serial.print("Temp_1 in °C"); // Print header name for column
  Serial.print(" \t"); // Create tab/space
  Serial.print("Temp_2 in °C"); // Print header name for column

  Serial.println();  // Insert a new line 
 
// Printing column name for date, time, Temp_1, Temp_2, Temp_DHT and Humidity on SD card
 
  dataFile.print("Date      "); // Print header name for column
  dataFile.print(" \t");        // Create tab/space
  dataFile.print("Time      "); // Print header name for column
  dataFile.print(" \t");        // Create tab/space
  dataFile.print("Humidity in %"); // Print header name for column
  dataFile.print(" \t"); // Create tab/space     
  dataFile.print("Temp_DHT in °C"); // Print header name for column 
  dataFile.print(" \t");  // Create tab/space
  dataFile.print("Temp_1 in °C"); // Print header name for column
  dataFile.print(" \t"); // Create tab/space
  dataFile.print("Temp_2 in °C"); // Print header name for column

  dataFile.println();  // Insert a new line
  dataFile.close();
 
}

//}

void loop() {
 
  File dataFile = SD.open("testing.txt", FILE_WRITE);
  // This allows for the update of variables for time or accessing the individual elements.                //
  myRTC.updateTime();   
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  Temp_1 = analogRead(A0)*5/1023.0; // Read value from analog port A0 voltage range 0V - 5V 1023 is to convert the 10 bit number to a voltage reading.
  Temp_1 = Temp_1 - 0.5;
  Temp_1 = Temp_1 / 0.01;
  Temp_1 = Temp_1 - 5.0;
  Temp_2 = analogRead(A1)*5/1023.0; // Read value from analog port A1 voltage range 0V - 5V 1023 is to convert the 10 bit number to a voltage reading.
  Temp_2 = Temp_2 - 0.5;
  Temp_2 = Temp_2 / 0.01;
  Temp_2 = Temp_2 - 5.0;

  if (dataFile){
// Save to SD card

  dataFile.print("");                                                         
  dataFile.print(myRTC.dayofmonth);                                                             
  dataFile.print("/");                                                                           
  dataFile.print(myRTC.month);                                                                 
  dataFile.print("/");                                                                         
  dataFile.print(myRTC.year);                                                               
  dataFile.print("       ");                                                                       
  dataFile.print(myRTC.hours);                                                                 
  dataFile.print(":");                                                                                   
  dataFile.print(myRTC.minutes);                                                                       
  dataFile.print(":");                                                                                   
  dataFile.print(myRTC.seconds);
  dataFile.print("    \t"); 
  dataFile.print(F(""));
  dataFile.print(h);
  dataFile.print(F("%  "));
  dataFile.print(t);
  dataFile.print("     \t"); // Create tab/space 
  dataFile.print(Temp_1); // Print temperature from temperature sensor 1
  dataFile.print("     \t"); // Create tab/space
  dataFile.print(Temp_2); // Print temperature from temperature sensor 2
  dataFile.print("     \t"); // Create tab/space
  dataFile.println();
  dataFile.close();
// Print to serial

  Serial.print("");                                                         
  Serial.print(myRTC.dayofmonth);                                                             
  Serial.print("/");                                                                           
  Serial.print(myRTC.month);                                                                 
  Serial.print("/");                                                                         
  Serial.print(myRTC.year);                                                               
  Serial.print("       ");                                                                       
  Serial.print(myRTC.hours);                                                                 
  Serial.print(":");                                                                                   
  Serial.print(myRTC.minutes);                                                                       
  Serial.print(":");                                                                                   
  Serial.print(myRTC.seconds);
  Serial.print("    \t");   
  Serial.print(F(""));
  Serial.print(h);
  Serial.print(F("%  \t"));
  Serial.print(t);
  Serial.print("     \t"); // Create tab/space
  Serial.print(Temp_1); // Print temperature from temperature sensor 1
  Serial.print("     \t"); // Create tab/space
  Serial.print(Temp_2); // Print temperature from temperature sensor 2
  Serial.print("     \t"); // Create tab/space
  Serial.println();
  }

  else {
    Serial.println("There has been an error saving data");
  }

// Temperature and humidity control

if(Temp_2 > 25) // If the temperature is above 25 then turn on fan
{
   digitalWrite(HEATER,LOW);
   digitalWrite(FAN,HIGH);
}
else
{
  digitalWrite(HEATER,HIGH);
  digitalWrite(FAN,LOW);
}
 

//if ( h > 60 ) // If the humidity if greater than 60%
//{
//  digitalWrite(HEATER, LOW); 
//  digitalWrite(FAN, HIGH);
//}
//else
//{
//  digitalWrite(HEATER, LOW); 
//  digitalWrite(FAN, LOW);
//}

//delay(minutes);
delay(2000);
 
}

PaulS

Quote
I believe the 15 mins would block this function to regulate every second.
Not if written properly (as in using the blink without delay technique).

Code: [Select]
unsigned long seconds = 1000L; // 1000ms to 1s
That's a dumb name for a variable that holds milliseconds.

Code: [Select]
  Temp_1 = Temp_1 / 0.01;
Multiplying by 100 has the same result but is far quicker.

Code: [Select]
  Temp_2 = analogRead(A1)*5/1023.0;
Gettingyourspacekeyfixedwouldbegood.

Code: [Select]
delay(2000);
Why? You've got a clock. Use it!
The art of getting good answers lies in asking good questions.

Hal_Posoyan

Not if written properly (as in using the blink without delay technique).
Have a look at this youtube video which blinks 2 leds independently of each other. That's pretty much what you want to do: one of the blinks would be do your regulation thing, the other blink is when you do the SD card.







Robin2

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

mateuszriou123

Thank you all for the information about millis() it truely is far more superior than delay()

It works like a charm.

In case you are all interested in seeing my new code, I have attached it.

Code: [Select]
/*
 * File:   main.c
 * Company: RIOU Glass
 * Author: Mateusz WINTER
 * Project: This project is to monitor temperature and humidity
 * in a beehive by logging and saving data on SD card
 *
 */

// Configuration for DHT Sensor

 #include <DHT.h>

 #define DHTPIN 2

 #define DHTTYPE DHT11

 DHT dht(DHTPIN, DHTTYPE);

 // Configuration for RTC Module

// DS1302 RST  -> Arduino Digital 7
// DS1302 DATA -> Arduino Digital 6
// DS1302 CLK  -> Arduino Digital 5

#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(6, 7, 8);


// Configuration SD card module

// SD card attached to SPI bus as follows:

// MOSI - pin 11
// MISO - pin 12
// CLK - pin 13
// CS - pin 4

#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;

// Temperature sensors

float Temp_1;
float Temp_2;

// Delay

//unsigned long seconds = 1000L; // 1000ms to 1s
//unsigned long minutes = seconds * 900; // 900s in 15 minutes

const unsigned long interval = 60000 ; // 90 minutes is 900000
unsigned long previous = 0 ;
const unsigned long intervala = 1000 ;
unsigned long previousa = 0 ;


// Fan and heater

const int HEATER = 10; //Red LED turn on heater
const int FAN = 9; //Blue LED turn on fan

void setup () {
  Serial.begin(9600); // Baud rate of 9600
  pinMode(chipSelect, OUTPUT);



// Heater and fan

  pinMode(HEATER,OUTPUT);
  pinMode(FAN,OUTPUT);
  digitalWrite(HEATER,LOW);
  digitalWrite(FAN,LOW);

// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year
  myRTC.setDS1302Time(00, 17, 11, 6, 25, 06, 2019);
 
  Serial.println(F("Beehive data logger"));
  Serial.println();
 
  while (!Serial) {
  }
  Serial.print("Starting SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed to start or not present");
    while (1);
  }
  Serial.println("   Card is now operational!");
  Serial.println();
  dht.begin();
 
  File dataFile = SD.open("testing.txt", FILE_WRITE);
// Printing column name for date, time, Temp_1, Temp_2, Temp_DHT and Humidity on serial terminal
 
  delay(100);
  Serial.print("Date      "); // Print header name for column
  Serial.print(" \t");        // Create tab/space
  Serial.print("Time      "); // Print header name for column
  Serial.print(" \t");        // Create tab/space
  Serial.print("Humidity in %"); // Print header name for column 
  Serial.print(" \t");  // Create tab/space   
  Serial.print("Temp_DHT in °C"); // Print header name for column 
  Serial.print(" \t"); // Create tab/space
  Serial.print("Temp_1 in °C"); // Print header name for column
  Serial.print(" \t"); // Create tab/space
  Serial.print("Temp_2 in °C"); // Print header name for column

  Serial.println();  // Insert a new line 
 
// Printing column name for date, time, Temp_1, Temp_2, Temp_DHT and Humidity on SD card
 
  dataFile.print("Date      "); // Print header name for column
  dataFile.print(" \t");        // Create tab/space
  dataFile.print("Time      "); // Print header name for column
  dataFile.print(" \t");        // Create tab/space
  dataFile.print("Humidity in %"); // Print header name for column
  dataFile.print(" \t"); // Create tab/space     
  dataFile.print("Temp_DHT in °C"); // Print header name for column 
  dataFile.print(" \t");  // Create tab/space
  dataFile.print("Temp_1 in °C"); // Print header name for column
  dataFile.print(" \t"); // Create tab/space
  dataFile.print("Temp_2 in °C"); // Print header name for column

  dataFile.println();  // Insert a new line
  dataFile.close();
 
}

//}

void loop() {
 
  unsigned long current = millis();
  unsigned long currenta = millis();

  if(current - previous >= interval) {
  File dataFile = SD.open("testing.txt", FILE_WRITE);
  // This allows for the update of variables for time or accessing the individual elements.                //
  myRTC.updateTime();   
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  Temp_1 = analogRead(A0)*5/1023.0; // Read value from analog port A0 voltage range 0V - 5V 1023 is to convert the 10 bit number to a voltage reading.
  Temp_1 = Temp_1 - 0.5;
  Temp_1 = Temp_1 / 0.01;
  Temp_1 = Temp_1 - 5.0;
  Temp_2 = analogRead(A1)*5/1023.0; // Read value from analog port A1 voltage range 0V - 5V 1023 is to convert the 10 bit number to a voltage reading.
  Temp_2 = Temp_2 - 0.5;
  Temp_2 = Temp_2 / 0.01;
  Temp_2 = Temp_2 - 5.0;

  if (dataFile){
// Save to SD card

  dataFile.print("");                                                         
  dataFile.print(myRTC.dayofmonth);                                                             
  dataFile.print("/");                                                                           
  dataFile.print(myRTC.month);                                                                 
  dataFile.print("/");                                                                         
  dataFile.print(myRTC.year);                                                               
  dataFile.print("       ");                                                                       
  dataFile.print(myRTC.hours);                                                                 
  dataFile.print(":");                                                                                   
  dataFile.print(myRTC.minutes);                                                                       
  dataFile.print(":");                                                                                   
  dataFile.print(myRTC.seconds);
  dataFile.print("    \t"); 
  dataFile.print(F(""));
  dataFile.print(h);
  dataFile.print(F("%  "));
  dataFile.print(t);
  dataFile.print("     \t"); // Create tab/space 
  dataFile.print(Temp_1); // Print temperature from temperature sensor 1
  dataFile.print("     \t"); // Create tab/space
  dataFile.print(Temp_2); // Print temperature from temperature sensor 2
  dataFile.print("     \t"); // Create tab/space
  dataFile.println();
  dataFile.close();
// Print to serial

  Serial.print("");                                                         
  Serial.print(myRTC.dayofmonth);                                                             
  Serial.print("/");                                                                           
  Serial.print(myRTC.month);                                                                 
  Serial.print("/");                                                                         
  Serial.print(myRTC.year);                                                               
  Serial.print("       ");                                                                       
  Serial.print(myRTC.hours);                                                                 
  Serial.print(":");                                                                                   
  Serial.print(myRTC.minutes);                                                                       
  Serial.print(":");                                                                                   
  Serial.print(myRTC.seconds);
  Serial.print("    \t");   
  Serial.print(F(""));
  Serial.print(h);
  Serial.print(F("%  \t"));
  Serial.print(t);
  Serial.print("     \t"); // Create tab/space
  Serial.print(Temp_1); // Print temperature from temperature sensor 1
  Serial.print("     \t"); // Create tab/space
  Serial.print(Temp_2); // Print temperature from temperature sensor 2
  Serial.print("     \t"); // Create tab/space
  Serial.println();
  }

  else {
    Serial.println("There has been an error saving data");
  }
  previous = current ;
  }

// Temperature and humidity control

if ( currenta - previousa >= intervala) {

  Temp_1 = analogRead(A0)*5/1023.0; // Read value from analog port A0 voltage range 0V - 5V 1023 is to convert the 10 bit number to a voltage reading.
  Temp_1 = Temp_1 - 0.5;
  Temp_1 = Temp_1 / 0.01;
  Temp_1 = Temp_1 - 5.0;

Serial.println(Temp_1); // Print temperature from temperature sensor 1 

if(Temp_1 > 35) // Set the temperature
{
   digitalWrite(HEATER,LOW);
   digitalWrite(FAN,HIGH);
}
else
{
  digitalWrite(HEATER,HIGH);
  digitalWrite(FAN,LOW);
}
      previousa = currenta ;
    }


//if ( h > 60 ) // If the humidity if greater than 60%
//{
//  digitalWrite(HEATER, LOW); 
//  digitalWrite(FAN, HIGH);
//}
//else
//{
//  digitalWrite(HEATER, LOW); 
//  digitalWrite(FAN, LOW);
//}
 
}

Go Up