Hello,
I am using an Arduino Nano and trying to make a data logger for a project in my shed. It currently records pressure and temperature ever 10 seconds. Similar to a barometer I guess. The code below currently logs the data however the problem is the next bit I'm trying to do. I would like to be able to compare the current result for both temp and pressure with a previous result 24 hours ago recorded on an SD card and turn an LED on if they are the same. The code will run for about 7 days at a time and i'll manually reset it.
I am happy to reorganize the way the information goes to the SD card if it makes it easier.
I currently have no idea how to do this and would really appropriate some help.
#include <Arduino.h>
#include <TM1637Display.h> // adds 8 led library
//code for ADS1115
#include <Wire.h> // needed for scl and sda
#include <Adafruit_ADS1015.h> //ADS library
Adafruit_ADS1115 ads1115; // construct an ads1115 at address 0x49
// Define temp sensor stuff
#include <Wire.h>
#include "TSYS01.h"
TSYS01 sensor;
//Include SD card reader set up
#include <SD.h>
#include <SPI.h>
File myFile;
//Add rtc
#include "RTClib.h"
RTC_PCF8523 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// Module connection pins (Digital Pins) for LED
#define CLK 3 // set pins for led
#define DIO 4 // set pins for led
TM1637Display display(CLK, DIO);
#define CLK1 5 // set pins for led
#define DIO1 6 // set pins for led
TM1637Display display1(CLK1, DIO1);
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 1000
const float OffSet = 0.5 ;
float V, P;
float L1; //Pressure sensor reading
float T1; //Temp sensor reading
int pinCS = 53; // Look this up for SD card - Pin 10 on Arduino Uno
int Sec;
//for loop rtc function
//String Sec = ""; // string to hold input
//int Sec1;
int Y; //for finding previous value
void setup()
{
Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
Serial.println("/** Water pressure sensor demo **/");
//ADS1115 code
ads1115.begin(); // Initialize ads1115 188uV/bit
// for temp sensor
Wire.begin();
sensor.init();
/* //Part of the rtc code
while (!Serial) {
delay(1); // for Leonardo/Micro/Zero
}
// Serial.begin(57600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.initialized()) {
Serial.println("RTC is NOT running!");
// 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(2014, 1, 21, 3, 0, 0));
}
*/
// SD Card Initialization
pinMode(pinCS, OUTPUT);
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
rtc.begin();
}
void loop()
{
// rtc startup
DateTime now = rtc.now();
Sec = ((now.second()));
//Sec1=(Sec.toInt());
if ((Sec)==00||Sec==10||Sec==20||Sec==30||Sec==40||Sec==50)
{
int k;
int16_t adc0, adc1, adc2, adc3; //create int for adc0 used later
uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
display.setBrightness(1); // set the level of brightness
display1.setBrightness(1);//sets brightness of small (second) LED display
adc0 = ads1115.readADC_SingleEnded(0); // read data from ads1115
//Temp Sensor info
sensor.read();
T1=sensor.temperature();
//Connect sensor to Analog 0
V = (adc0 * 0.1875)/1000; //adc0 * ((4.0 / 32768)); //Sensor output voltage 32767 in the max division for 15bits
P = (V - OffSet); //account for 0.5v sensor gives a 0psi
L1 = (P*374); // pressure read by sensor 150psi/4v=37.4
// Serial.print(" Pressure:");
// Serial.print(L1);
// Serial.println(" psi");
//Serial.println();
// Show decimal numbers with/without leading zeros
bool lz = false; //true all digits on, false only digits used on
//display.showNumberDec((L1),lz);
display.showNumberDecEx((L1*10), (0x80 >> 0), lz); // this statement adds a decimal point (0x80>>1) sets the decimal point location, change "1" to move it
display1.showNumberDecEx((T1*10), (0x80 >> 2), lz); // Temp sensor info on second LED
// This section is for storing values to SD card
myFile = SD.open("Nautilus.txt", FILE_WRITE);
if (myFile) {
myFile.print(now.day(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.year(), DEC);
myFile.print(' ');
myFile.print(now.hour(), DEC);
// myFile.print(':');
myFile.print(now.minute(), DEC);
// myFile.print(':');
myFile.print(now.second(), DEC);
myFile.print(" ");
myFile.print(float(T1));
myFile.print(' ');
myFile.println(float(L1));
myFile.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
//Part of rtc code
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print("Temperature: ");
Serial.print(T1);
Serial.println(" deg C");
Serial.print("Pressure: ");
Serial.println(float(L1*10));
Serial.println("---");
DateTime past (now-TimeSpan(0,0,30,6));
//X = myFile.seek(1);
myFile.readStringUntil(195830);
myFile.find("123");
Serial.println( );
Serial.println("---");
delay(TEST_DELAY);
}
}