Arduino Mega with 2004 Lcd Screen problem

Hello
I was working on a smart solar dryer project, which measures temperature, humidity and ultraviolet rays, and also weighs the sample tray during the drying period, and all of this works through solar energy (whether by drying itself or by operating the system through solar panels)

I'm working with an Arduino mega board, and it's connected to it

  • Outside the control box:
    . 2 * DHT22 sensor to measure temperature and humidity inside and outside the tunnel
    . 1 * VEML6070 . UV Sensor
    . 1 * Weighing cell 20 kg / with HX711
    . 2 * Relay turns on the aerating fan and opens and locks the box with a heat lamp for heating during the night
    . 1 * button that stops reading for 10 seconds to take it manually
  • Inside the control box:
    . RTC
    . SD card reader module
    . 2004 LCD screen

Finally, I am working on recording all the readings on the SD card

But the problem is that from time to time when I let it run by itself, I find that the system has stopped (the screen stops showing readings)

This has happened a lot.. I need your help to solve the problem of disabling reading
:pray:

This is the entire code after the last modification :
DHT.ino (1.2 KB)
Fan.ino (213 Bytes)
Final_Head.ino (5.6 KB)
Heater.ino (238 Bytes)
LCD.ino (106 Bytes)
Load_Cell.ino (933 Bytes)
Push_Button.ino (263 Bytes)
RTC.ino (655 Bytes)
SDCard.ino (1.9 KB)
UV_Sensor.ino (242 Bytes)

I will not download a zip file and neither will many members. Read the forum guidelines to see how to properly post code.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

That can be caused by things like reading or writing beyond the bounds of an array, the misuse of the String class in small memory processors or running out of memory to name a few.

The problem is that the code is grouped into more than one tab, and I don't know how to put the entire code with all the tabs in the Preformatted text " ctrl + E "

Post it in blocks

there he is ..

