Olá,
Tenho este código que está a funcionar bem, só que como o arduino vai ter que ficar numa zona sem eletricidade, vou ter que usar baterias. Só que o problema é que tem um consumo de 66mah e com baterias de 2100mah só vai durar sensivelmente 1 dia.
Seria possível alterar o código de modo a durar pelo menos o dobro?
Obrigado pela ajuda.
// 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;
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
const int chipSelect = 4;
File dataFile;
const int buttonSwitch = 7; // pushbutton connected to digital pin 7
// Switch - Button One
int buttonPushCounter = 0;
int buttonStateSwitch = 0; // current state of the Switch button
int lastButtonStateSwitch = 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
// SD
Serial.print("A inicializar o SD...");
pinMode(10, OUTPUT); // it's needed for SD library to work even if the CS is not connected to pin 10
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
}else{
Serial.println("SD inicializado.");
}
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(buttonSwitch, INPUT);
}
}
void loop () {
// Make a string for assembling the data to log:
String dataString = "";
// RTC Time Function
DateTime now = rtc.now();
// Read the Switch pushbutton input pin
buttonStateSwitch = digitalRead(buttonSwitch);
// compare the buttonState to its previous state
if (buttonStateSwitch != lastButtonStateSwitch) {
if (buttonStateSwitch == HIGH) {
Serial.println("O Switch comeu a espiga");
// 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);
String thesec = String(now.second(), DEC);
//Put all the time and date strings into one String
dataString = String("Switch comeu em, " + theyear + "/" + mon + "/" + theday + ", " + thehour + ":" + themin + ":" + thesec);
// 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
lastButtonStateSwitch = buttonStateSwitch;
}