hello,
I'm developing a simple program that logs some data to a sd card.
however the sd card never initializes.
i'm using:
Arduino uno
Sd card shield (http://imall.iteadstudio.com/im120417007.html) (switch at 3.3v)
LCD shield (Gravity: 1602 LCD Keypad Shield For Arduino - DFRobot)
here's my code:
#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 DO 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 now = 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 A 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 now = 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 A 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 now = RTC.now();
/*
//INICIA O OUTPUT PARA A 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