Hello, I am trying to write a basic data logging program where if a switch is flipped, a timer starts (from 0 and going up in 1 sec increments) and then if another input goes high, that is recorded alongside the time it occurred at. I'm having 2 issues. One, I am new to Arduino and didn't even know there was a data logging shield and already purchased an SD shield so it doesn't have the RTC Real Time clock on it. Is there any way to make this SD shield work for data logging where it accurately keeps track of the timer without the RTC? And two, my current program doesn't update the pin values once the program starts (i.e. I open Serial monitor and a switch reads 0, then I flip the switch and it still reads 0, but if I close the Serial Monitor and reopen it then the switch reads 1). I can't figure out how to get it to continuously update...I think something from my loop should be in my setup or vice versa. The current code doesn't even have a timer in it because I tried a basic counter with a 1 sec delay but it doesn't work, so right now I have it saying that if I hit the START switch then it will start displaying 0's and then log when a TRIP occurs. Any help would be greatly appreciated, thank you!
#include <SPI.h>
#include <SD.h>
const int chipSelect = 8; //SD shield CS pin for Arduino Uno
const int STARTpin = 2;
const int TRIPpin = 4 ;
int START=digitalRead(STARTpin);
int TRIP=digitalRead(TRIPpin);
File dataFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT); //default pin 10 must be set to output
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1) ;
}
Serial.println("card initialized.");
// Open up the file we're going to log to!
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (! dataFile) {
Serial.println("error opening datalog.txt");
// Wait forever since we cant write data
while (1) ;
}
}
void loop()
{
//Serial.println(START);
//Serial.println(TRIP);
if(START==1){
// make a string for assembling the data to log:
String dataString = "0";
if(TRIP==1){
dataString += String(START);
}
dataFile.println(dataString);
// print to the serial port too:
Serial.println(dataString);
// If you want to speed up the system, remove the call to flush() and it
// will save the file only every 512 bytes - every time a sector on the
// SD card is filled with data.
dataFile.flush();
// Take 1 measurement every 500 milliseconds
delay(500);
}