//DHT22 Temperature & Humidity Sensors
#include <DHT.h>
#include <Wire.h>
//Constants
#define DHTPIN1 8 // ------ out
#define DHTPIN2 13 // ------ in
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht1(DHTPIN1, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
DHT dht2(DHTPIN2, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
//Variables
int chk ;
int dhtCount;
float hum1 ; //Stores humidity 1 value
float temp1 ; //Stores temperature 1 value
float hum2 ; //Stores humidity 2 value
float temp2 ; //Stores temperature 2 value
//----------------------------------------------------------------------------
//LCD
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = A1, en = A2, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//----------------------------------------------------------------------------
//Load Cell
#include "HX711.h"
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 49;
const int LOADCELL_SCK_PIN = 48;
HX711 scale;
float weight ;
float Weight ;
//---------------------------------------------------------------------------
//Air Flipping Fan
#define fan A0
//--------------------------------------------------------------------------
//SD Card
#include <SPI.h>
#include <SD.h>
File dataFile ;
//--------------------------------------------------------------------------
//DS1307 RTC Time & Clock
#include <TimeLib.h>
#include <DS1307RTC.h>
//---------------------------------------------------------------------------
// push Button
const char Button = 6 ;
bool Pressed = false ;
//---------------------------------------------------------------------------
// UV Light Sensor
#include "Adafruit_VEML6070.h"
Adafruit_VEML6070 uv = Adafruit_VEML6070();
//---------------------------------------------------------------------------
int Heat_Lamp = 10 ;
/* Timming Setup /
const unsigned long event_1_time = 1000 ; // DHT22 Temperature & Humidity Sensors ( 1s )
const unsigned long event_2_time = 20000 ; // DC fan OFF" ( 20s )
const unsigned long event_3_time = 80000 ; // DC fan ON' ( 1m )
const unsigned long event_4_time = 5000 ; // LCD Refresh ( 5s )
const unsigned long event_5_time = 250 ; // Load Cell Weight ( 0.25s )
const unsigned long event_6_time = 300000 ; // SD Card Saving Time ( 5m )
const unsigned long event_7_time = 1000 ; // UV Light Sensor ( 1s )
unsigned long Previous_1_time = 0 ;
unsigned long Previous_2_time = 0 ;
unsigned long Previous_3_time = 0 ;
unsigned long Previous_4_time = 0 ;
unsigned long Previous_5_time = 0 ;
unsigned long Previous_6_time = 0 ;
unsigned long Previous_7_time = 0 ;
//-
------------------------------------
void setup()
{
//Serial.begin(9600);
LCD_Setup();
for (int i = 7 ; i >= 0 ; i-- )
{
lcd.clear ();
lcd.setCursor (i, 0) ;
lcd.print("Welcome To ..");
delay (500);
}
lcd.setCursor (0, 1);
lcd.print(" Drying ");
lcd.setCursor (0, 2);
lcd.print(" Agri. Products ");
lcd.setCursor (0, 3);
lcd.print(" by Green House ");
delay (5000);
lcd.clear ();
RTC_Loop ();
delay (2000) ;
PB_Setup ();
DHT_Setup ();
UV_Setup ();
FFan_Setup ();
LC_Setup ();
SD_Setup ();
HL_Setup ();
}
uint16_t line = 1 ;
void print2digits_sd(int number_sd) {
if (number_sd >= 0 && number_sd < 10) {
dataFile.write('0');
}
dataFile.print(number_sd);
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
lcd.write('0');
}
lcd.print(number);
}
void loop() {
PB_loop ();
if ( temp2 <= 35.0 ) {
HL_ON_Loop ();
}
if ( temp2 >= 40.0 ) {
HL_OFF_Loop ();
}
/* start record time with millis */
unsigned long Current_time = millis ();
//---------------------------------------------------------------
//DHT22 Temperature & Humidity Sensors .. " 1 "
if (Current_time - Previous_1_time >= event_1_time ) {
DHT_Loop ();
Previous_1_time = Current_time ;
}
//--------------------------------------------------------------
//Air Flipping Fan.. " 2 , 3 "
if (Current_time - Previous_2_time >= event_2_time ) {
FFan_ON_Loop ();
Previous_2_time = Current_time ;
}
if (Current_time - Previous_3_time >= event_3_time ) {
FFan_OFF_Loop ();
Previous_3_time = Current_time ;
}
//----------------------------------------------------------------
// LCD .. " 4 "
if (Current_time - Previous_4_time >= event_4_time ) {
LCD_Loop ();
Previous_4_time = Current_time ;
}
//----------------------------------------------------------------
//Load Cell .. " 5 "
if (Current_time - Previous_5_time >= event_5_time ) {
LC_Loop ();
Previous_5_time = Current_time ;
}
//---------------------------------------------------------------------
//SD Card .. " 6 "
if (Current_time - Previous_6_time >= event_6_time ) {
SD_Loop ();
Previous_6_time = Current_time ;
}
//---------------------------------------------------------------------
//UV Light Sensor .. " 7 "
if (Current_time - Previous_7_time >= event_7_time ) {
UV_Loop ();
Previous_7_time = Current_time ;
}
}

//DHT22 Temperature & Humidity Sensors
void DHT_Setup () {
dht1.begin();
dht2.begin();
}
void DHT_Loop () {
switch( dhtCount)
{
case 0:
//Read data and store it to variables hum and temp
hum1 = dht1.readHumidity();
temp1 = dht1.readTemperature();
//Print temp and humidity values to LCD
lcd.setCursor(0, 0);
lcd.print("Humidity O: ");
lcd.print(hum1);
lcd.print(" %");
lcd.setCursor(0, 1);
lcd.print("TempO: ");
lcd.print(temp1);
lcd.print(" Celsius");
break;
case 1:
//Read data and store it to variables hum and temp
hum2 = dht2.readHumidity();
temp2 = dht2.readTemperature();
//Print temp and humidity values to LCD
lcd.setCursor(0, 0);
lcd.print("Humidity I: ");
lcd.print(hum2);
lcd.print(" %");
lcd.setCursor(0, 1);
lcd.print("TempI: ");
lcd.print(temp2);
lcd.print(" Celsius");
break;
}
dhtCount++;
if( dhtCount > 1)
dhtCount = 0;
}

//Air Flipping Fan
void FFan_Setup (){
pinMode ( fan , OUTPUT );
}
void FFan_ON_Loop (){
digitalWrite (fan , HIGH );
}
void FFan_OFF_Loop (){
digitalWrite (fan , LOW );
}

//Heat Green House Air By Lamp
void HL_Setup (){
pinMode ( Heat_Lamp , OUTPUT );
}
void HL_ON_Loop (){
digitalWrite ( Heat_Lamp , HIGH );
}
void HL_OFF_Loop (){
digitalWrite ( Heat_Lamp , LOW );
}

//LCD
void LCD_Setup(){
lcd.begin(20,4);
}
void LCD_Loop (){
lcd.begin(20,4);
}

//Load Cell
void LC_Setup (){
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN) ;
scale.set_scale(115.50) ; // this value is obtained by calibrating the scale with known weights; see the README for details
//scale.tare(50) ; // reset the scale to 0
}
void LC_Loop (){
weight = scale.get_units()+( scale.get_units()* 0.0159 ) ; // Correction Value " 2%"
if(weight <= 5.0)
{
weight = 0.0;
}
if(weight<1000)
{
//lcd.clear();
lcd.setCursor(0, 3);
lcd.print("Weight = ");
lcd.print(weight); //(000.00)
lcd.print(" g ");
}
if(weight>=1000)
{
//lcd.clear();
Weight = weight/1000;
lcd.setCursor(0, 3);
lcd.print("Weight = "); //(00.00)
lcd.print(Weight);
lcd.print(" kg ");
}
}

