Pages: [1]   Go Down
Author Topic: Logging time of RTC once a sensor was triggered in csv format  (Read 1050 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello guys, this is a follow up post from my previous one, below is the code for my counter that im planning to sync with the RTC. Once the sensor was triggered it will record the increment and the timestamp.

This is one is the code for my counter sensor

Code:
//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

//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  buttonPin1 = 3;    // the pin that the pushbutton is attached to
const int  buttonPin2 = 5;
const int ledPin1 = 13;       // the pin that the LED is attached to
const int ledPin2 = 11;

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int buttonPushCounter1 = 0;   // counter for the number of button presses
int buttonState1 = 0;         // current state of the button
int lastButtonState1 = 0;     // previous state of the button

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


void setup() {

  Serial.begin(9600);
  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(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);

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


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin1);
  buttonState1 = digitalRead(buttonPin2);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == 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:
      buttonPushCounter++;
      Serial.println("Bees are being detected entering Feeding Station A");
      Serial.print("number of bees that entered Feeding Station A:  ");
      Serial.println(buttonPushCounter);
    }
   
 
  }
  if (buttonState1 != lastButtonState1)
  {
    // if the state has changed, increment the counter
    if (buttonState1 == 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:
      buttonPushCounter1++;
      Serial.println("Bees are being detected entering Feeding Station B");
      Serial.print("number of bees that entered Feeding Station B:  ");
      Serial.println(buttonPushCounter1);
    }
 

    //Write Log File Header
    File logFile = SD.open("DATALOG.csv", FILE_WRITE);
    if (logFile)
   
     {
       
      logFile.print(buttonPushCounter);
      logFile.print(", ");
      logFile.println(buttonPushCounter1);
      logFile.close();

      Serial.print(buttonPushCounter);
      Serial.print(", ");
      Serial.println(buttonPushCounter1);

    }
    else

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

    }

    lastButtonState = buttonState;
    lastButtonState1= buttonState1;


  }

}











This one is the code to show the time from my RTC DS1307

Code:
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  }

}

void loop () {
    DateTime now = RTC.now();
   
    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.println();
   

 
    Serial.println();
    delay(1000);
}

Some people said i should use interrupt once the sensor was triggered an interrupt function will get the time from the RTC and log it, however i have no idea how to use it. thanks!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset




this image is what i wish to happen in my logger
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 107
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could use an interrupt, but I don't see any reason to do that.  Doesn't look like you've done anything to make your variable names sensible, though (as has been pointed out several times, why are you using the variable "buttonPin" when you aren't using buttons?).  In any event, there are several ways to add the date and time to your logged output.  One of the simpler, in terms of lines of code, methods would be to add these variable declarations at the beginning:
Code:
char dateTimeString[18];
int yr;

...and then add this to whenever you're writing a new line to your logfile:

Code:
DateTime now = RTC.now();
yr = now.year() - 2000;
sprintf (dateTimeString, "%.2d/%.2d/%.2d %.2d:%.2d:%.2d", now.month(), now.day(), yr, now.hour(), now.minute(), now.second());
logFile.println(dateTimeString);

You can adjust the order of the elements and the separators as desired.  If you want to use four-digit years instead, use now.year() instead of yr in the sprintf statement, and increase the length of dateTimeString to 20.

The long way to do it would be to include everything from the loop() in your RTC sketch whenever you want to log something, but change Serial.print to logFile.print.  Lots more code, but doesn't use the sprintf command.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50934
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  buttonState = digitalRead(buttonPin1);
  buttonState1 = digitalRead(buttonPin2);
I love consistency. Too bad there isn't any here.
Logged

Pages: [1]   Go Up
Jump to: