Hi Everyone,
I hope someone can help me out, I am pretty new to the Arduino and am making a two input pulse datalogger for an experiment. So far everything has been going well except that I have run out of program storage space. I have been trying my best to reduce the code required but I am still just over at 31,946 bytes for the Arduino Nano.
I am using the Freetronics RTC and Temperature/Humidity modules (and accelerometer but this isn't as critical so has already been removed from the code) as well as the Arduino TFT screen / SD card module. The inputs are going into the interrupt pins D2 & D3 and everything works well on the LCD display.
I need to have both the LCD display working (so someone can check on the experiment from time to time) as well as logging to the SD card for analysis at the end of the experiment.
Can someone help me reduce my code size please?
Apologies for the poor coding style but I have spent most of today hacking away at it in frustration trying to reduce the sketch size.
/*
Two channel interrupt driven datalogger
*/
#include <Time.h> // Time library for timekeeping on the Arduino
#include <TFT.h> // Arduino TFT LCD library
#include <SPI.h> // SPI driver library
#include <SD.h> // SD card reading and writing library
#include <SoftI2C.h> // I2C interface library
#include <DS3232RTC.h> // Library for the RTC
#include "DHT.h" // Library for the Temperature and Humidity sensor
//#include <stdlib.h>
// Pin definition for the temperature and humidity sensor
const int DHTPIN = 8;
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Pin definition for the TFT screen
const int TFTcs = 4; // Chip select pin for the screen
const int dc = 6; // Pin for Data/Command selection for screen
const int rst = 7; // Reset pin for the screen
const int BL = 5; // Backlight dimming pin LOW = dimmed, HIGH = bright
// Pin definition for the Accelerometer
//const int XaxisAcc = A0; // X axis for the accelerometer
//const int YaxisAcc = A1; // Y axis for the accelerometer
//const int ZaxisAcc = A2; // Z axis for the accelerometer
// Pin definition for the SD card Serial Select
const int SDcs = 9; //
volatile int Input1 = LOW; // Set input for interrupt on D2
volatile int Input2 = LOW; // Set input for interrupt on D3
SoftI2C i2c(A4, A5); // Use pins A4 and A5 as software I2C for RTC
DS3232RTC rtc(i2c); // Attach the RTC to the I2C pins
// Create an instance of the TFTscreen library
TFT TFTscreen = TFT(TFTcs, dc, rst); // Pass the chip select pin, data/command pin and reset pin
// char array to print variables to the screen
char Time[9];
char Date[9];
char currentTemp[5];
char currentHumid[5];
char currentCount1[11];
char currentCount2[11];
String sHour;
String sMinute;
String sSecond;
String sDay;
String sMonth;
String currentTimeString;
String currentDateString;
char Filename[] = "Temp.txt";
String Datastring;
unsigned long Count1 = 0;
unsigned long Count2 = 0;
word currentMillis = 0;
word previousMillis = 0;
word previousTimeMillis = 0;
word previousDHTMillis = 0;
word previousDateMillis = 0;
const int Timeinterval = 1000;
const int DHTinterval = 10000;
const int Dateinterval = 60000;
const int interval = 1000;
void setup() {
//Serial.begin(115200);
dht.begin(); // Initialise the temperature and humidity module
pinMode(BL, OUTPUT); // set the backlight pin as an output
pinMode(10, OUTPUT); // Pin 10 needs to be kept as an output (even if not used) or SD library won't work
digitalWrite(BL, HIGH); // Set default brightness of backlight
attachInterrupt(0, Increment1, RISING);
attachInterrupt(1, Increment2, RISING);
TFTscreen.begin(); // Initialise the TFT LCD
TFTscreen.background(0, 0, 0); // clear the screen with a black background
TFTscreen.stroke(255, 255, 255);// Write static text to the screen with a white font
TFTscreen.setTextSize(2); // set the font size as 2 from here on
if (!SD.begin(SDcs)) {
//TFTscreen.text("Card failure", 0, 0);// Display SD card initialisation failure on TFT
//TFTscreen.text("check card", 0, 20);
//TFTscreen.text("is present", 0, 40);
//TFTscreen.text("and correctly", 0, 60);
//TFTscreen.text("formatted as", 0, 80);
//TFTscreen.text("FAT16 / FAT32", 0, 100);
//delay(86400000);
return; //nothing else to do
}else{
TFTscreen.text("Time:", 0, 0); // write the text to the top left corner
TFTscreen.text("Date:", 0, 20);
TFTscreen.text("Temp:", 0, 40);
TFTscreen.text("Humid:", 0, 60);
TFTscreen.text("Count1:", 0, 80);
TFTscreen.text("Count2:", 0, 100);
}
WriteDHTtoTFT();
WriteDatetoTFT();
}
void loop() {
currentMillis = millis();
if(currentMillis - previousDHTMillis >= DHTinterval){
previousDHTMillis = currentMillis;
WriteDHTtoTFT();
}
if(currentMillis - previousTimeMillis >= Timeinterval){
previousTimeMillis = currentMillis;
WriteTimetoTFT();
}
if(currentMillis - previousDateMillis >= Dateinterval){
previousDateMillis = currentMillis;
WriteDatetoTFT();
}
if(currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
WriteCountstoTFT();
}
}
void WriteCountstoTFT(){
TFTscreen.fill(0, 0, 0); // Set the shape fill to black
TFTscreen.stroke(0, 0, 0); // Set the outline colour for the shape to black
TFTscreen.rect(80, 80, 82, 14);
TFTscreen.stroke(255, 255, 255); // set the font color to white
dtostrf(Count1, 1, 0, currentCount1);
TFTscreen.text(currentCount1, 80, 80);
TFTscreen.fill(0, 0, 0); // Set the shape fill to black
TFTscreen.stroke(0, 0, 0); // Set the outline colour for the shape to black
TFTscreen.rect(80, 100, 82, 14);
TFTscreen.stroke(255, 255, 255); // set the font color to white
dtostrf(Count2, 1, 0, currentCount2);
TFTscreen.text(currentCount2, 80, 100);
}
void WriteDHTtoTFT(){
dtostrf(dht.readTemperature(),0,1,currentTemp);
TFTscreen.fill(0, 0, 0); // Set the shape fill to black
TFTscreen.stroke(0, 0, 0); // Set the outline colour for the shape to black
TFTscreen.rect(60, 40, 70, 14);
TFTscreen.stroke(255, 255, 255); // set the font color to white
strcat(currentTemp,"c");
strcat(currentTemp,'\0');
TFTscreen.text(currentTemp, 60, 40);
dtostrf(dht.readHumidity(),0,1,currentHumid);
TFTscreen.fill(0, 0, 0); // Set the shape fill to black
TFTscreen.stroke(0, 0, 0); // Set the outline colour for the shape to black
TFTscreen.rect(70, 60, 70, 14);
TFTscreen.stroke(255, 255, 255); // set the font color to white
strcat(currentHumid,"%");
strcat(currentHumid,'\0');
TFTscreen.text(currentHumid, 70, 60);
}
void WriteTimetoTFT(){
TFTscreen.fill(0, 0, 0); // Set the shape fill to black
TFTscreen.stroke(0, 0, 0); // Set the outline colour for the shape to black
TFTscreen.rect(60, 0, 94, 14);
TFTscreen.stroke(255, 255, 255); // set the font color to white
currentTimeString = printTime();
currentTimeString.toCharArray(Time, 9);
TFTscreen.text(Time, 60, 0);
}
void WriteDatetoTFT(){
TFTscreen.fill(0, 0, 0); // Set the shape fill to black
TFTscreen.stroke(0, 0, 0); // Set the outline colour for the shape to black
TFTscreen.rect(60, 20, 94, 14);
TFTscreen.stroke(255, 255, 255); // set the font color to white
currentDateString = printDate();
currentDateString.toCharArray(Date, 9);
TFTscreen.text(Date, 60, 20);
}
String printTime()
{
RTCTime time;
rtc.readTime(&time);
byte hour = time.hour;
byte minute = time.minute;
byte second = time.second;
//if(hour<10){
//sHour = (String)('0')+(String)hour;
//}else{
sHour = (String)hour;
//}
//if(minute<10){
//sMinute = (String)('0')+(String)minute;
//}else{
sMinute = (String)minute;
//}
//if(second<10){
//sSecond = (String)('0')+(String)second;
//}else{
sSecond = (String)second;
//}
String CurrentTime = sHour + ':' + sMinute + ':' + sSecond;
return CurrentTime;
}
String printDate()
{
RTCDate date;
rtc.readDate(&date);
byte day = date.day;
byte month = date.month;
int year = date.year;
//if(day<10){
//sDay = (String)('0')+(String)day;
//}else{
sDay = (String)day;
//}
//if(month<10){
//sMonth = (String)('0')+(String)month;
//}else{
sMonth = (String)month;
//}
String CurrentDate = sDay + '/' + sMonth + '/' + (String)(year-2000);
return CurrentDate;
}
void Increment1(){
Count1++;
Datastring="Counter1 "+printDate()+" "+printTime()+" "+currentHumid+" "+currentTemp;
File dataFile = SD.open(Filename, FILE_WRITE);
if (dataFile) {
dataFile.println(Datastring);
dataFile.close();
}
}
void Increment2(){
Count2++;
Datastring="Counter2 "+printDate()+" "+printTime()+" "+currentHumid+" "+currentTemp;
File dataFile = SD.open(Filename, FILE_WRITE);
if (dataFile) {
dataFile.println(Datastring);
dataFile.close();
}
}
/*
void ResetCounters(){
neutronCount1=0;
neutronCount2=0;
}
*/

