L
L
lengast2020-11-28 12:22:36
Arduino
lengast, 2020-11-28 12:22:36

Why can ARDUINO and SD freeze?

Good afternoon.

The board should turn on / off the heater depending on the temperature. And also write the LOG file to the SD card in parallel.

The problem is that after 1.5-2 hours of work, the board completely rises, or starts writing some kind of nonsense to the SD card. What could be the problem.
I am attaching the code.
And gentlemen, thanks in advance.
Arduino mega
BME-280
MicroSd card adapter.

//#include "logger.h"
#include "pin_map.h"
#include "settings.h"

// библиотека для работы I²C
#include <Wire.h>

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

#include <TimerOne.h>

// библиотека для работы с метеосенсором
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

// создаём объект для работы с датчиком в камере
Adafruit_BME280 bme;

// Переменные для хранения метеоданных 
volatile float pressure = 0;
volatile float temperature = 0;
volatile float humidity = 0;

unsigned long timestampTelemetryWrite = 0;

//переменные для диода
boolean InitSD;
boolean InitMeteo;

void writeSD(String str)
{
    File logFile = SD.open("log1.txt", FILE_WRITE);

    // if the file is available, write to it:
    if (logFile) 
    {
        logFile.println(String(millis()) + ":::" + " "+ str);
        logFile.close();
        Serial.println("Write to log1.txt---OK");
        InitSD = true;
    }
    // if the file isn't open, pop up an error:
    else 
    {
        Serial.println("error opening log1.txt");
        InitSD = false;
    }
}

void initMeteoSensor()
{
    // печатаем сообщение об успешной инициализации Serial-порта
    Serial.println("Meteo begin init...");
    writeSD("Meteo begin init...");
    // начало работы с датчиком
    unsigned status;

    status = bme.begin(0x76);
    if (!status) 
    {
        Serial.println("Meteo Sensor init FAIL");
        writeSD("Meteo Sensor init FAIL");
        InitMeteo = false;
    }
    else
    {
        Serial.println("Meteo Sensor init OK");
        writeSD("Meteo Sensor init OK");
        InitMeteo = true;
    }
}

void updateSensorData()
{
    pressure = bme.readPressure() / 100.0f;
    temperature = bme.readTemperature();
    humidity = bme.readHumidity();
}

void warmControl()
{
    Serial.println("Temperature in camera:" + String(temperature));
    writeSD("Temperature in camera:" + String(temperature));
    // Если температура ниже минимальной 
    if (temperature < TEMPERATURE_IN_CAMERA_MIN)
    {
        // то включаем подогрев
        digitalWrite(WARM_CONTROL_PIN, ON);
        Serial.println("Warm is ON");
        Serial.println("Write WARM_CONTROL_PIN = " + String(ON));
        writeSD("Warm is ON");
    }
    // Если больше максимальной
    else if (temperature > TEMPERATURE_IN_CAMERA_MAX)
    {
        // То выключаем подогрев 
        digitalWrite(WARM_CONTROL_PIN, OFF);
        Serial.println("Warm is OFF");
        Serial.println("Write WARM_CONTROL_PIN = " + String(OFF));
        writeSD("Warm is OFF");
    }
    else
    {
        /* code */
    }

    if (millis() - timestampTelemetryWrite > TELEMETRY_DELAY_MS)
    {
        Serial.println("Telemetry Write Start");
        writeSD("Telemetry Write Start");
        String result = "";

        result += String(millis()/60000);
        result += ":min";
        result += ":::";
        result += " TemperatureInCamera = " + String(temperature) + " ";
        result += " PressureInCamera = " + String(pressure) + " ";
        result += " HumidityInCamera = " + String(humidity) + " ";
        result += "\n";
    
        Serial.println(result);
    
        File dataFile = SD.open("datalog1.txt", FILE_WRITE);

        // if the file is available, write to it:
        if (dataFile) 
        {
            dataFile.println(result);
            dataFile.close();
            Serial.println("Write to datalog1.txt---OK");
        }
        // if the file isn't open, pop up an error:
        else 
        {
            Serial.println("error opening datalog1.txt");
        }
        timestampTelemetryWrite = millis();
        Serial.println("Telemetry Write End");
        writeSD("Telemetry Write End");
    }
}

void setup() 
{
    Serial.begin(115200);
    // Init Sensor
    initMeteoSensor();

    if (!SD.begin(SD_CARD_ENABLE_PIN)) 
    {
        Serial.println("Card failed, or not present");
        // don't do anything more:
    }
    Serial.println("card initialized.");

    // Настройка пина управления подогревателем
    pinMode(WARM_CONTROL_PIN, OUTPUT);
    pinMode(LED_PIN, OUTPUT);
}

void loop() 
{
    static unsigned long timestampSensorUpdate = 0;
    static unsigned long warmControlTimestamp = 0;
  if (millis() - warmControlTimestamp > 1000)
  {
        // контроль температуры в барокамере
        //Serial.println("warmControl");
        //writeSD("warmControl");
        warmControl();
        warmControlTimestamp = millis();
  }



    if (millis() - timestampSensorUpdate > 1000)
    {
        updateSensorData();
        timestampSensorUpdate = millis();
    }

    if (InitSD and InitMeteo)
    {
      digitalWrite(LED_PIN, HIGH);
    }
    else
    {
      digitalWrite(LED_PIN, LOW);
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
BasiC2k, 2020-11-28
@BasiC2k

This can happen if there is an overflow of some variable. Check the code.

L
lonelymyp, 2020-11-28
@lonelymyp

What do you include? There may be interference if there is a relay and did not take care of the high-quality power supply of the controller.

V
VT100, 2020-11-29
@VT100

+1 lonelymyp . What is the installation scheme, power and voltage of the heater, how does it turn on, etc.?
Wire.h - is it i2c through the "legs" or hardware? If the first - wool its code for resistance to interference. For starters, you can try to reduce the resistance of the "pullups" to the minimum possible (according to the allowable BMP inflowing current). If the situation improves - bad code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question