Second counter are not logged in the SD card

hi guys!!
I made a program to count the number of bees entering a feeding station. With use of IR sensor. I based my counter program on the “StateChangeDetection” program. I also sync it to RTC DS 1307. so that the time and present number of bees will be logged. I have to make two, so far my sd card are only logging per one sensor (but same code on two sensors). It is in a csv format.

I also want to view my file per time, or whenever the arduino was cut off. another file will be make when open once again. Thank you in advance any insight will do.

//Program by Jeremy Blum
//www.jeremyblum.com
//SD Card Demonstration
//Some code from public domain work by Tom Igoe
//Assisted by Inigo D. Villanueva III
//Edited by Ignacio D. Villanueva III
//For thesis purpose entitled "
//Thesis members: James Santiago. Belen Cruz. Franzes Francisco, Kaye Agut

#include <SD.h>  //SD Card Library
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;
//SPI SD Card Pins
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
const int CS_pin = 10;
int pow_pin = 8;


// this constant won't change:
const int  BeeSensorA = 3;    // IR sensor that will set HIGH whenever a bee passes otherwise LOW
const int  BeeSensorB = 5;


// Variables will change:
int BeeCounterA = 0;                 // counter for the number of bee that had passed sensorA
int CurrentSensorStateA = 0;         // current state of SensorA
int PreviousSensorStateA = 0;       // previous state of SensorA


int BeeCounterB = 0;               // counter for the number of bee that had passed sensorB
int CurrentSensorStateB = 0;       // current state of SensorB
int PreviousSensorStateB = 0;     // previous state of SensorB

long lastEvent= 0; // The last time a bee has entered the feeding station
long interval= 300; // The debounce time


char dateTimeString[30];
int yr;


