Introduction
Read moreThe Idea was to observe several temperature changes in my home central heating / solar water heating system and see the startup/shutdowns temprature values of the heating circuits.
Dummy code:Setup:
- clear logfile if button pressed within 2 sec. after startup
- write headers to CSV file
- optional: wait for button pressed to start logging
- store timestamp
- make 30 readings, drop first five, calculate average (to filter jitter)
- calculate celsius value
- write to array
- repeat this for each sensor
- make comma separated string out of array
- if countdown is elapsed, write result to logfile
- write each result to LCD display
- wait until interwal time is elapsed
- start over again
/*
6 Channel analog temperature logger by Dooni
*/
// include the library code:
#include <LiquidCrystal.h>
#include <SPI.h>
#include <SD.h>
// initialize the library with the numbers of the interface pins
// LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// pins 4, 11, 12 are used by SPI
LiquidCrystal lcd(9, 8, 5, 7, 3, 2);
String logFileName = "datalog.csv"; // set the File name
String cs = ";"; // ste the comma separator
const int startButton = 6; // set up a constant for the pushbutton pin
const int numberOfSensors = 6; // how many sensors did you hook up?
const int interval = 1000; // setup cycle Time to measure / update LCD (default: 1000)
const int logInterval = 5; // setup logging interval in number of "interval" (default: 60)
const int chipSelect = 4;
const int numberOfReadings = 30; // set number of readings for average calculation
const int numberOfRejects = 5; // the first readings are rejected
int logCountDown = logInterval; // reset the downcounter
int logCounter = 0; // count number of records
float celsiusValues[numberOfSensors]; // an array to store the celsius values
unsigned long previousTime = 0; // to store last timestamp
boolean fileDelete = false; // set true to delete logfile each startup
boolean autoStart = false; // set true to autostart logger on powerup
/*========================================= SETUP ===================================================*/
void setup() {
// set up the Start Button pin as an input
pinMode(startButton, INPUT);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// set up the number of columns and rows on the LCD
lcd.begin(16, 2);
// initialize SD Card!
Serial.println("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
lcd.clear(); lcd.setCursor(0, 0); lcd.print("SD card failed.");
lcd.setCursor(0, 1); lcd.print("insert and reset!");
//if failded, get trapped here
while (1) { }
}
Serial.println("card initialized.");
// Print a message to the LCD.
lcd.clear();
lcd.print("6ch temp. logger");
lcd.setCursor(0, 1); lcd.print("SD card ready...");
Serial.println("SD card ready...");
delay(2000);
// option to delete the log file
if (!fileDelete) {
lcd.clear();
lcd.print("press button to");
lcd.setCursor(0, 1);
lcd.print("delete log file");
}
//push the button within the time to delete the file
while (millis()<4000) {
if (digitalRead(startButton) == HIGH) {
fileDelete = true;
}
}
if (fileDelete) {
SD.remove(logFileName);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("file deleted!");
Serial.println("Logfile deleted!");
delay(2000);
//write table headers if file was deleted
fileWrite("T1"+cs+"T2"+cs+"T3"+cs+"T4"+cs+"T5"+cs+"T6");
} else {
//write zero values to see each restart of the logger
fileWrite("0"+cs+"0"+cs+"0"+cs+"0"+cs+"0"+cs+"0");
}
lcd.clear();
lcd.print("Logger ready!");
lcd.setCursor(0, 1);
if (autoStart) {
lcd.print("starting...");
Serial.println("starting...");
delay(4000);
} else {
lcd.print("press start...");
Serial.println("press start...");
//wait until somebody presses the button
while (digitalRead(startButton) == LOW) { }
}
lcd.clear();
}
/*========================================= BEGIN LOOP ===================================================*/
void loop() {
//store current Time
previousTime = millis();
// Read each sensor, calculate celsius and fill array
for (int i = 0; i < numberOfSensors; i++) {
int totalValue = 0;
for (int j = 0; j < numberOfReadings; j++) {
if (j >= numberOfRejects) {
totalValue += analogRead(i);
}
delay(1);
}
float voltage = totalValue * 5 / 1024.0 / (numberOfReadings-numberOfRejects);
float tempCels = (voltage - 0.5) * 100 ;
celsiusValues[i] = tempCels;
delay (20);
}
/*Serial.print(millis());
Serial.println("<- end of measuring");*/
//Assemble comma separated string and write to serial
String dataString = "";
for (int i = 0; i < numberOfSensors; i++) {
dataString += String(celsiusValues[i]);
if (i < (numberOfSensors - 1)) {
dataString += cs;
}
}
Serial.println(dataString);
// if countdown is elapsed, write log to file
if (logCountDown < 1) {
fileWrite(dataString);
logCountDown = logInterval; // reset the downcounter
logCounter += 1; // count the record
}
logCountDown -= 1; // increment one down
//write data to LCD
lcd.clear(); //clear the LCD
lcd.setCursor(0, 0); lcd.print(celsiusValues[0]);
lcd.setCursor(3, 0); lcd.print(celsiusValues[1]);
lcd.setCursor(6, 0); lcd.print(celsiusValues[2]);
lcd.setCursor(0, 1); lcd.print(celsiusValues[3]);
lcd.setCursor(3, 1); lcd.print(celsiusValues[4]);
lcd.setCursor(6, 1); lcd.print(celsiusValues[5]);
lcd.setCursor(9, 0); lcd.print("t-" + String(logCountDown));
//lcd.setCursor(9, 0); lcd.print(millis());
lcd.setCursor(9, 1); lcd.print("n:" + String(logCounter));
//wait until interval time reached
while (millis() - previousTime < interval) { }
/*Serial.print(millis());
Serial.println("<- loop time elapsed");*/
}
/*========================================= END LOOP ===================================================*/
void fileWrite(String data) {
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open(logFileName, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(data);
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening logfile");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("error opening");
lcd.setCursor(0, 1);
lcd.print("logfile");
while (1) {}
}
Serial.println(data + " <-- written to file");
}
Thanks to Tom Igoe.
Comments