I'm using a Ardino Uno and a Ethernet shield with micro SD slot.
The main goal is to execute a routine in preestablished time intervals for when I'll save some data I get through 4 wire from an old measuring system that I have.
For now, I'm trying to reproduce an exemple of Datalogger from the SD library.
It works like this: it will read the input from A0, A1, A2 and show it at the serial port and save it in a micro SD card.
I don't need all data, I need to save just it periodically. So, I'm using the same script of Datalogger with the same circuit. It worked.
But, when I join the Timer1 example and the Datalogger I always get errors compiling the sketch into the Arduino, mainly after"Timer1.attachInterrupt" and timer1.update.
I would appreciate help with functions that I should use. What are my mistakes?
Sources:
http://playground.arduino.cc/code/timer1
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
#include <TimerOne.h>
/* No Escudo Ethernet, CS é o pino 4 Note-se que mesmo que ele não é usado como o pino CS,
o pino de hardware CS (10 na maioria das placas Arduino, 53 no mega) deve ser deixado como
uma saída ou as funções da biblioteca SD vontade não funciona. */
// TimerOne timer1Datalogger; // Timer para movimentacao do servo
const int chipSelect = 4; // fazer funcionar corretamente
void setup(void)
{
Timer1.initialize(500000); // initialize timer1, and set a 1/2 second period
// Abra comunicação serial e espere por porta para abrir
Serial.begin(9600);
while (!Serial) {
; // esperar que a posta serial conecte. Needed for Leonardo only
Timer1.attachInterrupt(Datalogger); // attach the service routine here
}
Serial.print("Initializing SD card...");
// certificar-se de que selecionou o chip padrão selecione pino é definido como
// Saída, mesmo se você não usá-lo:
pinMode(10, OUTPUT);
// ver se o cartão está presente e pode ser inicializado:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// Não faz mais nada
return;
}
Serial.println("card initialized.");
}
void loop()
{
timer1Datalogger.update() ;// executa a chamada para a contagem de tempo do timer
void Datalogger (void)
{
// fazer uma seqüência (string) para montar os dados para login:
String dataString = "";
// leia três sensores e acrescente à string
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
}
}
// abrir o arquivo. note que apenas um arquivo pode ser aberto por vez,
// Então você tem que fechar este antes de abrir outro.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// Se o arquivo estiver disponível, escreva nele:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}