Not enough memory, Coding troubleshooting.

Hi, everyone.

I could really need your help.
There's an error when I want to verify my code.
Which is [processing.app.debug.RunnerException: Not enough memory; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing your footprint]

Could someone help me to solve this matter?
Most of the codes are useful to me. If I delete one line of the code, especially the library and #define, many errors will come out.

Here is my code.

#include <DS3232RTC.h>    //http://github.com/JChristensen/DS3232RTC
#include <Time.h>         //http://www.arduino.cc/playground/Code/Time
#include <Wire.h>         //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
#include <TimeAlarms.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define THERMISTORPIN A0
#define NUMSAMPLES 5
#define SERIESRESISTOR 30000
#define A  0.0008447572316
#define B  0.0002195170370
#define C  0.0000001061307230

AlarmId id;
const int buzzer = 12;
int pinCS = 4;
int samples[NUMSAMPLES];
File dataFile;

void setRtc()
{
  if (timeStatus() != timeSet)
  {
    Serial.println("Unable to sync with the RTC");
  }
  else
  {
    Serial.println("RTC has set the system time");
  }
  // create the alarms, to trigger at specific times
  Serial.println("RTC create the alarms, to trigger at specific times");
  Alarm.alarmRepeat(10, 33, 0, MorningAlarm); // 8:30am every day
  // create timers, to trigger relative to when they're created
  Serial.println("RTC create timers, to trigger relative to when they're created");
  Alarm.timerRepeat(15, Repeats);           // timer for every 15 seconds
  id = Alarm.timerRepeat(2, Repeats2);      // timer for every 2 seconds
  Alarm.timerOnce(10, OnceOnly);            // called once after 10 seconds
}

void setOled()
{
  // by default, generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);
  // Clear the buffer.
  display.clearDisplay();
  delay(2000);
  display.clearDisplay();

  // text display tests
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("TempIoT1.0");
  display.display();
  delay(2000);
  display.clearDisplay();
  pinMode(pinCS, OUTPUT);
  delay(2000);
  // SD Card Initialization
  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
  // Create/Open file
  dataFile = SD.open("test.txt", FILE_WRITE);
  if (dataFile)
  {
    Serial.println("Done.");
  }
  // if the file didn't open, print an error:
  else
  {
    Serial.println("error opening test.txt");
  }


}
void setup()
{
  Serial.begin(9600);;
  analogReference(EXTERNAL);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  while (!Serial);  //wait for serial
  pinMode(buzzer, OUTPUT);
  setRtc();
  setOled();
  delay(2000);   
  processD();
}

void loop() {}

void processD()
{
  int i;
  //int data;
  double average;
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);

  // take N samples in a row, with a slight delay
  for (i = 0; i < NUMSAMPLES; i++) {
    samples[i] = analogRead(THERMISTORPIN);
    delay(50);  //50 5 bacaan
  }

  // average all the samples out
  average = 0;
  for (i = 0; i < NUMSAMPLES; i++) {
    average += samples[i];
  }
  average /= NUMSAMPLES;
  // convert the value to resistance
  average = 1023 / average - 1;
  average = SERIESRESISTOR / average;

  double steinhart1, steinhart2, steinhart3, steinhart4, steinhart5;

  Serial.println("start");
  steinhart1 = log(average);                   // (ln R)
  steinhart2 = C * steinhart1 * steinhart1 * steinhart1;    // C * (ln R)***
  steinhart3 = (B * steinhart1) + steinhart2;                 // B*(ln R) + C * (ln R)***
  steinhart4 = 1.0 / (A + steinhart3); // 1/(A + B*(ln R) + C * (ln R)***)
  steinhart5 = steinhart4 - 273.15;                         // convert to C

  Serial.print("Temperature ");
  display.print("Body Temp = ");
  Serial.print(steinhart5);
  display.print(steinhart5);
  Serial.println(" *C");
  display.println(" *C");
  display.display();
  dataFile.println(steinhart5);
  dataFile.close();

  // Reading the file
  dataFile = SD.open("test.txt");
  if (dataFile) {
    Serial.println("Read:");
    // Reading the whole file
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
    dataFile.close();
  }
  else {
    Serial.println("error opening test.txt");
  }
  delay(5000);
  //display.clearDisplay();


}

