Writing data to file on SD card
hi,
firstly, apologies clunky coding attempt attach post still high on learning curve. you'll recognise blink in sketch , see i've progressed that. here code:
code: [select]
/*
blink
turns on led on 1 second, off 1 second, repeatedly.
example code in 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 led connected on arduino boards.
// give name:
int led = 13;
rtc_millis rtc;
// set variables using sd utility library functions:
sd2card card;
sdvolume volume;
sdfile root;
file datafile;
// setup routine runs once when press reset:
void setup() {
// initialize digital pin output.
pinmode(led, output);
serial.begin(9600);
accelero.begin(13, 12, 11, 10, a0, a1, a2);
accelero.setarefvoltage(3.3); //sets aref voltage 3.3v
accelero.setsensitivity(high); //sets sensitivity +/-6g
accelero.calibrate();
while (!serial) {
; // wait serial port connect. needed leonardo only
}
rtc.begin(datetime(__date__, __time__));
serial.println("\ninitializing sd card...");
// on ethernet shield, cs pin 4. it's set output default.
// note if it's not used cs pin, hardware ss pin
// (10 on arduino boards, 53 on mega) must left output
// or sd library functions not work.
pinmode(10, output);
digitalwrite(10, high);
// we'll use initialization code utility libraries
// since we're testing if card working!
while (!card.init(spi_half_speed, 4)) {
serial.println("initialization failed. things check:");
serial.println("* card inserted?");
serial.println("* wiring correct?");
serial.println("* did change chipselect pin match shield or module?");
}
// print 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");
}
// try open 'volume'/'partition' - should fat16 or fat32
if (!volume.init(card)) {
serial.println("could not find fat16/fat32 partition.\nmake sure you've formatted card");
return;
}
//serial.print("time ch0,ch1,ch2");
//serial.println("\n");
}
// loop routine runs on , on 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 readable)
serial.print("\t"); // prints tab
string datastring = "";
datetime = 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 cant write data
while (1) ;
}
serial.println(datastring);
datafile.println(datastring);
//datafile.flush();
int = 0; //use counter
// print serial port too:
//serial.println(' ');
// line datafile.flush() 'save' file sd card after every
// line of data - use more power , slow down how data
// can read it's safer!
// if want speed system, call flush() less , it
// save file when called , every 512 bytes - every time sector on the
// sd card filled data. however, don't depend on automatic flush!
i++;
if (i>50){ //call every 50 times
datafile.flush();
= 0;
}
while (datafile.available()) {
serial.write(datafile.read());
}
digitalwrite(led, high); // turn led on (high voltage level)
delay(250); // wait second
digitalwrite(led, low); // turn led off making voltage low
delay(250); // wait second
}
my aim take data mma 7361 analogue accelerometer , ds1307 rtc , write x, y , z values date , time stamp file, datalog.txt, on sd card.
i haven't been able , can't work out why.
using serial monitor can see data presented in format want mega/mma7361 , rtc working correctly. see attachment.
grateful advice on how achieve data write card.
once have data being captured on card progress project closing file @ 23:59:59 , creating new file @ 00:00:05 next day file name of new date. possible?
finally want add ethernet capability allow files accessed remotely.
but @ current rate of progress think tat long way off
i haven't been able , can't work out why.don't try @ once!
what type of arduino board using?
what type of sd-module using?
do try use sd card slot on ethernet-shield? or maybe use same module guy in thread does?
did try run of sd library programming examples came sd library?
did work?
Arduino Forum > Using Arduino > Programming Questions > Writing data to file on SD card
arduino
Comments
Post a Comment