I make weight logger by 4 separate load cell, hx711 module, sd card and RTC module.
when ram use more than 83% my program not work and i cant complete my program.
how can i manage ram and library. can i light libraries and delete unnecessary methods?
Without seeing your code I couldn't guess where the memory is being used. SD library uses a big chunk of memory, but not that much, and none of the other libraries are memory hogs, so it's something in your sketch
Do you have lots of print statements like Serial.print("the weight is ");
?
If so, try Serial.print(F("the weight is "));
in all such cases
Thanks. Serial.print(F("the weight is ")); good idea and i will test it.
but how i can past cod in this page?
#include <SD.h>
#include <SPI.h>
#include <EEPROM.h>
#include <Rtc_Pcf8563.h>
//#include <TimerOne.h>
#include <HX711.h>
#define version "this is Weight Logger V 1.6.3"
#define intervalMicroS 10000// 10 milli seconds
#define limitcount 60000//10m60s100hz
#define jsonFile true// If you want file in csv format change this "true" to "false".
//HX711(byte dout, byte pd_sck, byte gain = 128)
HX711 Load4(6, A1);
HX711 Load1(3, 2);
HX711 Load3(5, A2);
HX711 Load2(8, A3);
Rtc_Pcf8563 rtc;
File myFile;
String fileName = "name8chr.txt";
String pTime = "00:00:00";
#define btn 7
bool sd = true;
int sensNum = 0;
long calibration_factor1 = -56400;
long calibration_factor2 = -56400;
long calibration_factor3 = -56400;
long calibration_factor4 = -56400;
long logCount = -1;
float weight[5];
bool logging = false;
bool endLog = false;
bool fileOpen = false;
void setup()
{
pinMode(btn, INPUT_PULLUP);
if (!SD.begin(4)) {
Serial.println("Cannot connect SD card.");
sd = false;
}
Serial.begin(38400);
Serial.println(version);
Serial.print("Date: ");
Serial.print(rtc.formatDate());
Serial.print("\t");
Serial.print("Time: ");
///this section use for set time********************
//// Uncomment 2 below line and filltime date, upload one time to set your *
//// time to RTC module and comment 2 line and upload program immediately. *
//// .setDateTime(day, weekday, month,0, year, hour minute, sec); *
//// example: setDateTime(24, 3, 1,0, 18, 22 ,15, 0); *
//// time setting Code:____________________________________________________________*
//rtc.initClock(); *
//rtc.setDateTime(22, 7, 1, 0, 18, 0, 22,0); *
////***********************************************************************************
Serial.println(rtc.formatTime());
Load1.tare();
Load2.tare();
Load3.tare();
Load4.tare();
Load1.set_scale(calibration_factor1);
Load2.set_scale(calibration_factor2);
Load3.set_scale(calibration_factor3);
Load4.set_scale(calibration_factor4);
// Serial.println("To calibration sensors remove all weight from scale and send me 'c' \r\n\t else program run after 5 second ");
Timer1.initialize(intervalMicroS);
Timer1.stop();
Timer1.attachInterrupt(Log);
Serial.println("Push button to start recording");
}
void Log()
{
if (logCount++ >= limitcount)
{
Timer1.stop();
if (jsonFile)
{
myFile.print("\r\n]\r\n}\r\n}");
}
myFile.close();
fileOpen = false;
endLog = true;
//Serial.print("end\t");
logging = false;
return;
}
if (!logging)
{
Timer1.stop();
myFile.close();
return;
}
//Serial.print("ID: ");
Serial.print(logCount);
Serial.print("\t");
weight[1] = Load1.get_units(1);
weight[2] = Load2.get_units(1);
weight[3] = Load3.get_units(1);
weight[4] = Load4.get_units(1);
for (int i = 1; i < 5; i++)
{
if (weight > -0.01 && weight < 0)
_ weight = 0;
Serial.print(weight*, 2);
Serial.print("\t");
}
Serial.println();
if (fileOpen)
{
if (jsonFile)
{
myFile.print("\t\t{"id": ");
myFile.print(logCount);
myFile.print(", "weights":[ ");
}
else*
* {
myFile.print("ID: ");
myFile.print(logCount);
myFile.print("\t");
}
for (int i = 1; i < 5; i++)
{
if (jsonFile)
{
myFile.print(weight, 2);
if (i < 4)
myFile.print(", ");
}
else*
* {
myFile.print(weight, 2);
myFile.print("\t");
}
}
if (jsonFile)
{
myFile.print("]}");
if (logCount<limitcount)
myFile.println(",");
}
}
}
void loop()
{
if (endLog)
{
endLog = false;
Serial.print("Start time\t");
Serial.println(pTime);
Serial.print("End time\t");
Serial.println(rtc.formatTime());
Serial.println("-------------------------------------");
if (jsonFile)
{
}
else*
* {
}
}
if (!digitalRead(btn))
{
delay(20);
if (!digitalRead(btn))
{
if (logging)
{
logging = false;
Serial.println("stop");
while (!digitalRead(btn));
delay(20);
Timer1.stop();
}
else*
* {
CreateFile();
logging = true;
Serial.print("Recording ");
Serial.print(limitcount);
Serial.print(" samples every ");
Serial.print(intervalMicroS / 1000);
Serial.print(" milli second for ");
long time = intervalMicroS / 1000;
time = time * limitcount;
time = time / 1000;
Serial.print(time);
Serial.println(" seconds ");
Serial.println("start:");
while (!digitalRead(btn));
delay(20);
logCount = 0;
Serial.print(rtc.formatTime());
Serial.print("\t");
Serial.println(rtc.formatDate());
Serial.print("ID\t");
Serial.print("SN1\t");
Serial.print("Sn2\t");
Serial.print("SN3\t");
Serial.println("SN4");
if (fileOpen)
{
if (jsonFile)
{
myFile.print("{\r\n\t"weightLog": {\r\n\t"date": "");
myFile.print(rtc.formatDate());
myFile.print("",\r\n\t"time": "");
myFile.print(rtc.formatTime());
myFile.print("",\r\n\t"sample-no": ");
myFile.print(limitcount);
myFile.print(",\r\n\t"interval-ms": ");
myFile.print(intervalMicroS / 1000);
myFile.print(",\r\n\t"unit": "kg",\r\n\t"samples" : [\r\n");
}
else*
* {
myFile.print(rtc.formatTime());
myFile.print("\t");
myFile.println(rtc.formatDate());
myFile.print("ID\t");
myFile.print("SN1\t");
myFile.print("Sn2\t");
myFile.print("SN3\t");
myFile.println("SN4");
}
}
pTime = rtc.formatTime();
Timer1.start();
}
}
}
}
void CreateFile() {
fileName = rtc.formatTime();
fileName.replace(':', '-');
//if (jsonFile)
// fileName += ".json";
//else*
* fileName += ".txt";
if (SD.exists(fileName))
{
fileName = "2-" + fileName;
}
Serial.print("file name = ");
Serial.println(fileName);_
myFile = SD.open(fileName, FILE_WRITE);
_ if (myFile)
{
fileOpen = true;
}
else*
* {
Serial.println("error opening file");
}
}*_
You missed out all the "Serial.print (F("YOUR STUFF HERE"));", and you're supposed to use code tags, otherwise all your indices using 'i' turn to italics
Anyone that complains about memory issues, while using the String class, deserves all the problems they are having.
@Anarit, to make it easy for people to help you please modify your post and use the code button </> so your code looks like this
and is easy to copy to a text editor. See How to use the Forum
Your code is much too long for me to study quickly without copying to a text editor.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
...R