//~~~~~~~~~~~~~~~~~~~~~~~~ TIME SETTING ~~~~~~~~~~~~~~~~~~~~~~~~~~
void MorningAlarm() {
  Serial.println("Alarm: - turn lights off");
  buzz();
}

void ExplicitAlarm() {
  Serial.println("Alarm: - this triggers only at the given date and time");
}

void Repeats() {
  Serial.println("15 second timer");
}

void Repeats2() {
  Serial.println("2 second timer");
}

void OnceOnly() {
  Serial.println("This timer only triggers once, stop the 2 second timer");
  // use Alarm.free() to disable a timer and recycle its memory.
  Alarm.free(id);
  // optional, but safest to "forget" the ID after memory recycled
  id = dtINVALID_ALARM_ID;
  // you can also use Alarm.disable() to turn the timer off, but keep
  // it in memory, to turn back on later with Alarm.enable().
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(' ');
  Serial.print(day());
  Serial.print(' ');
  Serial.print(month());
  Serial.print(' ');
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(':');
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void buzz() {

  tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec
  tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec
  tone(buzzer, 1000); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec

}

Please, everyone. I appreciate all your kindness.
Thanks.

The first thing to do is use the F macro to move string literals out of SRAM. A string literal is like "I am a literal". There are some in serial prints. Use the F macro like this: Serial.print(F("I am a literal")).

Hi, groundfungus.

Just now I've tried your suggestion and it's great, the error is missing.
But now only its say that "Low memory available, stability problems may occur."

Can you teach me how to simplify the way of using the F macro?
Because it will a time to change the sentences to F macro.

By the way, as you say it's about SRAM.
How about PROGMEM?
Can PROGMEM help my problem too?

The SD card library is notorious for its huge size.

Instead of
#include <SD.h>
try SdFat.h

Hi, CrossRoads.

I've just now tried to replace the library to #include <SdFat.h>.
but more error coming, because of the library.
Can you look at my code and make me understand better how to change it.

Sir CrossRoads,

Try look at my coding.
I've changed it to be compatible with the library. It shows 'ok' when I verify it.
But when I try to upload it to my Arduino Yun Mini, the same error come out.
[Not enough memory]

Here is the code.

#include <DS3232RTC.h>    //http://github.com/JChristensen/DS3232RTC
#include <Time.h>         //http://www.arduino.cc/playground/Code/Time
#include <Wire.h>         //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
#include <TimeAlarms.h>
#include <SPI.h>
#include <SdFat.h>
//#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define THERMISTORPIN A0
#define NUMSAMPLES 5
#define SERIESRESISTOR 30000
#define A  0.0008447572316
#define B  0.0002195170370
#define C  0.0000001061307230

AlarmId id;
const int buzzer = 12;
int pinCS = 4;
int samples[NUMSAMPLES];
//File dataFile;
SdFat sd;
SdFile dataFile;



void setRtc()
{
  if (timeStatus() != timeSet)
  {
    Serial.println(F("Unable to sync with the RTC"));
  }
  else
  {
    Serial.println(F("RTC has set the system time"));
  }
  // create the alarms, to trigger at specific times
  Serial.println(F("RTC create the alarms, to trigger at specific times"));
  Alarm.alarmRepeat(10, 33, 0, MorningAlarm); // 8:30am every day
  // create timers, to trigger relative to when they're created
  Serial.println(F("RTC create timers, to trigger relative to when they're created"));
  Alarm.timerRepeat(15, Repeats);           // timer for every 15 seconds
  id = Alarm.timerRepeat(2, Repeats2);      // timer for every 2 seconds
  Alarm.timerOnce(10, OnceOnly);            // called once after 10 seconds
}

void setOled()
{
  // by default, generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);
  // Clear the buffer.
  display.clearDisplay();
  delay(2000);
  display.clearDisplay();

  // text display tests
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println(F("TempIoT1.0"));
  display.display();
  delay(2000);
  display.clearDisplay();
  delay(2000);
}

void setSd()
{
  // SD Card Initialization
  if (!sd.begin(pinCS, SPI_HALF_SPEED)) sd.initErrorHalt();

  // open the file for write at end like the Native SD library
  if (!dataFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }
}

void setup()
{
  Serial.begin(9600);;
  analogReference(EXTERNAL);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  while (!Serial);  //wait for serial
  pinMode(buzzer, OUTPUT);
  pinMode(pinCS, OUTPUT);
  setRtc();
  setOled();
  setSd();
  delay(2000);    //MAKE SURE FIX DON'T CHANGE
  l();
}

void loop() {}

void l()
{
  int i;
  //int data;
  double average;
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);

  // take N samples in a row, with a slight delay
  for (i = 0; i < NUMSAMPLES; i++) {
    samples[i] = analogRead(THERMISTORPIN);
    delay(50);  //50 5 bacaan
  }

  // average all the samples out
  average = 0;
  for (i = 0; i < NUMSAMPLES; i++) {
    average += samples[i];
  }
  average /= NUMSAMPLES;
  // convert the value to resistance
  average = 1023 / average - 1;
  average = SERIESRESISTOR / average;

  double steinhart1, steinhart2, steinhart3, steinhart4, steinhart5;

  Serial.println(F("start"));
  steinhart1 = log(average);                   // (ln R)
  steinhart2 = C * steinhart1 * steinhart1 * steinhart1;    // C * (ln R)***
  steinhart3 = (B * steinhart1) + steinhart2;                 // B*(ln R) + C * (ln R)***
  steinhart4 = 1.0 / (A + steinhart3); // 1/(A + B*(ln R) + C * (ln R)***)
  steinhart5 = steinhart4 - 273.15;                         // convert to C

  Serial.print(F("Temperature "));
  display.print(F("Body Temp = "));
  Serial.print(steinhart5);
  display.print(steinhart5);
  Serial.println(F(" *C"));
  display.println(F(" *C"));
  display.display();
  dataFile.println(steinhart5);
  dataFile.close();

  // Reading the file
  // re-open the file for reading:
  if (!dataFile.open("test.txt", O_READ)) {
    sd.errorHalt("opening test.txt for read failed");
  }
  Serial.println("test.txt:");

  // read from the file until there's nothing else in it:
  int data;
  while ((data = dataFile.read()) >= 0) Serial.write(data);
  // close the file:
  dataFile.close();
  delay(5000);
  //display.clearDisplay();


}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TIME SETTING ~~~~~~~~~~~~~~~~~~~~~~~~~~
void MorningAlarm() {
  Serial.println(F("Alarm: - turn lights off"));
  buzz();
}

void ExplicitAlarm() {
  Serial.println(F("Alarm: - this triggers only at the given date and time"));
}

void Repeats() {
  Serial.println(F("15 second timer"));
}

void Repeats2() {
  Serial.println(F("2 second timer"));
}

void OnceOnly() {
  Serial.println(F("This timer only triggers once, stop the 2 second timer"));
  // use Alarm.free() to disable a timer and recycle its memory.
  Alarm.free(id);
  // optional, but safest to "forget" the ID after memory recycled
  id = dtINVALID_ALARM_ID;
  // you can also use Alarm.disable() to turn the timer off, but keep
  // it in memory, to turn back on later with Alarm.enable().
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(' ');
  Serial.print(day());
  Serial.print(' ');
  Serial.print(month());
  Serial.print(' ');
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(':');
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void buzz() {

  tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec
  tone(buzzer, 2000); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec
  tone(buzzer, 1000); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(500);        // ...for 1sec

}

What should I do now?

What should I do now?

Get an Arduino with more memory.

Or, get real about the amount of stuff you can expect to have the Arduino do.