void setup() {

  Serial.begin(9600);
  Wire.begin();
  RTC.begin();


  Serial.println("Initializing Card");
  //CS Pin is an output
  pinMode(CS_pin, OUTPUT);

  //SD Card will Draw Power from Pin 8, so set it high
  pinMode(pow_pin, OUTPUT);  
  digitalWrite(pow_pin, HIGH);

  //Initialize Card
  if (!SD.begin(CS_pin))
  {
    Serial.println("Card Failure");
    return;
  }
  Serial.println("Card Ready");

  // initialize the button pin as a input:
  pinMode(BeeSensorA, INPUT);
  pinMode(BeeSensorB, INPUT);

  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the Sensor input pin:

  CurrentSensorStateA = digitalRead(BeeSensorA);
  CurrentSensorStateB = digitalRead(BeeSensorB);

  // compare the Sensor State to its previous state
  if (CurrentSensorStateA != PreviousSensorStateA) 
  {
    // if the state has changed, increment the counter for bees
    if (CurrentSensorStateA == HIGH && millis() - lastEvent > interval)// Ignore this reading if it is too close to the last one
    {
      lastEvent= millis(); // a bee can be recorded

      // if the current state is HIGH then the button
      // wend from off to on:

      BeeCounterA++;
      Serial.println("Bees are being detected entering Feeding Station A");
      Serial.print("number of bees that entered Feeding Station A:  ");
      Serial.println(BeeCounterA);
    } 

    if (CurrentSensorStateB != PreviousSensorStateB) 
    {
      // if the state has changed, increment the counter for bees
      if (CurrentSensorStateB == HIGH && millis() - lastEvent > interval)// Ignore this reading if it is too close to the last one
      {
        lastEvent= millis(); // a bee can be recorded

        // if the current state is HIGH then the button
        // wend from off to on:

        BeeCounterB++;
        Serial.println("Bees are being detected entering Feeding Station B");
        Serial.print("number of bees that entered Feeding Station B:  ");
        Serial.println(BeeCounterB);
      } 

    }
    { 
      DateTime now = RTC.now();
      yr = now.year() - 2000;

      sprintf (dateTimeString, "DATE:%.2d/%.2d/%.2d-TIME:%.2d:%.2d:%.2d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
    }

    //Write Log File Header
    File logFile = SD.open("DATALOG.csv", FILE_WRITE);
    if (logFile)

    {

      logFile.print(BeeCounterA);
      logFile.print(", ");
      logFile.println(BeeCounterB); 
      logFile.println(dateTimeString);
      logFile.close();

      Serial.print(BeeCounterA);
      Serial.print(", ");
      Serial.println(BeeCounterB);
      Serial.println(dateTimeString);


    }
    else

    {
      Serial.println("LogFile cannot be opened");

    }

    PreviousSensorStateA = CurrentSensorStateA;
    PreviousSensorStateB = CurrentSensorStateB;


  }

}

only Sensor A are being logged in the SD CARD??

Can we see the contents of the log file from the SD and also the serial output for the same time period?

      logFile.print(BeeCounterA);
      logFile.print(", ");
      logFile.println(BeeCounterB); 
      logFile.println(dateTimeString);

Two values on one record, then the time on the next. Why is that?

That little tiny picture is unreadable. Copying and pasting the text would be a much better idea.

A little snipping is going to happen

  if (CurrentSensorStateA != PreviousSensorStateA) 
  {
    // if the state has changed, increment the counter for bees
    if (CurrentSensorStateA == HIGH && millis() - lastEvent > interval)
    {
      BeeCounterA++;
     } 

    if (CurrentSensorStateB != PreviousSensorStateB) 
    {
      // if the state has changed, increment the counter for bees
      if (CurrentSensorStateB == HIGH && millis() - lastEvent > interval)
      {
        BeeCounterB++;
      } 
    }
  }

You only care about whether the state of sensor B has changed IF the state of sensor A has also changed.

The likelihood of that happening seems pretty remote. The if(CurrentSensorStateB != PreviousSensorStateB) statement and block does not belong inside the if(CurrentSensorStateA != PreviousSensorStateA) block.

Sorry for the late post, i was out for a week, anyway i changed some bracket on my code. The one in the BeeCounterA, what happens is that i was able to see the number of count in A but it cannot be log in the sd. When the BeeCounterB counts, it will log its present value and at the SAME time log the present value of counterA. I tried rearranging my brackets to no avail. I also follow what sir Paul recommended to do.

void loop() {
  // read the Sensor input pin:

  CurrentSensorStateA = digitalRead(BeeSensorA);
  CurrentSensorStateB = digitalRead(BeeSensorB);

  // compare the Sensor State to its previous state
  if (CurrentSensorStateA != PreviousSensorStateA) 
  {
    // if the state has changed, increment the counter for bees
    if (CurrentSensorStateA == HIGH && millis() - lastEvent > interval)// Ignore this reading if it is too close to the last one
    {
      lastEvent= millis(); // a bee can be recorded

      // if the current state is HIGH then the button
      // wend from off to on:

      BeeCounterA++;
      Serial.println("Bees are being detected entering Feeding Station A");
      Serial.print("number of bees that entered Feeding Station A:  ");
      Serial.println(BeeCounterA);
    } 
  }
  if (CurrentSensorStateB != PreviousSensorStateB) 
  {
    // if the state has changed, increment the counter for bees
    if (CurrentSensorStateB == HIGH && millis() - lastEvent > interval)// Ignore this reading if it is too close to the last one
    {
      lastEvent= millis(); // a bee can be recorded

      // if the current state is HIGH then the button
      // wend from off to on:

      BeeCounterB++;
      Serial.println("Bees are being detected entering Feeding Station B");
      Serial.print("number of bees that entered Feeding Station B:  ");
      Serial.println(BeeCounterB);
    }

number of bees that entered Feeding Station A: 1
Bees are being detected entering Feeding Station A
number of bees that entered Feeding Station A: 2
Bees are being detected entering Feeding Station A
number of bees that entered Feeding Station A: 3
Bees are being detected entering Feeding Station B
number of bees that entered Feeding Station B: 1
3, 1
DATE:2012/11/04-TIME:21:49:36
3, 1
DATE:2012/11/04-TIME:21:49:37
Bees are being detected entering Feeding Station B
number of bees that entered Feeding Station B: 2
3, 2
DATE:2012/11/04-TIME:21:49:37
3, 2
DATE:2012/11/04-TIME:21:49:37
Bees are being detected entering Feeding Station A
number of bees that entered Feeding Station A: 4
Bees are being detected entering Feeding Station A
number of bees that entered Feeding Station A: 5
Bees are being detected entering Feeding Station B
number of bees that entered Feeding Station B: 3
5, 3
DATE:2012/11/04-TIME:21:49:40
5, 3
DATE:2012/11/04-TIME:21:49:40

this is what happens on my serial port, In stationA it only shows the count but does not log, in stationB EVERTIME there is a count it will log it with the time included PLUS the present number in station A

Post ALL of your code. It appears that you are logging in the wrong place, still. Counting A bees, counting B bees, and logging A and B bees are (supposed to be) independent activities.

I suggest that you create a function, countA(), containing the code to see if a bee is in the A station.
I suggest that you create a function, countB(), containing the code to see if a bee is in the B station.
I suggest that you create a function, log(), containing the code to log the bee activity.

Then, loop() would be far simpler:

void loop()
{
   countA();
   countB();
   log();
}

Here, you can see that the three activities are completely independent, and there is no possibility of doing it any other way except by calling log() from one of the other functions.

You probably need lastEventA and lastEventB, not just lastEvent.

sorry i have little knowledge on programming From what i see im pretty close to what i wanted to attain. That thing is actually happening on the sensor B, it log every count with its specific time but not in sensor A. But i can see also that same code was used in both sensor. thank you!