RTC Code Trouble

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
lexical43
 
Posts: 24
Joined: Tue Jun 24, 2014 6:10 pm

RTC Code Trouble

Post by lexical43 »

All,

Can someone help identify what I am doing wrong in the following code:

I am attempting to read the flow data from a hall effect sensor. The sample code for the sensor runs fine until I added the code to log the date and time from the RTC (Chronodot). Sensor is on pin 2 and powered with 5V. Chronodot is powered with 3V from uno board. Chronodot SDA is connected to A4 and SCL is A5. Chronodot when run with a sample code works fine. Something is probably wrong with the way I am calling the interrupt.

Code: Select all

/**********************************************************
This is example code for using the Adafruit liquid flow meters. 

Tested and works great with the Adafruit plastic and brass meters
    ------> http://www.adafruit.com/products/828
    ------> http://www.adafruit.com/products/833

Connect the red wire to +5V, 
the black wire to common ground 
and the yellow sensor wire to pin #2

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Limor Fried/Ladyada  for Adafruit Industries.  
BSD license, check license.txt for more information
All text above must be included in any redistribution
**********************************************************/
#include <Wire.h>
#include "RTClib.h"  // Credit: Adafruit

volatile RTC_DS1307 RTC;

#include <SD.h>

File myFile;
String fileName = String("WaterMon.csv");

// which pin to use for reading the sensor? can use any pin!
#define FLOWSENSORPIN 2

// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
  uint8_t x = digitalRead(FLOWSENSORPIN);
  
  if (x == lastflowpinstate) {
    lastflowratetimer++;
    return; // nothing changed!
  }
  
  if (x == HIGH) {
    //low to high transition!
    pulses++;
  }
  lastflowpinstate = x;
  flowrate = 1000.0;
  flowrate /= lastflowratetimer;  // in hertz
  lastflowratetimer = 0;
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
  }
}

void setup() {
   Serial.begin(115200);
   Serial.print("Flow sensor test!");
   
   //initialize SD card 
   pinMode(10, OUTPUT);
    if (!SD.begin(4)) {
      return;
    }
   myFile = SD.open("WaterMon.csv",FILE_WRITE);
    if (myFile) {
    myFile.println("Date, Time, Frequency, Pulses, Liters");
    myFile.close();
    } 
    else {
      //do nothing
     }
   
   pinMode(FLOWSENSORPIN, INPUT);
   digitalWrite(FLOWSENSORPIN, HIGH);
   lastflowpinstate = digitalRead(FLOWSENSORPIN);
   useInterrupt(true);
}

void loop()                     // run over and over again
{ 
  // Get the current time
  DateTime now = RTC.now();
  
  // if a plastic sensor use the following calculation
  // Sensor Frequency (Hz) = 7.5 * Q (Liters/min)
  // Liters = Q * time elapsed (seconds) / 60 (seconds/minute)
  // Liters = (Frequency (Pulses/second) / 7.5) * time elapsed (seconds) / 60
  // Liters = Pulses / (7.5 * 60)
  float liters = pulses;
  liters /= 7.5;
  liters /= 60.0;


  // if a brass sensor use the following calculation
  //  float liters = pulses;
  //  liters /= 8.1;
  //  liters -= 6;
  //  liters /= 60.0;

    myFile = SD.open("WaterMon.csv", FILE_WRITE);
    if (myFile) {
      myFile.print(now.year(),DEC); myFile.print('/'); myFile.print(now.month(), DEC); myFile.print('/'); myFile.print(now.day(), DEC);
      myFile.print(",");
      myFile.print(now.hour(), DEC); myFile.print(':'); myFile.print(now.minute(), DEC); myFile.print(':'); myFile.print(now.second(), DEC);
      myFile.print(",");
      myFile.print(flowrate);
      myFile.print(",");
      myFile.print(pulses, DEC);
      myFile.print(",");
      myFile.print(liters);
      myFile.println(",");
      myFile.close();
    }
      else{
      //Do nothing
      }

  Serial.print("Current time: ");
  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.print("Freq: "); Serial.println(flowrate);
  Serial.print("Pulses: "); Serial.println(pulses, DEC);
  Serial.print(liters); Serial.println(" Liters");

 
  delay(1000);
}
Last edited by Franklin97355 on Thu Sep 11, 2014 3:23 pm, edited 1 time in total.
Reason: Added code tags.

User avatar
SeiRruf
 
Posts: 82
Joined: Thu Sep 04, 2014 3:07 am

Re: RTC Code Trouble

Post by SeiRruf »

I am not sure if it's just me, but when I load your code into my Arduino 1.5.7 IDE, it won't compile, stating there's a missing library.

When I added #include <SPI.h> right after line 25 (#include <SD.h>) and compiled, everything went smoothly. I don't have your same setup, so I am unable to reproduce your exact errors otherwise.

This wouldn't be the solution to your problem, would it?

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: RTC Code Trouble

Post by adafruit_support_bill »

The sample code for the sensor runs fine until I added the code to log the date and time from the RTC (Chronodot).
And what exactly does it do (or not do) now?
To narrow down the problem - try commenting out parts of the code you added (e.g. read the time but don't log). If it starts working again, the last thing you commented out will be the source of the problem.

User avatar
lexical43
 
Posts: 24
Joined: Tue Jun 24, 2014 6:10 pm

Re: RTC Code Trouble

Post by lexical43 »

The code compiles just fine on my unit so it's strange it won't compile on another.

As the code is I get "Flow Sensor Test!" In the serial monitor and the headings are written to the SD card file. Then the program hangs. If I comment out everything to do with RTC it runs fine. Tried commenting out the interrupt but no improvement.

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: RTC Code Trouble

Post by adafruit_support_bill »

The code compiles just fine on my unit so it's strange it won't compile on another.
He's using 1.5.7 which is still beta software. Many libraries are not compatible with it. Your code compiles fine on the 1.0.5 release.
If I comment out everything to do with RTC it runs fine.
I don't see a wire.begin() or rtc.begin() in your setup.

User avatar
lexical43
 
Posts: 24
Joined: Tue Jun 24, 2014 6:10 pm

Re: RTC Code Trouble

Post by lexical43 »

...helps to initialize the features you wan to use.

Thanks for the help.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”