Hi group!
I am using arduino for a school project, where I want to measure accelerations, or vibrations and save them on an SD card. What I need is that I set the sampling time, because it gives me a lot of variation when I graph it later, I leave the code, to see if they can see anything ..
#include <SD.h>
#include <SPI.h>
#include <AcceleroMMA7361.h>\
AcceleroMMA7361 accelero;
int x;
int y;
int z;
volatile unsigned MuestreoAnterior = 0;
volatile unsigned MuestreoActual = 0;
volatile unsigned deltaMuestreo = 0;
const int chipSelect = 4;
void setup()
{
Serial.begin(9600); // Preparar las comunicaciones serie para mostrar mensajes en la consola
accelero.begin(13, 12, 11, 10, A0, A1, A2);
accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V
accelero.setSensitivity(LOW); //sets the sensitivity to +/-6G
accelero.calibrate();
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
}
void loop()
{
MuestreoActual = millis();
deltaMuestreo = (double) MuestreoActual - MuestreoAnterior;
if (deltaMuestreo<=60);
x = accelero.getXAccel();
y = accelero.getYAccel();
z = accelero.getZAccel();
Serial.print("\nx: ");
Serial.print(x);
Serial.print(" \ty: ");
Serial.print(y);
Serial.print(" \tz: ");
Serial.print(z);
Serial.print("\tG*10^-2");
delay(10);
String dataString = "";
// read three sensors and append to the string:
dataString += String(MuestreoActual);
dataString += ",";
dataString += String(x);
dataString += ",";
dataString += String(y);
dataString += ",";
dataString += String(z);
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");
}
}//make it readable
thanks a lot