Olá.
Tenho um Arduino Uno, um leitor sd e um real time clock.
O que estou a tentar fazer é, quando carrego no interruptor, a hora e data ficam registadas no cartão, só que estou com problemas no código.
Será que alguém pode ajudar? Desde já, muito obrigado.
O código
// Date and time functions using DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>
RTC_DS1307 rtc;
File dataFile;
const int buttonWater = 7; // pushbutton connected to digital pin 7
// Water – Button One
int buttonPushCounter = 0;
int buttonStateWater = 0; // current state of the water button
int lastButtonStateWater = 0; // previous state of the button
void setup()
Serial.begin(9600);
Wire.begin();
rtc.begin();
// Check that the microSD card exists and can be used
Serial.print("Initializing SD card…");
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("Card failed, or not present");
while (1) ;
}
Serial.println("card initialized.");
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(__DATE__, __TIME__));
// Set my digital pins as inputs or outputs
pinMode(buttonWater, INPUT);
}
}
void loop () {
// Make a string for assembling the data to log:
String dataString = "";
// RTC Time Function
DateTime now = rtc.now();
// Read the water pushbutton input pin
buttonStateWater = digitalRead(buttonWater);
// compare the buttonState to its previous state
if (buttonStateWater != lastButtonStateWater) {
if (buttonStateWater == HIGH) {
Serial.println("Button one is pressed");
// Create the file for writing the data
dataFile = SD.open("datalog.txt", FILE_WRITE);
// Assemble Strings to log data
String theyear = String(now.year(), DEC);
String mon = String(now.month(), DEC);
String theday = String(now.day(), DEC);
String thehour = String(now.hour(), DEC);
String themin = String(now.minute(), DEC);
//Put all the time and date strings into one String
dataString = String("water, " + theyear + "/" + mon + "/" + theday + ", " + thehour + ":" + themin);
// Open the datafile and write the dataString to the microSD card
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
// if the file isn’t open, register an error:
else {
Serial.println("error opening datalog.txt");
}
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonStateWater = buttonStateWater;
}