// push Button
void PB_Setup (){
pinMode ( Button , INPUT_PULLUP );
}
void PB_loop (){
bool Button_State = digitalRead (Button) ;
if ( Button_State == Pressed ){
delay (10000); // Hold Down Timing ( 10s )
}
}

//DS1307 RTC Time & Clock
void RTC_Setup (){
}
void RTC_Loop (){
tmElements_t tm ;
if (RTC.read(tm)) {
lcd.setCursor(0, 0);
lcd.print("Time :");
lcd.setCursor(6, 1);
print2digits(tm.Hour);
lcd.write(':');
print2digits(tm.Minute);
lcd.write(':');
print2digits(tm.Second);
if ( tm.Hour > 12 ){
lcd.write(" PM");
}
else {lcd.write(" AM");}
lcd.setCursor(0,2);
lcd.print("Date :");
lcd.setCursor(8,3);
lcd.print(tm.Day);
lcd.write('/');
lcd.print(tm.Month);
lcd.write('/');
lcd.print(tmYearToCalendar(tm.Year));
}
}

//SD Card
void SD_Setup (){
lcd.clear ();
lcd.setCursor (0,0);
lcd.print("Initializing SD card");
delay (5000);
if (!SD.begin()) {
lcd.setCursor (0,1);
lcd.print("initialization ");
lcd.setCursor (0,2);
lcd.print(" failed!");
delay (2000);
while (1);
lcd.clear ();
}
lcd.setCursor (0,3);
lcd.print("initialization done.");
delay (2000);
lcd.clear ();
}
void SD_Loop (){
dataFile = SD.open("dry.TXT", FILE_WRITE);
tmElements_t tm ;
if (RTC.read(tm)) {
dataFile.print(line++);
dataFile.print(". Time : ");
print2digits_sd(tm.Hour);
dataFile.write(':');
print2digits_sd(tm.Minute);
dataFile.write(':');
print2digits_sd(tm.Second);
dataFile.write(' ');
if ( tm.Hour > 12 ){
dataFile.write(" PM ");
}
else {dataFile.write(" AM ");}
dataFile.write(',');
dataFile.print(" Date : ");
dataFile.print(tm.Day);
dataFile.write('/');
dataFile.print(tm.Month);
dataFile.write('/');
dataFile.print(tmYearToCalendar(tm.Year));
}
dataFile.print(" : Temperature Out = ");
dataFile.print(temp1);
dataFile.print(" °C , Humidity Out = ");
dataFile.print(hum1);
dataFile.print(" %");
dataFile.print(" || Temperature In = ");
dataFile.print(temp2);
dataFile.print(" °C , Humidity In = ");
dataFile.print(hum2);
dataFile.print(" %");
dataFile.print(" || UV light level: ");
dataFile.print(uv.readUV());
//dataFile.print(" lux ");
if(weight <= 5.0)
{
weight = 0.0;
}
if(weight<1000)
{
dataFile.print(" || Weight = ");
dataFile.print(weight);
dataFile.println(" g");
}
if(weight>=1000)
{
dataFile.print(" || Weight = ");
dataFile.print(Weight);
dataFile.println(" kg");
}
dataFile.close();
}

// UV Light Sensor
void UV_Setup (){
uv.begin(VEML6070_1_T); // pass in the integration time constant
}
void UV_Loop (){
lcd.setCursor(0, 2);
lcd.print("UV light level: ");
lcd.print(uv.readUV());
}

