Hey
I´m working on a measuring unit for my model railroad, but i have some problems.
It´s read data from a Hall sensor and logging it on a SD card. the problem is, after some time it stop logging, and i can press reset on my Uno and it´s starts again. Can someone please help me with my code?
#include <SD.h>
int rpmcount = 0 ;
unsigned long lastmillis = 0;
File logfile;
const int chipSelect = 10;
void setup(){
Serial.begin(9600);
Serial.println("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
attachInterrupt(0, rpm_fan, FALLING);
}
void loop(){
if (millis() - lastmillis == 1000){
detachInterrupt(0);
float rpm = rpmcount*43.5 ;
float kmp = rpm/69.08 ;
logfile = SD.open("log.txt", FILE_WRITE);
if (logfile) {
logfile.print("Km/t: ");
logfile.print(kmp);
logfile.print(" - ");
logfile.print("Impulser: ");
logfile.println(rpmcount);
logfile.close();
Serial.print("Km/t: ");
Serial.print(kmp);
Serial.print(" - ");
Serial.print("Impulser: ");
Serial.println(rpmcount);
}
else {
Serial.println("error opening file");
}
rpmcount = 0; //
lastmillis = millis();
attachInterrupt(0, rpm_fan, FALLING);
}
}
void rpm_fan(){
rpmcount++;
}
Cheers
Peter