DSPX01 Accelerometer data logging

hi , my project is counting the number of steps and total number of steps in each and every 5 min time interval and put the 5 min pedo data into a file and close it then repeat the same for 100 files

i can log the pedo sensor data upto 5 min but the steps are not counting properly as i move my sensor for 10 time forward & backword for making a step , the steps inside the file is shows 2 or 3 steps only
as of of now i could able to load the 5 min data in one file only how to repeat this thing for 100 files ?

please reffer attachment for code

_1_min_RTC.ino (3.71 KB)

If you put every { on a new line, and properly indented your code, it would be perfectly obvious what the problem is.

Think about WHEN you determine the analog reading to decide is a step has been taken. You are NOT doing it in the proper place.

my project is to count the steps and total steps within the time stamps using the Real time clock

1.I have used DSPX01 Accerelometer Sensor for counting steps and total steps (http://www.dorji.com/docs/data/DSPX01.pdf)

2.I have used RTC DS1307 realtime clock for time stamps
(http://datasheets.maximintegrated.com/en/ds/DS1307.pdf)

3.SD card reader for micro SD card to store the data files
4. Arduino MEGA 2560 board

Sensor connected to Analog port A0 of arduino board and for code please reffer the attachment
i have given 670 volts threshold level when it exceed at analog port A0, step n Total step counters will get increment by 1
i am geeting the time stamps from RTC every sec
and I set the counter1=300 (5 min x 60 sec) in my code fordata logging will be upto 5 minutes of interval and flush data in .csv file in a SD card

problem is wrong counting steps
when i am moving accelerometer sensor for making steps, i did 10 steps but data in .csv file shows only 3 steps n total steps

problem is wrong counting steps

So, ditch all the code for logging crap. There is NO sense in logging data that is no good.

When you do that, I think you will see that the problem is NOT with actually reading that a step occurs. It is with WHEN you are actually reading the data.

Looking, now and then, is not a good idea. You must be looking all the time, looking away ONLY long enough to do something else.

You are looking away most of the time, looking at the accelerometer only now and then.

sorry ,i couldnt understand what are you trying to say can you mentione clearly !

i have tested my DSPX01 acccelerometer sensor with basic step counting program

int value=analogRead(A0);

if(value>670) // threshold for counting steps
{
StepCnt+=1; //increment the counters
TotalCnt+=1;
}

so this code worked fine Then i moved to log the data for 5 min using RTC
I THING problem with the SYN TIME in my code ( i have used 1 sec syn time to get the sensor values So within 1 sec fetching the sensor value is not proper )

If you put every { on a new line, and properly indented your code,

Have you done that? If not, GET BUSY!

Here is my code

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include<SPI.h>

#define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
#define pedoPin 0           // analog 0,DSPX01 accelerometer sensor

RTC_DS1307 RTC; // define the Real Time Clock object

uint32_t syncTime = 0; // time of last sync()
int StepCnt=0;
int TotalCnt=0;

const int chipSelect = 53;
unsigned long currentmillis;
unsigned long prevmillis=0;
int counter=0;
int counter1=300;  // 5 min time period data logging counter 5*60=300 sec 

File logfile;     // creating a log file 
void error(char *str)  // error declaration 
   {
   Serial.print("error: ");
   Serial.println(str);
   while(1);
   }

void setup(void)

{
    Serial.begin(9600);
    Serial.println();
    Serial.print("Initializing SD card...");// initialize the SD card
    pinMode(53, OUTPUT); // make sure that the default chip select pin is set to
    
    if (!SD.begin(chipSelect)) {  // see if the card is present and can be initialized:
    error("Card failed, or not present");
         }
         
    Serial.println("card initialized.");
    
    Wire.begin(); 
       
    if (!RTC.begin()) {     // connect to RTC DS1307 for time n date 
    logfile.println("RTC failed");
    }
    
    char filename[] = "LOGGER00.CSV";  // creating the .CSV file for data logging 
    for (uint8_t i = 0; i < 10; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
         
         if (! SD.exists(filename)) {      // if file exist write sensor ouput data to file 
         logfile = SD.open(filename, FILE_WRITE);// logfile is only open a new file if it doesn't exist
         logfile.println("Date,Time,StepCount,TotalStepCount"); //print these characteres at top of the file 
         break;  // if above condition is true go the next loop
            }
         }
}

void loop(void)
{

  if(counter<counter1){       // couter start from 0 counts upto 300 for each n every sec
    currentmillis=millis();
    if(currentmillis>prevmillis){  // exicute the loop for each and every sec
    DateTime now;
    delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));// delay for the amount of time we want between readings
    now = RTC.now();    // fetch the time from RTC
    logfile.print(now.day(), DEC); // log time
    logfile.print("/");
    logfile.print(now.month(), DEC);
    logfile.print("/");
    logfile.print(now.year(), DEC);
    logfile.print(",");
    logfile.print(now.hour(), DEC);
    logfile.print(":");
    logfile.print(now.minute(), DEC);
    logfile.print(":");
    logfile.print(now.second(), DEC);
    logfile.print('"');
    //#if ECHO_TO_SERIAL  // if serail data is true 
    Serial.print('"');
    Serial.print(now.year(), DEC);
    Serial.print("/");
    Serial.print(now.month(), DEC);
    Serial.print("/");
    Serial.print(now.day(), DEC);
    Serial.print(",");
    Serial.print(now.hour(), DEC);
    Serial.print(":");
    Serial.print(now.minute(), DEC);
    Serial.print(":");
    Serial.print(now.second(), DEC);
    Serial.print('"'); 
   //#endif //ECHO_TO_SERIAL

   int Reading = analogRead(A0);
       if(Reading>670) {   //threshold for counting  step n totalstep 
       StepCnt+=1;
       TotalCnt+=1;
       delay(100);  
       }
   logfile.print(", "); 
   logfile.print(StepCnt);    // printh the steps n total steps in a file 
   logfile.print(", "); 
   logfile.print(TotalCnt);
   //#if ECHO_TO_SERIAL
   Serial.print(", ");
   Serial.print(StepCnt);  // for serial monitoring 
   Serial.print(", ");
   Serial.print(TotalCnt);
   //#endif //ECHO_TO_SERIAL
   logfile.println();
   //#if ECHO_TO_SERIAL
   Serial.println();
   //#endif // ECHO_TO_SERIAL
   if ((millis() - syncTime) < SYNC_INTERVAL) return;// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
   syncTime = millis();
   currentmillis=millis(); // update the time 
   prevmillis=currentmillis; // update the prevmillis 
   counter+=1;               // increment counter for each sec 
   logfile.flush();         // flush the data in file 
   }

 // Serial.println("Date,Time,StepCount,TotalStepCount");
 
 if(counter==counter1){   // if counter is 300 sec  = 5min*60 sec  then stop logging the data                           
  Serial.println("stop logging data");
  logfile.close();      // close the file 
  }
 }
 }