Pages: [1]   Go Down
Author Topic: Using a RTC to take readings  (Read 780 times)
0 Members and 1 Guest are viewing this topic.
Wisconsin, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello All.

I'm a noob programmer for adruino.  I was able to get my i2c RTC working, commuicating, and printing with a Serial.print().

What I am trying to do with my RTC is take reading from a analog temp sensor, an i2c pressure sensor, an analog wind speed sensor, and an i2c compass module.  (More or less its a home built weather station.)

I want the reading to happen every 5 min.  I dont know if I am even heading in the right direction.  Currently I have:

Code:
if(now.minute() == 5,10,15,20,...,55,00)
{
     // RTD Day Of the Week
    if(dispDOW == 0)
      Serial.print("SUN: ");
    if(dispDOW == 1)
      Serial.print("MON: ");
...
...
              // Temp Display in Deg F
      Serial.print("  Temp:  "); // Temp will be in a format of
      Serial.println(degF);         // ##.#
   
        Serial.println(MPH);
       
}

Any thoughts?!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19080
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This part is wrong:

Code:
if(now.minute() == 5,10,15,20,...,55,00)

You want more like:

Code:
if(now.minute() % 5 == 0)

That divides by 5, gets the remainder, and if it is zero, then 5 minutes are up.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19080
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

However a word of caution. When the time is (say) 10:05, then the "5 minutes are up" will happen thousands of times (until it becomes 10:06). You also need to check if the time has changed (eg. if the minute now is different from the minute a moment ago).
Logged


Dee Why NSW
Offline Offline
Edison Member
*
Karma: 48
Posts: 2300
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think there may be a conceptual thing here but it may be just a matter of language. You don't use the RTC to gather the data, that is the Arduino's job and the RTC tells it when to do it.

You apparently have the RTC (nearly!) sorted. All you need is to get some code together to read a sensor, than  add code for another sensor etc. Once done, then bring the RTC into play. You will probably find that you don't really need the RTC to do this sort of thing but it is still the best way to go as it increases the versatility and viability of your project.
« Last Edit: February 08, 2013, 09:07:00 pm by Nick_Pyner » Logged

Wisconsin, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

However a word of caution. When the time is (say) 10:05, then the "5 minutes are up" will happen thousands of times (until it becomes 10:06). You also need to check if the time has changed (eg. if the minute now is different from the minute a moment ago).


Would adding the use of my now.second() take care of thousands of readings?

Code:
if(now.minute() /5 ==0 && now.second() ==0)
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19080
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Modulus is not the same as divide, you know.

No, now.second() won't really help. It will execute your main loop thousands of times, probably, in one second.

You want something like:

Code:
if (now.minute () != previousMinute)
  {
  previousMinute = now.minute ();
  if (now.minute () % 5 == 0)
    {
    // do something
    }
  }
Logged


New Jersey
Online Online
Faraday Member
**
Karma: 70
Posts: 3734
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For your purposes, I'd suggest that you don't need the RTC to control the five minute interval at all, you can just use the good old blink without delay method to have millis do that. If you actually want to timestamp your data, just ask the RTC what time it is whenever millis tells you it's time to read your sensors.
Logged

Wisconsin, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...just ask the RTC what time it is whenever millis tells you it's time to read your sensors.

Code:
// something like below
if (millis() % 6000 == 0)
{
Serial.print (weather data)
}

That worked out perfectly! it only displays the time temp and so on once! and it prints it every 5 min!!


Thank you all for your great ideas! I learned a lot!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19080
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
// something like below
if (millis() % 6000 == 0)
{
Serial.print (weather data)
}

Caution though, millis() might jump a few units while you are processing. In other words, you might miss exactly the 6000 point.

Better to have a "lastDisplayTime" variable, and compare until it has passed the required time. eg.

Code:
if (millis() - lastDisplayTime >= 300000)   // 5 minutes up?
  {
  // display stuff
 
  lastDisplayTime = millis ();  // when we updated
  }
Logged


Pages: [1]   Go Up
Jump to: