time diffrence betwwen interupts

Dependencies:   mbed-os sd-driver_compatible_with_MAX32630FTHR

main.cpp

Committer:
muigaijosphat
Date:
2019-06-13
Revision:
1:ecf216fbee90
Parent:
0:b7d9a60d3d44

File content as of revision 1:ecf216fbee90:

#include "mbed.h"
#include "SDBlockDevice.h"
#include "FATFileSystem.h"
//#include "stdio.h"
//SDBlockDevice sd(PC_12, PC_11, PC_10, D10);
SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs
InterruptIn motion_sensor(D6); //motin trigger
DigitalIn myinput(D6);  //motion sensor

Timer timer;

int time_lapse,x;
FATFileSystem fs("sd");
FILE*fptr ;

void motion_write() {
    
if(myinput==0){
 timer.start();
 time_lapse= timer.read_ms(); //time between an interrupt
}

        // FILE*fptr = fopen("/sd/myfile.txt", "w");

        
       // fclose(fptr);
    
   // printf("tiggers time is= :%d\r\n",time_lapse);
 timer.reset();

//return time_lapse;

}   
int main(){  
  char date[64];      //string for storing the current date
    char timeofday[64]; //string for storing the current time
    
    time_t seconds;     //time_t variable to read the number of seconds since January 1, 1970 for time calculations
   //time_t now;

    struct tm t;        //time structure for setting the time
    time_t mytime = time(NULL);
    char * time_str = ctime(&mytime);
    time_str[strlen(time_str)-1] = '\0';
    printf("Current Time : %s\n", time_str);
    //set time structure to 20 Sep 2016 00:31:01
    time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
 /* printf ( "Current local time and date: %s", asctime (timeinfo) );
   t.tm_year = 2019 - 1900;
    t.tm_mon = 6 - 1;
    t.tm_mday = 11;
    t.tm_hour = 15;
    t.tm_min = 26;
    t.tm_sec = 00;
    set_time(mktime(&t));*/
      int err = fs.mount(&sd);
    printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
    if (err)
        return err;
        time_lapse= timer.read_ms();
      //  FILE*fptr = fopen("/sd/myfiles.txt", "a");v
      motion_sensor.fall( &motion_write);
      while(1){
     if(myinput==1){wait(3);
         FILE*fptr = fopen("/sd/dspclass.txt", "a");
          
         if ( fptr == NULL )  {
               printf( "Could not open file test.c" ) ;
               return 1;
               }
     
         printf("OUR FILE IS SUCCESSFUL CREATED\t");
         
         seconds = time(NULL);                                           //read the new time value
                strftime(date, 32, "%d/%m/%y", localtime(&seconds));            //create a date string from the time value
                strftime(timeofday, 32, "%H:%M:%S", localtime(&seconds));       //create a time string from the time value
                
                fprintf(fptr, "%s  timelapse is %d\r\n", asctime (timeinfo), time_lapse);   //write the date string, time string, and analog integer value to the file
                 printf( "%s  %d\r\n", asctime (timeinfo),time_lapse);                                                                 //the /r/n combination puts the entries on different lines in the file
                                                                //close the file to prevent corrupt f   
      //  fprintf(fptr,"%d\r\n",time_lapse); 
       
      //  printf("tiggers time is= :%d\r\n",time_lapse);
      
       
    fclose(fptr);
  //  wait(1);
    }
      
       }
       
       
     
    }