Hi,
Firstly, apologies for the very clunky coding attempt that I will attach to this post but am still very high on the learning curve. You'll probably recognise Blink in the sketch and see that I've progressed from that. Here is the code:
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>
#include <AcceleroMMA7361.h>
AcceleroMMA7361 accelero;
int x;
int y;
int z;
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
RTC_Millis rtc;
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
File dataFile;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600);
accelero.begin(13, 12, 11, 10, A0, A1, A2);
accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V
accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G
accelero.calibrate();
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
rtc.begin(DateTime(__DATE__, __TIME__));
Serial.println("\nInitializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
while (!card.init(SPI_HALF_SPEED, 4)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card is inserted?");
Serial.println("* Is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
}
// print the type of card
Serial.print("\nCard type: ");
switch(card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
//Serial.print("Time Ch0,Ch1,Ch2");
//Serial.println("\n");
}
// the loop routine runs over and over again forever:
void loop() {
x = accelero.getXRaw();
y = accelero.getYRaw();
z = accelero.getZRaw();
Serial.print("\nx: ");
Serial.print(x);
Serial.print("\ty: ");
Serial.print(y);
Serial.print("\tz: ");
Serial.print(z);
delay(250); //(make it readable)
Serial.print("\t"); // prints a tab
String dataString = "";
DateTime now = rtc.now();
dataString += now.day();
dataString += "/";
dataString += now.month();
dataString += "/";
dataString += now.year();
dataString += " ";
dataString += now.hour();
dataString += ":";
dataString += now.minute();
dataString += ":";
dataString += now.second();
//dataString += " ";
//dataString += " ";
//dataString += String(analogRead(0));
//dataString += ",";
//dataString += String(analogRead(1));
//dataString += ",";
//dataString += String(analogRead(2));
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
Serial.println("error opening datalog.txt");
// Wait forever since we cant write data
while (1) ;
}
Serial.println(dataString);
dataFile.println(dataString);
//dataFile.flush();
int i = 0; //Use this for the counter
// print to the serial port too:
//Serial.println(' ');
// The line dataFile.flush() will 'save' the file to the SD card after every
// line of data - this will use more power and slow down how much data
// you can read but it's safer!
// If you want to speed up the system, call flush() less often and it
// will save the file only when called and every 512 bytes - every time a sector on the
// SD card is filled with data. However, don't depend on the automatic flush!
i++;
if (i>50){ //Call every 50 times
dataFile.flush();
i = 0;
}
while (dataFile.available()) {
Serial.write(dataFile.read());
}
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(250); // wait for a second
}
My aim is to take the data from an MMA 7361 analogue accelerometer and a DS1307 RTC and write the x, y and z values with a date and time stamp to a file, datalog.txt, on the SD card.
I haven't currently been able to do that and I can't work out why.
Using the serial monitor I can see the data presented in the format I want so the Mega/MMA7361 and RTC are all working correctly. See attachment.
Grateful for advice on how to achieve the data write to the card.
Once I have the data being captured on the card I would like to progress this project by closing the file at 23:59:59 and creating a new file at 00:00:05 the next day with a file name of the new date. Is that possible?
Finally I want to add an Ethernet capability to allow the files to be accessed remotely.
But at current rate of progress I think tat is a long way off >:(