I am using the Arduino Uno R3 with the SD data logging shield. I can send the data exactly as I want using the serial print option, however when writing to the sd card the date and time data is incorrect and It puts a carriage return between each number written.
To help understand what I am attempting to do I am posting my code that gives me the correct info when sent to the serial monitor. This is what I eventually would like the data to read when writing to the SD card.
Here is my code:
/*
Writes date and time to the serial monitor when pin 2 is set high (motor on) Writes date and time when Pin 2 goes low (motor off). Also I turn the light emitting diode(LED) connected to digital
pin 13, when pressing a push button attached to pin 2.
The circuit:
- LED attached from pin 13 to ground
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground
created 2014 with help of examples
*/
// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include “RTClib.h”
RTC_DS1307 rtc;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int DoOnce = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (DoOnce == 0){
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print(’/’);
Serial.print(now.month(), DEC);
Serial.print(’/’);
Serial.print(now.day(), DEC);
Serial.print(’ ‘);
Serial.print(now.hour(), DEC);
Serial.print(’:’);
Serial.print(now.minute(), DEC);
Serial.print(’:’);
Serial.print(now.second(), DEC);
Serial.print(’ ‘);
Serial.print(’ ');
Serial.print(“Motor On”);
Serial.println();
DoOnce = 1;
}}
else {
// turn LED off:
if (DoOnce == 1) {
if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin, LOW);
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print(’/’);
Serial.print(now.month(), DEC);
Serial.print(’/’);
Serial.print(now.day(), DEC);
Serial.print(’ ‘);
Serial.print(now.hour(), DEC);
Serial.print(’:’);
Serial.print(now.minute(), DEC);
Serial.print(’:’);
Serial.print(now.second(), DEC);
Serial.print(’ ‘);
Serial.print(’ ');
Serial.print(“Motor Off”);
Serial.println();
DoOnce = 0;
digitalWrite(ledPin, LOW);
}
}}}
Ouput when toggling pin 2 looks like this:
2014/6/2 9:37:18 Motor On
2014/6/2 9:37:23 Motor Off
2014/6/2 9:37:27 Motor On
2014/6/2 9:37:30 Motor Off
2014/6/2 9:37:31 Motor On
2014/6/2 9:37:33 Motor Off
2014/6/2 9:37:34 Motor On
2014/6/2 9:37:41 Motor Off