Go Down

Topic: Help needed (Read 737 times) previous topic - next topic

ricemann

Hey

I´m working on a measuring unit for my model railroad, but i have some problems.  :smiley-sad-blue:

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


AWOL

Code: [Select]
if (millis() - lastmillis == 1000){
>= is safer.

Please remember to use code tags when posting code
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

rpm_count is used in loop() and in the ISR, and yet is not declared volatile. It should be.

Does the serial output stop, too? Or does that continue while the writing to the card is what stops?

Using spaces in you code or (even better, in my opinion) putting each { on a new line would improve readability.

How often is the ISR triggered? Is there a possibility that rpm_count could overflow?

mrburnette

Peter,
Quote
<...> 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.


The performance of SD recording is vastly affected by the SD card characteristics.  In the data logging that I designed for my friend's Europa, I decided to off-load the entire process to a dedicated uC... A $2 decision that solved potential headaches.

http://forum.arduino.cc/index.php?topic=154864.msg1160998#msg1160998


Ray

Go Up