Programming with SD Library
hi,
i trying implement arduino nano (with atmega 328) in 1 of projects. this, have attached fsr force sensing resistor arduino. have program outputs value serial monitor corresponding force push with.
now, trying data write micro sd card..
here code:
after running cardinfo demo, verified wiring correct. program/set-up works part, i'm having 2 minor issues..
1) randomly while running, "initializing sd card..." message show in serial monitor , counter reset... indicates me setup() being run again, don't understand why.. ideas?
example:
analog reading = 1010
15,1010
analog readiinitializing sd card...card initialized.
analog reading = 1013
1,1013
analog reading = 0
2,0
2) randomly while running, serial monitor hang , stop outputting screen. ideas why may caused?
also, have counter number of times setup() run, can store separate data files each time happens. ideas on how implement that?
thanks!!!
i trying implement arduino nano (with atmega 328) in 1 of projects. this, have attached fsr force sensing resistor arduino. have program outputs value serial monitor corresponding force push with.
now, trying data write micro sd card..
here code:
code: [select]
/*
sd card datalogger
example shows how log data 3 analog sensors
sd card using sd library.
circuit:
* analog sensors on analog ins 0, 1, , 2
* sd card attached spi bus follows:
** mosi - pin 11
** miso - pin 12
** clk - pin 13
** cs - pin 4
created 24 nov 2010
updated 2 dec 2010
tom igoe
example code in public domain.
*/
#include <sd.h>
// on ethernet shield, cs pin 4. note if it's not
// used cs pin, hardware cs pin (10 on arduino boards,
// 53 on mega) must left output or sd library
// functions not work.
const int chipselect = 10;
int counter;
void setup()
{
serial.begin(9600);
serial.print("initializing sd card...");
// make sure default chip select pin set to
// output, if don't use it:
pinmode(10, output);
// see if card present , can initialized:
if (!sd.begin(chipselect)) {
serial.println("card failed, or not present");
// don't more:
return;
}
serial.println("card initialized.");
counter = 0;
}
void loop()
{
// make string assembling data log:
string datastring = "";
counter = counter + 1;
//read sensor value , output serial monitor
int analogpin = 0;
int sensor = analogread(analogpin);
serial.print("analog reading = ");
serial.println(sensor);
//store sensor value in datastr
datastring = (string)counter + "," + (string)sensor + "\n";
// read 3 sensors , append string:
//for (int analogpin = 0; analogpin < 3; analogpin++) {
//int sensor = analogread(analogpin);
//datastring += string(sensor);
//if (analogpin < 2) {
//datastring += ",";
//}
//}
// open file. note 1 file can open @ time,
// have close 1 before opening another.
file datafile = sd.open("datalog2.txt", file_write);
// if file available, write it:
if (datafile) {
datafile.println(datastring);
datafile.close();
// print serial port too:
serial.println(datastring);
}
// if file isn't open, pop error:
else {
serial.println("error opening datalog2.txt");
}
delay(1000);
}after running cardinfo demo, verified wiring correct. program/set-up works part, i'm having 2 minor issues..
1) randomly while running, "initializing sd card..." message show in serial monitor , counter reset... indicates me setup() being run again, don't understand why.. ideas?
example:
analog reading = 1010
15,1010
analog readiinitializing sd card...card initialized.
analog reading = 1013
1,1013
analog reading = 0
2,0
2) randomly while running, serial monitor hang , stop outputting screen. ideas why may caused?
also, have counter number of times setup() run, can store separate data files each time happens. ideas on how implement that?
thanks!!!
code: [select]
datastring = (string)counter + "," + (string)sensor + "\n";the sd library uses lot of sram (of necessity). string class uses lot of sram, too, , not necessary. learn how use char arrays , string functions, instead. sprintf() might useful, too.
casting int string not useful thing do.
quote
this indicates me setup() being run again, don't understand why.. ideas?
when run out of memory, bad things happen.
Arduino Forum > Using Arduino > Programming Questions > Programming with SD Library
arduino
Comments
Post a Comment