Implementing Date and Time stamp in a SD loggfile.

Hello,

I've been working on a project with the arduino (School project). I've had an arduino for about 1week now so I'm still a newbie.

The objective is to write a message to a SD card each time a sensor activates a pump (including errors when it doesnt too). The tricky part is: It has to be with a date and time stamp.

I've found a video on YouTube that helped me out alot ( Arduino Tutorial Lesson 5.1 - SD Card Write - YouTube ) And I used his program (With credits of course) as the basic to write my own.


So what I'd like to do is: The program checks if there is a SD card plugged in, if not it will give an error and return (Also: Does this reset my program or just stop it and wait for a manual reset?) then it waits until the sensor (Simulated by a button in this program) activates a pump. If there is a signal it will display that the pump is activated and open an SD file.

If there is a signal AND if there is no error with opening the file it will write the date and time to the SD card and give a message (to the serial monitor) that it has been written to the SD card . Then it will close the file.

If there is a signal but there is also an error it will display the error for 5000ms and then return.
If there is no signal it will display 'No signal' until there is a signal (thats why I put it in loop, so it will Continuously scan for an input)

the serial printlines will be exchanged by commands for the LCD display (16x2)


Here is the code:

/*
SD write by Aaron Lee
This sketch writes text data to an SD card

Simulatie van sensor - Pin 2
Master out slave in - Pin 11
Master in slave out - pin 12
Clock - pin 13
Chip select - pin 10
*/


#include <SD.h> //Library for SD commands

const int buttonPin = 2; // Simulates a sensor
int buttonState = 0 // variable from this button

File myFile; // create data file

void setup() 
{ 
Serial.begin(9600); // start communication at 9600baud

pinMode(buttonPin, INPUT); // Configure pin as input
Serial.println("SD card loading.."); // Just a notification

if (!SD.begin(10)) // if-statement to test the SD initializing for error, 10 is chipselect pin
{
Serial.println("SD card error") // Error if SD doesnt load
return; //Stops program after error
}
Serial.println("SD configured") // gives a notification of it succeeded
}

void loop()
{
  buttonState = digitalRead(buttonPin)
  if (buttonState == HIGH) // If the sensor is activated
{
Serial.println("Pump is activated") //Give a notification that the sensor triggered the pump  
  myFile = SD.open("werkuren.txt", FILE_WRITE); // opens and write to the file
if (myFile) // If sensor is activated and if myFile opens
// Not sure If I can do an 'if' in an 'if' but arduino doesnt have a 'if.. and..' function
{
  myFile.println("Date and time"); // Writes date and time on the SD card (I'm still figuring this out
  //I have no idea of how I can get the date but the time I can get from a clock shield?)
  Serial.println("Written to SD") // gives a notification that there has been written on the SD
  myFile.close() // Closes file untill next time the sensor is active?
}
else
{
  Serial.println("Error with file") // Gives an error if the file can't be opened
delay(5000)
return;
}
}
else
{
 delay(1000);
 Serial.println("No signal") // If there is no signal, display it on the screen.
}
}

I'd appreciate any kind of help.

Hello and welcome :slight_smile:

You need a way to get the time and date.

For this you have some solutions, the simplest in my opinion is to use a RTC module, such as a DS3231 which is very accurate ( compared to other cheap RTCs ).

I use this one, you can find it on ebay for few €:

And, for doing an 'and' you have to use '&&' like so:

if ( condition1 && condition2 )

(and 'or' is '||' )

Hi! And thank you for the reply!

Thanks for the advise and for informing me about an 'Or' and 'and' port! I already know about a 'not' port (Its an exclamation mark, right? :slight_smile: )

EDIT:
I had an error but already resolved it!

ANOTHER EDIT:

Can a 'goto' command go up in its program?

like this:

...
{buttonState = digitalRead(buttonPin);

reset:

delay(2000);
Serial.println("TEST");
delay(3000);
Serial.println("Hier komt ie");
delay(2000);
if (buttonState == HIGH)
{ goto reset;}
else 
{
  Serial.println("Het is gebeurd?")
...

The serial print is in Dutch but that doesnt really matter ^^.

There are three problems with the loop code. First it uses a goto statement. You should avoid it like the plague. The second is that there's no way to exit that loop if buttonState is HIGH because you don't read the button state again on the next pass through the loop. The third is the use of so many delays. Do you really want to read the button state once every 7 seconds? Try once every second

Try this code:

do {
  buttonState = digitalRead(buttonPin);
  Serial.println("TEST");
  Serial.println("Hier komt ie");
  delay(1000);
} while (buttonState == HIGH);

Serial.println("Het is gebeurd?")

Pete

Defink:
Can a 'goto' command go up in its program?

Yes, it can. But just because you can do something, doesn't mean you should do it.

el_supremo:
it uses a goto statement. You should avoid it like the plague.

Absolutely right. There are lots of structured alternatives to goto. I used goto in BASIC, FORTRAN, and assembly language programming many years ago when there was no other alternative. But since I started using structured languages about 30 years ago, I haven't had a situation since that required a goto.

Read up on structured programming techniques, and forget that goto even exists. You'll write better code for it.

In the code within your first post in this thread, you have a question:

// Not sure If I can do an 'if' in an 'if' but arduino doesnt have a 'if.. and..' function

Yes, you can nest an if inside another if. And as guix pointed out, you can do AND, OR, and NOT (as you discovered.) Not just IFs can be nested, but so can any combination of loops, IFs, switch statements, etc. It's what gives structured programming so much power and eliminates the need for the dangerous and error prone goto.