SD Card error
hello,
i'm developing simple program logs data sd card.
however sd card never initializes.
i'm using:
arduino uno
sd card shield (http://imall.iteadstudio.com/im120417007.html) (switch @ 3.3v)
lcd shield (http://www.dfrobot.com/index.php?route=product/product&product_id=51#.u3cdf5uu_iu)
here's code:
thanks in advance
i'm developing simple program logs data sd card.
however sd card never initializes.
i'm using:
arduino uno
sd card shield (http://imall.iteadstudio.com/im120417007.html) (switch @ 3.3v)
lcd shield (http://www.dfrobot.com/index.php?route=product/product&product_id=51#.u3cdf5uu_iu)
here's code:
code: [select]
#include <sd.h>
#include <rtclib.h>
#include <liquidcrystal.h>
#include <wire.h>
#include <spi.h>
liquidcrystal lcd(8,13,9,4,5,6,7);
rtc_ds1307 rtc;
file myfile;
const int hall_sensor = 2;
const int led = 13;
const int piez = 2;
const int override = 3;
const int chipselect = 10;
void setup(){
rtc.begin();
serial.begin(9600);
wire.begin();
lcd.begin(16, 2);
sd.begin();
// rtc.adjust(datetime(__date__, __time__));
pinmode(hall_sensor, input);
pinmode(led, output);
pinmode(piez, output);
pinmode(override, input);
lcd.setcursor(0,0);
lcd.print(" a iniciar ");
lcd.setcursor(0,1);
lcd.print(" espere ");
delay(5000); //tempo de calibraÇÃo
lcd.clear();
lcd.setcursor(0,0);
lcd.print(" sistema activo ");
delay(2000);
serial.println("a iniciar o cartÃo.");
pinmode(10, output);
if (!sd.begin(chipselect)) {
serial.println("a inicializaÇÃo sd falhou");
return;
}0
serial.println("inicializaÇÃo bem sucedida.");
// abrir o ficheiro
myfile = sd.open("logsense.txt", file_write);
// se o ficheiro abrir:
if (myfile) {
serial.print("a escrever no logsense.txt...");
myfile.println("teste 1, 2, 3.");
// fechar o ficheiro:
myfile.close();
serial.println("done.");
}
else {
// se o ficheiro nÃo abrir
serial.println("erro ao abrir logsense.txt");
}
// re-abrir o ficheiro para leitura
myfile = sd.open("logsense.txt");
if (myfile) {
serial.println("logsense.txt:");
// ler o ficheiro atÉ ao fim e posicionar o cursor na ultima linha
while (myfile.available()) {
serial.write(myfile.read());
}
// fechar o ficheiro
myfile.close();
}
else {
serial.println("erro ao abrir o ficheiro");
}
}
void indicator_hall_sensor(){
digitalwrite(led, high);
tone(piez, 300);
delay(1000);
digitalwrite(led, low);
delay(1000);
}
void key(){
datetime = rtc.now();
//delay(2500);
int override = digitalread(3);
//delay(2500);
if (override == high){
lcd.clear();
lcd.setcursor(0,0);
lcd.print(" chave aceite ");
delay(2500);
//inicia o output para serial 9600
serial.println("registado acesso autorizado");
serial.print(now.hour(), dec);
serial.print(":");
serial.print(now.minute(), dec);
serial.print(":");
serial.println(now.second(), dec);
serial.print(now.day(), dec);
serial.print("/");
serial.print(now.month(), dec);
serial.print("/");
serial.println(now.year(), dec);
//inicia o output para o cartÃo sd
myfile.println("registado acesso autorizado");
myfile.print(now.hour(), dec);
myfile.print(":");
myfile.print(now.minute(), dec);
myfile.print(":");
myfile.println(now.second(), dec);
myfile.print(now.day(), dec);
myfile.print("/");
myfile.print(now.month(), dec);
myfile.print("/");
myfile.println(now.year(), dec);
}
else{
}
}
void trip_hall_sensor(){
datetime = rtc.now();
delay(2500);
int override = digitalread(3);
delay(2500);
if (override == high){
key();
}
else if (override == low){
lcd.clear();
lcd.setcursor(0,0);
lcd.print("iniciar registo");
//inicia o output para serial 9600
serial.println("registado acesso não autorizado");
serial.print(now.hour(), dec);
serial.print(":");
serial.print(now.minute(), dec);
serial.print(":");
serial.println(now.second(), dec);
serial.print(now.day(), dec);
serial.print("/");
serial.print(now.month(), dec);
serial.print("/");
serial.println(now.year(), dec);
//inicia o output para o cartÃo sd
myfile.println("registado acesso não autorizado");
myfile.print(now.hour(), dec);
myfile.print(":");
myfile.print(now.minute(), dec);
myfile.print(":");
myfile.println(now.second(), dec);
myfile.print(now.day(), dec);
myfile.print("/");
myfile.print(now.month(), dec);
myfile.print("/");
myfile.println(now.year(), dec);
indicator_hall_sensor();
}
}
void loop(){
int override = digitalread(3);
int hall_sensor = digitalread(2);
if ( hall_sensor == high){
//delay(10000);
trip_hall_sensor();
}
else
digitalwrite(led, low);
notone(piez);
datetime = rtc.now();
/*
//inicia o output para serial 9600
serial.println("a porta está fechada");
serial.print(now.hour(), dec);
serial.print(":");
serial.print(now.minute(), dec);
serial.print(":");
serial.println(now.second(), dec);
serial.print(now.day(), dec);
serial.print("/");
serial.print(now.month(), dec);
serial.print("/");
serial.println(now.year(), dec);
//inicia o output para o cartÃo sd
myfile.println("a porta está fechada");
myfile.print(now.hour(), dec);
myfile.print(":");
myfile.print(now.minute(), dec);
myfile.print(":");
myfile.println(now.second(), dec);
myfile.print(now.day(), dec);
myfile.print("/");
myfile.print(now.month(), dec);
myfile.print("/");
myfile.println(now.year(), dec);
*/
lcd.clear();
lcd.setcursor(0,0);
lcd.print(" sistema activo ");
lcd.setcursor(0,1);
lcd.print(" ");
lcd.print(now.hour(),dec);
lcd.print(':');
lcd.print(now.minute(), dec);
lcd.print(':');
lcd.print(now.second(), dec);
delay(200);
}
thanks in advance
the sd card spi device. on 328-based arduino, means uses pins 10, 11, 12, , 13 , can't use pins else.
you trying use pin 13 3 different purposes. real.
you trying use pin 13 3 different purposes. real.
Arduino Forum > Using Arduino > Programming Questions > SD Card error
arduino
Comments
Post a Comment