Excellent. Now people can try to help :smiley:
Very cool project, btw :+1:

Not posting in code tags makes the code difficult to copy to the IDE or a text editor for examination.

Not formatting the code makes it hard to read and follow.

We ask that code be formatted and posted in code tags for good reasons, not to be mean or petty.

Please read the forum guidelines.

As said, please post the complete code using the code tags. There appear to be missing sections of code.

For trouble shooting there are a few things to do.

Do you know if the entire program has stopped or just the lcd display? You can put a blink without delay style heartbeat using the onboard led to verify what is running.

If you physically disconnect the relays, does the program still freeze? Relays firing frequently create problems for lcd displays.

There maybe issues on the i2c bus. What is the longest wire run for an i2c device. What pullups are being used?

there he is again .. in one tap :rose:

//DHT22 Temperature & Humidity Sensors

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


//Constants
#define DHTPIN1 8  //   ------ out
#define DHTPIN2 13 //   ------ in

#define DHTTYPE DHT22   // DHT 22  (AM2302)

DHT dht1(DHTPIN1, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
DHT dht2(DHTPIN2, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino


//Variables

int chk ;
int dhtCount;

float hum1  ;  //Stores humidity 1 value
float temp1  ; //Stores temperature 1 value

float hum2  ;  //Stores humidity 2 value
float temp2  ; //Stores temperature 2 value


//----------------------------------------------------------------------------
//LCD

#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = A1, en = A2, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


//----------------------------------------------------------------------------
//Load Cell

#include "HX711.h"

// HX711 circuit wiring

const int LOADCELL_DOUT_PIN = 49;
const int LOADCELL_SCK_PIN = 48;

HX711 scale;

float weight  ;
float Weight  ;


//---------------------------------------------------------------------------
//Air Flipping Fan

#define fan A0

//--------------------------------------------------------------------------
//SD Card

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

File dataFile  ;



//--------------------------------------------------------------------------
//DS1307 RTC Time & Clock

#include <TimeLib.h>
#include <DS1307RTC.h>



//---------------------------------------------------------------------------
// push Button

const char  Button =  6 ;
bool Pressed = false ;

//---------------------------------------------------------------------------
// UV Light Sensor

#include "Adafruit_VEML6070.h"

Adafruit_VEML6070 uv = Adafruit_VEML6070();

//---------------------------------------------------------------------------


int Heat_Lamp = 10 ;

/* Timming Setup */

const unsigned long event_1_time  = 1000    ;  // DHT22 Temperature & Humidity Sensors ( 1s )
const unsigned long event_2_time  = 20000   ;  //  DC fan OFF"  ( 20s )
const unsigned long event_3_time  = 80000   ;  //  DC fan  ON'  ( 1m )
const unsigned long event_4_time  = 5000    ;  // LCD Refresh ( 5s )
const unsigned long event_5_time  = 250     ;  // Load Cell Weight ( 0.25s )
const unsigned long event_6_time  = 300000  ;  // SD Card Saving Time ( 5m )
const unsigned long event_7_time  = 1000    ;  // UV Light Sensor ( 1s )





unsigned long Previous_1_time  = 0 ;
unsigned long Previous_2_time  = 0 ;
unsigned long Previous_3_time  = 0 ;
unsigned long Previous_4_time  = 0 ;
unsigned long Previous_5_time  = 0 ;
unsigned long Previous_6_time  = 0 ;
unsigned long Previous_7_time  = 0 ;


//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

void setup()
{

  //Serial.begin(9600);

  LCD_Setup();

  for (int i = 7 ; i >= 0 ; i-- )
  {
    lcd.clear ();
    lcd.setCursor (i, 0) ;
    lcd.print("Welcome To ..");
    delay (500);
  }
  lcd.setCursor (0, 1);
  lcd.print("       Drying       ");
  lcd.setCursor (0, 2);
  lcd.print("   Agri. Products   ");
  lcd.setCursor (0, 3);
  lcd.print("  by  Green  House  ");
  delay (5000);
  lcd.clear ();


  RTC_Loop   ();
  delay (2000) ;
  PB_Setup   ();
  DHT_Setup  ();
  UV_Setup   ();
  FFan_Setup ();
  LC_Setup   ();
  SD_Setup   ();
  HL_Setup   ();



}

uint16_t line = 1 ;

void print2digits_sd(int number_sd) {
  if (number_sd >= 0 && number_sd < 10) {
    dataFile.write('0');
  }
  dataFile.print(number_sd);
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    lcd.write('0');
  }
  lcd.print(number);
}

//DHT22 Temperature & Humidity Sensors

void DHT_Setup () {
  
  dht1.begin();
  dht2.begin();
  

}

void DHT_Loop () {


    switch( dhtCount)
    {
      case 0:
          //Read data and store it to variables hum and temp
          hum1 = dht1.readHumidity();
          temp1 = dht1.readTemperature();
          //Print temp and humidity values to LCD 
          lcd.setCursor(0, 0);
          lcd.print("Humidity O: ");
          lcd.print(hum1);
          lcd.print(" %");
          lcd.setCursor(0, 1);
          lcd.print("TempO: ");
          lcd.print(temp1);
          lcd.print(" Celsius"); 
        break;
      case 1:
          //Read data and store it to variables hum and temp
          hum2 = dht2.readHumidity();
          temp2 = dht2.readTemperature();
          //Print temp and humidity values to LCD 
          lcd.setCursor(0, 0);
          lcd.print("Humidity I: ");
          lcd.print(hum2);
          lcd.print(" %");
          lcd.setCursor(0, 1);
          lcd.print("TempI: ");
          lcd.print(temp2);
          lcd.print(" Celsius");
        break;
      
    }
    
    dhtCount++;
    if( dhtCount > 1)
      dhtCount = 0;
  }

  //Air Flipping Fan  

void FFan_Setup (){
  
  pinMode ( fan , OUTPUT );
}

void FFan_ON_Loop (){
  
  digitalWrite (fan , HIGH );
  
  }

void FFan_OFF_Loop (){

  digitalWrite (fan , LOW );

}

//Heat Green House Air By Lamp

void HL_Setup (){
  
  pinMode ( Heat_Lamp , OUTPUT );
}

void HL_ON_Loop (){
  
 digitalWrite ( Heat_Lamp , HIGH );
  
}

void HL_OFF_Loop (){
  
  digitalWrite ( Heat_Lamp , LOW );
  
}

//LCD

void LCD_Setup(){
  
    lcd.begin(20,4);
}

void LCD_Loop (){

 lcd.begin(20,4);
  
}

//Load Cell

void LC_Setup (){
  
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN) ;
  scale.set_scale(115.50) ;                           // this value is obtained by calibrating the scale with known weights; see the README for details
  //scale.tare(50) ;                                   // reset the scale to 0
  
}


