jremington:
Turn off the interrupts and collect sensor data at regular intervals.
I have a civil engineer background so i am not very sure about ISR, because i don't see any interruption on my code, and MarkT confirmed my doubt.
MarkT:
What ISR events? You are being held up writing to the SDcard I think, thats not in an ISR.
You sampling should be done in an ISR, regularly triggered from a timer?
However SDcard uses SPI and the MPU6050 might be driven as SPI too (its I2C or SPI) - so,
as always, post your code so we can figure things out rather than guess wildly.
Yes i forgot to show the code, sorry for that!
It was on the stackoverflow link, but i will put here to.
// For I2C:
#include<Wire.h>
// For the SD Card:
#include <SPI.h>
#include <SD.h>
// Defines:
const int MPU = 0x68; // I2C address of the MPU-6050
const int SD_CS = 10; // SD CS pin
const int LEDpin = 9; // LED
// Declaration of global functions:
void Read_Write();
void blinkL(unsigned long T, int N);
void stampC();
void errorFW();
// Global variables:
unsigned long Time0 = 0; // A starting time reference, in millis
unsigned long TimeD = 0; // Delay required to match the desired R&W frequency.
bool LEDstatus = false;
bool Serial_plus_SD = true; // Output to SD and Serial, or only to SD.
unsigned long TimeDRW = 10UL; // Average time for data reading and writing to file, ms.
unsigned int frequency = 300; // Data R&W frequency, Hz. Limited by TimeDRW: fmax=1000/TimeDRW.
// Setup:
void setup()
{
// Configure the LED:
pinMode(LEDpin, OUTPUT);
// Join I2C bus via Wire library: collagati alla comunicazione I2C attraverso la libreria Wire
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register Collegati con il sensore MPU6050
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
// Initialize serial communication:
if (Serial_plus_SD)
Serial.begin(57600);
// Initialize SD card:
if (Serial_plus_SD)
Serial.println("\nInitializing SD card.");
if (!SD.begin(SD_CS))
{
if (Serial_plus_SD)
Serial.println("Card failed, or not present.");
// Blink LED quickly to indicate trouble(es. se non funziona la SD:
blinkL(200UL, 10);
digitalWrite(LEDpin, LOW);
// And "do nothing else":
while(1)
;
}
else
{
if (Serial_plus_SD)
Serial.println("Card initialized.");
}
// Blink LED to indicate readiness:
blinkL(300UL, 10);
// Questa parte non รจ stata implementata passa diretto al loop
if (1000UL <= TimeDRW * frequency)
TimeD = 0UL; // if the specified frequency is too large, a cap is introduced.
else
TimeD = (1000UL-TimeDRW*frequency)/frequency;
// Initialize time references:
Time0 = millis();
}
// Main:
void loop()
{
// Body of main/loop
// Check status:
//Inizializza il tempo
Time0 = millis();
Read_Write();
// LED off and continue:
digitalWrite(LEDpin, LOW);
}
void Read_Write()
// function that reads the MPU and writes the data to the SD.
{
// Local variables:
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; // Variables read from MPU6050
// Read data:
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true); // request a total of 14 registers
AcX = Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
GyX = Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Serial.print(AcX);
Serial.print(',');
Serial.print(AcY);
Serial.print(',');
Serial.print(AcZ);
Serial.print(',');
Serial.print(GyX);
Serial.print(',');
Serial.print(GyY);
Serial.print(',');
Serial.print(GyZ);
Serial.print(',');
Serial.println();
// Data preparation for file saving:
String dataString = ""; // string for assembling the data to log:
// Add time tag:
dataString += String(Time0); dataString += ",";
// Append the MPU6050 data to the string:
dataString += String(AcX); dataString += ",";
dataString += String(AcY); dataString += ",";
dataString += String(AcZ); dataString += ",";
dataString += String(GyX); dataString += ",";
dataString += String(GyY); dataString += ",";
dataString += String(GyZ);
// Open the file in append mode:
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// If the file is available, write to it:
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
if (Serial_plus_SD)
Serial.println(dataString);
}
// if the file does not open, pop up an error:
else
errorFW();
return;
}
void blinkL(unsigned long T, int N)
// function that blinks the LED N times with a delay of T millis.
{
bool statusL = false;
for (int i=0; i<N; i++)
{
delay(T);
statusL = !statusL;
digitalWrite(LEDpin, statusL);
}
return;
}
void errorFW()
// function that informs about an error while writing in the file, and "holds on" the sketch
{
if (Serial_plus_SD)
Serial.println("Error opening datalog.txt.");
// In any case, blink LED quickly to indicate trouble:
blinkL(200UL, 10);
// LED off and "nothing else":
digitalWrite(LEDpin, LOW);
while(1)
;
return;
}
(Additional Notes: I am studing on the Margolis, i read some guides for begineers, and i am writing my thesis on Matlab, but this is my first approach to coding, and it seems very hard for a self-taught )