Hello....
I have an arduino nano and I'm trying to build a project with two DHT22 sensors , one RTC module (DS3231) , a microSD module and an 0,96'' OLED 128x32 monitor.
I wrote a program to read the temps and humidities from the two sensors and store them in a microsd card.The terminal output shows the date,time,temp1,hum1,temp2,hum2 and stores the values just fine.
The problem is that when I want to see this values in the OLED display , the program reaches 97% of the total flash memory of arduino nano and I can't upload it.
If I skip the last lines that stores the data in microsd card , the program is about 87% and I can upload it just fine.
Is there any way to make the program more compact to fit to memory?
I asume that the library of the OLED display reserves some amount of memory for buffer purposes so I can't fit my program in the flash memory.
If there is no way to make the program more compact to fit , should I go to a ESP8266 chip?
Will the code run without changes or I have to learn to program in this new chip?
The code is below
#include "RTClib.h"
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
File myFile;
RTC_DS3231 rtc;
char cl[32];
#include "DHT.h"
#define DHTPIN1 4 // Digital pin connected to the DHT sensor
#define DHTPIN2 5 // Digital pin connected to the DHT sensor
#define DHTTYPE1 DHT22 // DHT 22 (AM2302), AM2321
#define DHTTYPE2 DHT22 // DHT 22 (AM2302), AM2321
DHT dht1(DHTPIN1, DHTTYPE1);
DHT dht2(DHTPIN2, DHTTYPE2);
void setup(){
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
dht1.begin();
dht2.begin();
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop() {
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2019, 9, 11, 12, 40, 0));
}
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h1 = dht1.readHumidity();
float h2 = dht2.readHumidity();
// Read temperature as Celsius (the default)
float t1 = dht1.readTemperature();
float t2 = dht2.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f1 = dht1.readTemperature(true);
float f2 = dht2.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h1) || isnan(t1) || isnan(f1)) {
Serial.println(F("Failed to read from DHT1 sensor!"));
return;
}
if (isnan(h2) || isnan(t2) || isnan(f2)) {
Serial.println(F("Failed to read from DHT2 sensor!"));
return;
}
DateTime now = rtc.now();
sprintf(cl, "%02d/%02d/%02d %02d:%02d:%02d", now.day(), now.month(), now.year(),now.hour(), now.minute(), now.second());
//Serial.print(F("Date/Time: "));
//Serial.print(cl);
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
display.println(cl);
float hif1 = dht1.computeHeatIndex(f1, h1); // Compute heat index in Fahrenheit (the default)
float hif2 = dht2.computeHeatIndex(f2, h2); // Compute heat index in Fahrenheit (the default)
display.print(F("H1: "));
display.print(h1);
display.print(F("% "));
display.print(F("T1: "));
display.print(t1);
display.println(F("C"));
display.print(F("H2: "));
display.print(h2);
display.print(F("% "));
display.print(F("T2: "));
display.print(t2);
//display.print(F("C"));
//display.print(" Temp2(RTC) : ");
//display.print(rtc.getTemperature());
//display.print("C");
display.display();
Serial.print(F(" Humidity: "));
//Serial.print(h);
Serial.print(F("% Temp1: "));
//Serial.print(t);
Serial.print(F("C "));
Serial.print(" Temp2(RTC) : ");
Serial.print(rtc.getTemperature());
Serial.print("C");
if (now.second() == 00 ) {
myFile = SD.open("test.txt", FILE_WRITE);
myFile.print(cl);
myFile.print(" , ");
myFile.print(h1);
myFile.print(" , ");
myFile.print(t1);
myFile.print(" , ");
myFile.print(h2);
myFile.print(" , ");
myFile.print(t2);
myFile.println(rtc.getTemperature());
myFile.close();
Serial.print(" MicroSD REC ");
}
Serial.println();
delay(1000);
}