Data logging to sd card with a millis function

Hello everyone, and excuse me at the beginning for my bad English and bad programming . :sweat_smile:
I am currently working on my graduation project from the university. It used Arduino UNO as a main board , 3 sensors of a type DHT22 for Temp. and Humidity, 1 sensor of a type BH1750 for Light intensity, and 20kg Load cell with HX711 board and a 20*4 LCD screen to display the readings and SD card Module and small bush Button to frezze the readings .
I know there are a lot of sensitivities, unfortunately :disappointed_relieved:
Most of the codes are pieces of other codes copied and pasted only. I divided the code and organized it well and tested each component with an individual code.. and it all works fine .
But the problem is when you put all the input together
Specifically, the code for saving the data on the card and the rest of the code .. knowing that it works efficiently on its own, and this works efficiently on its own .

I hope to find someone to help me solve this problem.. and thank you to everyone

This was the file saved in the test card alone :

Lcd When Showing Data :

full code :
Final_Head.zip (5.1 KB)

Hi, welcome to the forum.

Can you give a schematic of how everything is connected ?
Or a list of the used pins ?
Can you tell which SD module you have ? Some modules are only for 3.3V.
Can you tell what pressing the button is for ? Is that to safely remove the SD card ? Then you could show a message on the display to remove the SD card.

Intro

I can see the problem. To make everything run smooth and each part happily run on its own, you have to remove all the delay(). The main structure of the code is okay, but it needs some adjusting here and there. I prefer that the button does not use a long delay but uses a millis-timer.

LCD

The lcd.begin() should only be in setup(), not in the loop().
Updating text on a display is often done by adding extra spaces that clears the previous text. That means you don't need to clear the whole display, but work only on that part of the display.

DHT

You don't need three millis-timers for three DHT sensors. Just one millis-timer that collects data from all three will do. If you want to use them one after another, then you can do this:

int dhtCount;

void loop()
{
  if( currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;

    switch( dhtCount)
    {
      case 0:
        // do the first DHT sensor
        break;
      case 1:
        // do the second DHT sensor
        break;
      case 2:
        // do the third DHT sensor
        break;
    }
    dhtCount++;
    if( dhtCount > 2)
      dhtCount = 0;
  }
}

It is possible to make your own characters for the display.
I think that °C is nicer than Celsius.
Perhaps "29.3" is better than "29.30".

The humidity as "48.60" ? Would just "48" be enough ? The DHT sensor are not accurate.

Fan

The fan is just one fan, so it needs one millis-timer.
Remove the delay from the fan functions and use one millis-timer.
You can change the interval of a millis-timer or turn a millis-timer on and off. You can do whatever you want with a running or not-running millis-timer. I don't understand what the fan should do.

Scale

The scale has a delay of 250ms. Can you re-organize the code to avoid that delay ?
What about this:

millis-timer
{
  power_up
  do the scale thing
  power_down
}

On the LCD display the text is "L_C val:". Would "Weight" be better ?

Button

Ten seconds delay after pressing a button ? That is so long ! That delay has to go completely, to let the sketch do all the other things.
You should start by debouncing the button, there are libraries for that.
Perhaps you also need the State Change Detection, but some debouncing libraries have that already.

Serial Monitor

There is no Serial.begin() and no Serial.println() in your sketch. I think you really need that and show a lot of messages to the Serial Monitor to know what is going on.

SD

If you write something every second to the SD card, then I would open and close that file every time.
This code is not okay:

if (Current_time >= timeLimit) {
    dataFile.close(); //close the file
  }

Can you explain what you wanted to do with that code ?

1 Like

First I would like to thank you for your help :rose:

This graduation project is a solar dryer that dries agricultural products using solar energy and is completely self-reliant , All of these sensors to read the temperature and humidity outside and inside the closed greenhouse and to know the passing lighting by isolating the greenhouse.

  • The fan in particular works to distribute the air inside the greenhouse
    It only works for two minutes every ten minutes .

  • The button is used to stop the update of the readings already for 10 seconds in order to take the readings manually.

.. And I forgot to say that I use Tiny RTC to know Time and Date

Arduino .. pin " 1 " : bush Button
pin " 2 (D4) , 3 (D5) , 4 (D6) , 5 (D7) " ، " A1 (RS) , A2 (E) " : LCD
pin " 6 , 7 , 8 " : DHT22 Sensors outputs
pin " 9 (DT) " ، " A3 (SCK) " : HX711 ' load cell analog to digital converter board '
pin " 10 (CS) , 11 (MOSI) , 12 (MISO), 13 (SCK) " : SD Card Module
pin " A0 " : Fan Relay
pin " A4 (SDA) , A5 (SCL) " : BH1750 , Tiny RTC

it is a " MicroSD Card Adapter " works with voltage divider and 3.3v regulator

I will give it a try and work on your advice right now ..

Is there any device in your project, other than the SD card reader, that uses SPI?

1 Like

No . just uploading the code to arduino by USBasp
But I removed it after uploading the code

Ok. I just wondered because the typical microSD card module doesn't play well with other SPI devices. But if you dont' have any such devices, then the problem must be elsewhere.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.