program to run two PIR with time stamp

how does this look. I have it to collect data from hallway traffic from two passive IR sensors. When some one walks by it writes a time stamp to sram. Im running the board on two 6v lantern batteries for a total of 12volts. It looks like in a 24 hour period that im getting some weird times tho. Please help!

#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <EEPROM.h>

#define INIT_TIMER_COUNT 6
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT

int infra_red_a = 2;    // IR Sensor connected to digital pin 2 
int infra_red_l = 3;    // IR Sensor connected to digital pin 3
int int_counter = 0;

volatile int i = 0;
volatile int z = 0;
volatile int timer1=0;
volatile int timer2=0;
volatile int time_limit = ;//The time period between allowed writes to the board
volatile char time_h = 0;//hours,minutes,seconds
volatile char time_m = 0;
volatile char time_s = 0;
int ledPin = 13; //FOR TESTING DUH~
volatile char timeStamp[64][4];
char incomingByte = 0;


 // Aruino runs at 16 Mhz, so we have 1000 Overflows per second...  
 // 1/ ((16000000 / 64) / 256) = 1 / 1000  
 //this is to do timestamps correctly
  ISR(TIMER2_OVF_vect) {  //everytime counter = 1000, 1 sec passed , run clock function
   RESET_TIMER2;  
   int_counter += 1;  
   if (int_counter == 1000) {  
     //second+=1;  
       time_s+=1;
     int_counter = 0;
       clock();
   }   
 };  

void setup(){                             // run once, when the sketch starts
  pinMode(infra_red_a, INPUT);        // sets the digital pin as input 2
  pinMode(infra_red_l, INPUT);              // sets the digital pin as input 3  
  attachInterrupt(0,arrival,RISING);  //interrupts when first sensor goes off
  attachInterrupt(1,leaving,FALLING); //interrupts when second sensor turns off
  pinMode(ledPin, OUTPUT);//MORE TESTING
  Serial.begin(9600);//Sets Baud Rate For Serial Comm.
  Serial.flush();

 // for(int i=0;i<800;i++)
   // timeStamp[i]=0;
    //Timer Settings, for the Timer Control Register etc. , thank you internets
   //Timer2 Settings: Timer Prescaler /64,   
   TCCR2B |= (1<<CS22);      
   TCCR2B &= ~((1<<CS21) | (1<<CS20));       
   // Use normal mode  
   TCCR2A &= ~((1<<WGM21) | (1<<WGM20));    
   // Use internal clock - external clock not used in Arduino  
   ASSR |= (0<<AS2);  
   //Timer2 Overflow Interrupt Enable  
   TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A);    
   RESET_TIMER2;                 
   //sei();  
   //starttime = millis();    
  
 }

void loop(){               // run over and over again
 //dont know if this will actually do anything.... as its all interrupt driven
 delay(1000);
 digitalWrite(ledPin, LOW);
 
  if (Serial.available() > 0) {
            // read the incoming byte:
            incomingByte = Serial.read();
            if(incomingByte = 'a'){
                  sss();}//grab the data and output it to the serial monitor
              else if(incomingByte = 'b'){
                  Serial.flush();}//Clears the serial data
       
}
 

 
}


void sss(){
//this is going to be fun!
//read flash memory
//send all data points to the serial monitor
//POINTS ARE LOST AFTER MONITOR IS CLOSED, DO NOT CLOSE IF U DONT HAVE DATA

//timeStamp[0][0]=87;
int f=0;



for(int k=0;k<z;k++){
      for(int j=0;j<4;j++){
            f=timeStamp[k][j];
            Serial.println(f,DEC);
      }
      }
      

}

void arrival(){
 //EEPROM will be tooo small
/* This is where the code goes that will
 *  Store a timestamp when the 1st sensor triggers
 *  Example Stamp
 *  Sensor - Hour - Minute - Second
*        1         10              5        32
*      110532
*/
if(timer1 == 0){
char arrival = 1;



digitalWrite(ledPin, HIGH);   // sets the LED on


EEPROM.write(i,arrival);
EEPROM.write(i+1,time_h);
EEPROM.write(i+2,time_m);
EEPROM.write(i+3,time_s);

i = i+4;

//timeStamp[z][0]= {arrival,time_h,time_m,time_s};
timeStamp[z][0]=arrival;
timeStamp[z][1]=time_h;
timeStamp[z][2]=time_m;
timeStamp[z][3]=time_s;
z++;
timer1 = 1;
}
}

/*
prog_int16_t   - a signed int (2 bytes) -32,767 to 32,768

*/


void leaving(){
/* This is where the code goes that will
 *  Store a timestamp when the 2nd sensor triggers
 *  Example Stamp
 *  Sensor - Hour - Minute - Second
*        2         10              5        42
*      210532
*/
if(timer2 == 0 ){
char leave = 2;

digitalWrite(ledPin, HIGH);   // sets the LED on

EEPROM.write(i,leave);
EEPROM.write(i+1,time_h);
EEPROM.write(i+2,time_m);
EEPROM.write(i+3,time_s);

i = i+4;

timeStamp[z][0]=leave;
timeStamp[z][1]=time_h;
timeStamp[z][2]=time_m;
timeStamp[z][3]=time_s;
z++;
timer2 = 1;
}
}

void clock(){
int time3 =0;


time_s++;
  if(time_s > 59){
    time_s = 0;
    time_m++;
      time3++;
    if(time_m > 59){
      time_m = 0;
      time_h++;
      if(time_h > 23){
        time_h = 0;
      }
      }
 }
 
 //code to renable writes
 
 if( (time3 >= time_limit) && (timer1==1)){
      timer1=0;
      time3=0;
 }
 if((time3 >= time_limit) && (timer2==1)){
            timer2=0;
            time3=0;
 }
 
};

//EEPROM.write(i, arrival);
//EEPROM.write(i, time_h);
//EEPROM.write(i, time_m);
//EEPROM.write(i, time_s);
//i = sizeof(arrival) + sizeof(time_h) + sizeof(time_m) + sizeof(time_s);

//PROGMEM  prog_uint16_t charSet[]  = { 65000, 32796, 16843, 10, 11234};
//PROGMEM  prog_int16_t timeStamp[z]  = { arrival , time_h , time_m, time_s};

here is some data i have collected on one of my seven boards with in a 26hour period" the first column is hours the second is minutes, is my clock running fast or something i dont get it....

0 1 3 3 3 3 4 4 4 4 19 21 21 21 22 22 23 0 0 0 0 0 0 1 1 1 1 2 2 3 3 3 4 4 5 7 7 7 7 9 9 16 16 16 16 17 17 18 21 22 1 2 2

43 6 22 28 33 36 1 5 25 31 54 18 48 56 37 38 21 8 32 41 44 49 56 2 9 27 48 29 36 34 36 47 55 58 35 18 21 31 46 5 14 17 22 28 32 33 34 46 13 56 58 49 50

Anyone? :frowning:

It's not clear from your description what the problem actually is. You might also want to try re-formatting your sample data.

In your ISR you increment time_s when your millisecond counter (int_counter) reaches 1000:

time_s+=1;

...but then you call clock() which also increments time_s:

time_s++;

So you're double-clocking it seems! :slight_smile:

Joe