Hello:
I'm trying to make a circuit witch save on a SD card the state of switch with day and hour too. I'n not an expert in Arduino so I'm typing the code step by step. Firsty typed the electronic software part, later I included the RTC and now It Works!! But finally I want to sabe the information on a SD card with Ethernet shield. I think I'm very close of the right code but with the sd software part, Ardino doesn't detect the state of the swith.....someone could help me please?? In this moment just I want to know if I can sabe different data based on the state the witch "JAIME" one state, "RAQUEL" the other, this is my code:
// SISTEMA APERTURA Y CIERRE DE HORNO
const int ledHornoAbierto = 2;
const int ledHornoCerrado = 3;
const int puertaHornoSensor = 13;
int estadoPuerta;
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
#include <SPI.h>
#include <SD.h>
File myFile;
void setup()
{
pinMode(ledHornoAbierto, OUTPUT);
pinMode(ledHornoCerrado, OUTPUT);
pinMode(puertaHornoSensor, INPUT);
Serial.begin(9600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(DATE), F(TIME)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop()
{
estadoPuerta = digitalRead(puertaHornoSensor);
if(estadoPuerta==LOW)
{
digitalWrite(ledHornoAbierto, LOW);
digitalWrite(ledHornoCerrado, HIGH);
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(" ");
Serial.println("CERRADO");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("jaime.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile)
{
Serial.print("Writing to test.txt...");
myFile.println("JAIME");
// close the file:
myFile.close();
Serial.println("done.");
}
else
{
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
else
{
digitalWrite(ledHornoAbierto, HIGH);
digitalWrite(ledHornoCerrado, LOW);
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(" ");
Serial.println("ABIERTO");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("jaime.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile)
{
Serial.print("Writing to test.txt...");
myFile.println("RAQUEL");
// close the file:
myFile.close();
Serial.println("done.");
}
else
{
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
delay(1000);
}