void LC_Loop (){
  
  
  
  
  weight = scale.get_units()+( scale.get_units()* 0.0159 ) ; // Correction Value " 2%"
  
  if(weight <= 5.0)
  {
    weight = 0.0;
  }
  if(weight<1000)
  {
  //lcd.clear();
  lcd.setCursor(0, 3);
  lcd.print("Weight = ");
  lcd.print(weight); //(000.00)
  lcd.print(" g   ");
  }
  if(weight>=1000)
  {
  //lcd.clear();
  Weight = weight/1000;
  lcd.setCursor(0, 3);
  lcd.print("Weight = "); //(00.00)
  lcd.print(Weight);
  lcd.print(" kg   ");
  }
}

// push Button

void PB_Setup (){

  pinMode ( Button , INPUT_PULLUP );
  
}

void PB_loop (){
  
 bool Button_State = digitalRead (Button) ;
 
  if ( Button_State == Pressed  ){
   
  delay (10000); // Hold Down Timing ( 10s )
  
  } 
}

//DS1307 RTC Time & Clock

void RTC_Setup (){


}



void RTC_Loop (){

  tmElements_t tm ;
  
  if (RTC.read(tm)) {
    
    lcd.setCursor(0, 0);
    lcd.print("Time :");
    lcd.setCursor(6, 1);
    print2digits(tm.Hour);
    lcd.write(':');
    print2digits(tm.Minute);
    lcd.write(':');
    print2digits(tm.Second);
   if ( tm.Hour > 12 ){
      lcd.write(" PM");
    }
    else {lcd.write(" AM");}
    lcd.setCursor(0,2);
    lcd.print("Date :");
    lcd.setCursor(8,3);
    lcd.print(tm.Day);
    lcd.write('/');
    lcd.print(tm.Month);
    lcd.write('/');
    lcd.print(tmYearToCalendar(tm.Year));
  }
  
}

//SD Card

