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.