I am printing a temperature difference to the lcd.
When the diff becomes larger than 10 and then goes below 10 there is a lingering value in the one's digit. The ten's digit contains the new temp(single digit).
I know the bulk of the code is not very elegant or efficient. That is for another day.
Can you help me with the subtraction code.
#include <RTClib.h> //include necessary libraries
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS 2 //data line is plugged into pin 2
OneWire oneWire(ONE_WIRE_BUS); //set up a oneWire instance to communicate with oneWie device(s)
DallasTemperature sensors(&oneWire); //pass oneWire reference to DallasTemperature library
RTC_DS3231 rtc; //initialize the DS3231
LiquidCrystal_I2C lcd(0x27,20,4); //set the address and size of the LCD display
File myFile; //name an instance of the SD module myFile
const unsigned long eventTime_1_Serial = 5000; //define numerical format of the three time counters
const unsigned long eventTime_2_SD = 60000; //and set the lendths of the three time delays
const unsigned long eventTime_3_LCD = 1000;
unsigned long previousTime_1 = 0; //set the initial count of the timer to zero
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 = 0;
int difference = 0;
void setup() {
Serial.begin(9600); //open the serial port
delay(3000); //wait 6 secs for the serial port to open
sensors.begin(); //open the sensors
sensors.setResolution(12); //set the sensor resolution to 12
lcd.init(); //open the lcd
lcd.backlight();
rtc.begin(); //open the RTC
while (!Serial) { //check that sd card is present
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) { //check that sd card hasn't lost power or battery is dead
Serial.println("RTC lost power, lets set the time!");
}
SD.remove("today.txt"); //clear the sc card of previous recordings
myFile = SD.open("today.txt", FILE_WRITE); //create and open an sd file named today.txt
if (myFile) { //getting ready to print column headers in sd file
Serial.print("Writing to today.txt...");
myFile.print("Year/Month/Day");
myFile.print(" ");
myFile.print("Hour");
myFile.print(':');
myFile.print("Minute");
myFile.print(",");
myFile.print("Temp In");
myFile.print(",");
myFile.print("Temp Out");
myFile.println();
myFile.close();
}
}
void loop() {
unsigned long currentTime = millis(); //defining numeric format and setting current time to millis time
DateTime now = rtc.now(); //get real current time and date from RTC
if( currentTime - previousTime_1 >= eventTime_1_Serial ){ //tests if millis time is ready for first event, serial print
Serial.print(now.year(), DEC); //serial print date, time, and two temps
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(",");
Serial.print(now.hour(),DEC);
Serial.print(':');
if(now.minute() < 10){ //puts a leading zero to single digit minutes
Serial.print("0");
}
Serial.print(now.minute(), DEC);
Serial.print(':');
if(now.second() < 10){ //puts a leading zero to single digit secs
Serial.print("0");
}
Serial.print(now.second(), DEC);
Serial.print("...");
Serial.println("DONE");
Serial.print(" Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
Serial.print("Temperatures are: ");
Serial.print(sensors.getTempFByIndex(0));
Serial.print("...");
Serial.println(sensors.getTempFByIndex(1));
Serial.println();
previousTime_1 = currentTime; //first timed event completed and counter1 reset
}
if( currentTime - previousTime_2 >= eventTime_2_SD ){ //tests if millis time is ready for second event, sd write
myFile = SD.open("today.txt", FILE_WRITE); //opens the file and gets ready to write
if (myFile) { //if file is open, write year, month, day, hour, min, and two temps
Serial.print("Writing to today.txt...");
myFile.print(now.year(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
//myFile.print(",");
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(",");
myFile.print(sensors.getTempFByIndex(0));
myFile.print(",");
myFile.println(sensors.getTempFByIndex(1));
myFile.close();
previousTime_2 = currentTime; //second timed event completed and counter2 reset
}
}
if( currentTime - previousTime_3 >= eventTime_3_LCD ){ //tests if millis time is ready for third event, lcd display
lcd.setCursor (0, 0 ); //display two temps, month, day, year, hour, min, sec
lcd.print(" TIn:");
lcd.print(sensors.getTempFByIndex(0),0);
lcd.print("F ");
lcd.print(" TOut:");
lcd.print(sensors.getTempFByIndex(1),0);
lcd.print("F");
lcd.setCursor(4,1);
lcd.print("Temp Diff:");
difference = (sensors.getTempFByIndex(0))-(sensors.getTempFByIndex(1));
lcd.print(difference);
lcd.setCursor (5, 2 );
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.print(" ");
lcd.setCursor (6,3);
lcd.print(now.hour(),DEC);
lcd.print(':');
if(now.minute() < 10){ //puts a leading zero to single digit minutes
lcd.print("0");
}
lcd.print(now.minute(), DEC);
lcd.print(':');
if(now.second() < 10){ //puts a leading zero to single digit secs
lcd.print("0");
}
lcd.print(now.second(), DEC);
previousTime_3 = currentTime; //third timed event completed and counter3 reset
}
}