void SD_Setup (){
  
  
  lcd.clear ();
  lcd.setCursor (0,0);
  lcd.print("Initializing SD card");
  delay (5000);
  if (!SD.begin()) {
    lcd.setCursor (0,1);
    lcd.print("initialization    ");
    lcd.setCursor (0,2);
    lcd.print("           failed!");
    delay (2000);
    while (1);
    lcd.clear ();
  }
  lcd.setCursor (0,3);
  lcd.print("initialization done.");
  delay (2000);
  lcd.clear ();

}

void SD_Loop (){

  

  dataFile = SD.open("dry.TXT", FILE_WRITE);

  tmElements_t tm ;
  
  if (RTC.read(tm)) {
    
    dataFile.print(line++);
    dataFile.print(".  Time : ");
    print2digits_sd(tm.Hour);
    dataFile.write(':');
    print2digits_sd(tm.Minute);
    dataFile.write(':');
    print2digits_sd(tm.Second);
    dataFile.write(' ');
    if ( tm.Hour > 12 ){
      dataFile.write(" PM ");
    }
    else {dataFile.write(" AM ");}
    dataFile.write(',');
    dataFile.print(" Date : ");
    dataFile.print(tm.Day);
    dataFile.write('/');
    dataFile.print(tm.Month);
    dataFile.write('/');
    dataFile.print(tmYearToCalendar(tm.Year));
  }

  

    dataFile.print(" : Temperature Out = ");
    dataFile.print(temp1);
    dataFile.print(" °C , Humidity Out = ");
    dataFile.print(hum1);
    dataFile.print(" %");
    dataFile.print(" || Temperature In = ");
    dataFile.print(temp2);
    dataFile.print(" °C , Humidity In = ");
    dataFile.print(hum2);
    dataFile.print(" %");
    dataFile.print(" || UV light level: ");
    dataFile.print(uv.readUV());
    //dataFile.print(" lux ");
    
    
    
    if(weight <= 5.0)
  {
    weight = 0.0;
  }
  if(weight<1000)
  {
  dataFile.print(" || Weight = ");
  dataFile.print(weight);
  dataFile.println(" g");
  }
  if(weight>=1000)
  {
  dataFile.print(" || Weight = ");
  dataFile.print(Weight);
  dataFile.println(" kg");
  }
  
      dataFile.close();


}

// UV Light Sensor

void UV_Setup (){
  
   uv.begin(VEML6070_1_T);  // pass in the integration time constant
   
}


void UV_Loop (){
  
  lcd.setCursor(0, 2);
  lcd.print("UV light level: "); 
  lcd.print(uv.readUV());
  
}

void loop() {

  PB_loop ();

  if ( temp2 <= 35.0 ) {
    HL_ON_Loop ();
  }
  if ( temp2 >= 40.0 ) {
    HL_OFF_Loop ();
  }


  /* start record time with millis */
  unsigned long Current_time = millis ();


  //---------------------------------------------------------------
  //DHT22 Temperature & Humidity Sensors  .. " 1 "

  if (Current_time - Previous_1_time >=  event_1_time ) {
    DHT_Loop ();
    Previous_1_time = Current_time ;
  }

  //--------------------------------------------------------------
  //Air Flipping Fan.. " 2 , 3 "

  if (Current_time - Previous_2_time >=  event_2_time ) {
    FFan_ON_Loop ();
    Previous_2_time = Current_time ;
  }

  if (Current_time - Previous_3_time >=  event_3_time ) {
    FFan_OFF_Loop ();
    Previous_3_time = Current_time ;
  }

  //----------------------------------------------------------------
  // LCD .. " 4 "

  if (Current_time - Previous_4_time >=  event_4_time ) {
    LCD_Loop ();
    Previous_4_time = Current_time ;
  }


  //----------------------------------------------------------------
  //Load Cell .. " 5 "

  if (Current_time - Previous_5_time >=  event_5_time ) {
    LC_Loop ();
    Previous_5_time = Current_time ;
  }

  //---------------------------------------------------------------------
  //SD Card .. " 6 "

  if (Current_time - Previous_6_time >=  event_6_time ) {
    SD_Loop ();
    Previous_6_time = Current_time ;
  }

  //---------------------------------------------------------------------
  //UV Light Sensor .. " 7 "

  if (Current_time - Previous_7_time >=  event_7_time ) {
    UV_Loop ();
    Previous_7_time = Current_time ;

  }



}