Go Down

Topic: SD Card Logging + attachInterrupt Problem! (Read 991 times) previous topic - next topic

TCPR

Hello everyone,

I am trying to make a data logger to log a few sensors. It's everything working properly until I start logging data to the SD card.
The problem is that I have a Hall Effect sensor connected to the board using attachInterrupt() to calculate wheel speed, but when the SD card logging is on the reading go crazy.

I made a sketch to test just this issue, only hall sensor + sd card connected to the board with two different SD libraries and the problem still occurs.

This is an example of what I get when I put a magnet in front of the hall sensor, or even if I just pass it fast in front of the sensor like 1 or 2 times:

Quote
DATA, 318, 309, 311
DATA, 319, 311, 313
RPMcount: 227
DeltaT: 1640
V: 208.71
DATA, 320, 312, 314
RPMcount: 50
DeltaT: 554
V: 136.75
DATA, 318, 309, 311


Line DATA is what is being write to SD. In this sketch I'm not logging data from hall sensor if it matters.
RPMcount should always be equal 4!

Here is my simplified code:
Code: [Select]
#include <SD.h>

const int chipSelect = 51;

volatile byte rpmcount_D1 = 0;
unsigned long timeold_D1 = 0;
float rpm_D1 = 0, V_D1 = 0;
const int rodaD1 = 2;

void setup()
{
  Serial.begin(57600);
  Serial.print("Initializing SD card...");
//  pinMode(53, OUTPUT);
 
  if (!SD.begin(chipSelect)) { Serial.println("Card failed, or not present"); return; }
  Serial.println("card initialized.");
 
// Velocidade das Rodas ------------------------------------------------------
  pinMode(rodaD1,INPUT);
  attachInterrupt(0, rev_add, FALLING);
// Velocidade das Rodas ------------------------------------------------------

}

void loop()
{
 
// Bloco do Sensor Hall ------------------------------------------------
  if (rpmcount_D1 >= 4) {
    rpm_D1 = 6283.19 / (millis() - timeold_D1) * rpmcount_D1/4;
    V_D1 = rpm_D1 * 0.9576; // V [Km/h]
    Serial.print("RPMcount: "); Serial.println(rpmcount_D1, DEC);
    Serial.print("DeltaT: "); Serial.println((millis()-timeold_D1), DEC);
    rpmcount_D1 = 0; timeold_D1 = millis();
    Serial.print("V: ");Serial.println(V_D1);
  }
// Bloco do Sensor Hall -------------------------------------------------

    // make a string for assembling the data to log:
    String dataString = "";
    // read three sensors and append to the string:
   
    //dataString += String(tempo);
   dataString += "DATA";
    dataString += ", "; // Adiciona o tempo no inicio da String
    for (int analogPin = 0; analogPin < 3; analogPin++) {
      int sensor = analogRead(analogPin);
      dataString += String(sensor);
      if (analogPin < 2) { dataString += ", "; }
    }
    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    File dataFile = SD.open("datalog.txt", FILE_WRITE);
    // if the file is available, write to it:
    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"); }
}

// Funcoes do Sensor Hall --------------------------------------
void rev_add() { rpmcount_D1++; }


Thanks for your help and sorry for my "engrish"!
Cheers!

Go Up