Data logging shield - how to wake up every 15 minutes

Hello everyone. I am new in the Arduino's world. I love it so far.
I have:
Arduino UNO
IR temperature sensor (measures ambient and object temp)
Data logger shield (https://www.tindie.com/products/Dead_Bug_Prototypes/extreme-low-power-data-logging-shield-for-arduino/)

I bought this shield because it can go to sleep using a "coin type" baterry. Also, has a slot for microSD.
I believe the WAKING UP stuff is with something called DS1337 ?? I found that in a sketch in the web site of the shield.

I want to make measures every 15 minutes, write it on the SD card, and put it to sleep 15 minutes. And so on,
I was able to make it do everything, but I CANT MAKE IT GO TO SLEEP AND COME BACK. Anyone knows about this??

This is my sketch, without any line for waking up (after this I put the sketch the developer sent me to try, but it's not working):

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_MLX90614.h>
const int chipSelect = 8;
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
RTC_DS1307 RTC;

File myFile;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Wire.begin(); //IR Sensor
  RTC.begin();  //IR Sensor
  mlx.begin();  //IR Sensor

  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  pinMode(10, OUTPUT);

  if (!SD.begin(8)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");

    DateTime now = RTC.now();   //IR SENSOR
    myFile.print(now.day(), DEC);
    myFile.print('.');
    myFile.print(now.month(), DEC);
    myFile.print('.');
    myFile.print(now.year(), DEC);
    myFile.print("\t");

    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print("\t");

    myFile.print("SensorTemp="); 
    myFile.print("\t") ; 
    myFile.print(mlx.readAmbientTempC()); 
    myFile.print("\t*C\tCorn=\t"); 
    myFile.print(mlx.readObjectTempC()); 
    myFile.println("\t*C");
    myFile.println();

    // close the file:
    myFile.close();
    Serial.println("done.");
  } 
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } 
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
// nothing happens after setup
}

This is the sketch the developer sent me, but is not working for me:

To have the logger wake up every 30 minutes you need to add the following function to your sketch.

void SetAlarmMin(uint8_t AntallMin)
{
Wire.begin(DS1337_ADDRESS);
Wire.beginTransmission(DS1337_ADDRESS);
Wire.write((byte) 0);
Wire.endTransmission();
Wire.requestFrom(DS1337_ADDRESS, 2);
Wire.read(); // hopper over sekunder
uint8_t mm = bcd2bin(Wire.read());
mm = mm + AntallMin;
if (mm  59) mm = mm - 60;
Wire.beginTransmission(DS1337_ADDRESS);
Wire.write((byte) 7);
Wire.write(0b00000000); // Alarm 1 sekunder
Wire.write(bin2bcd(mm)); // Alarm 1 minutter
Wire.write(0b10000000); // Alarm 1 timer (flagg satt)
Wire.write(0b10000000); // Alarm 1 dato (flagg satt)
Wire.write(0b00000000); // Alarm 2 minutter
Wire.write(0b00000000); // Alarm 2 timer (flagg satt)
Wire.write(0b00000000); // Alarm 2 dato (flagg satt)
Wire.write(0b00011101); // skru av ocillator og enable alarm 1
Wire.write((byte) 0); // Nullstill alt
Wire.endTransmission();
}
//This will take an integer and set the alarm accordingly with this function call in you main loop (that puts your logger to sleep)
SetAlarmMin(10); // Sets the alarm 10 minutes for now

THANKS!

I never work with SLEEP in Arduino, but it seams easy. Take a look to the following page:
http://playground.arduino.cc/Learning/arduinoSleepCode

For what I see in your code, you don't "send the device to sleep" mode. You don't configure the wakeup too. Take a look to the example of the page of the link, and then think how to make that with this RTC.

I tried that. It seems that CODE is just to put the Arduino to sleep, and wake it up with an external swith. What I need is it to wake up by itself every 15 minutes, write the measures, and go back to sleep.
This is the third day, and I can't make it work. Or this is quite hard or i'm dumb.

OK. So now that I know that you try this code, can you please tell me where in your code you put the Arduino sleeping?

For long sleeps.
https://code.google.com/p/narcoleptic/

void longSleep(long milliseconds) 
{ 
    while(milliseconds > 0)
  { 
       if(milliseconds > 8000) 
      { 
             milliseconds -= 8000; Narcoleptic.delay(8000); 
      } 
       else 
      { 
         Narcoleptic.delay(milliseconds); 
         break; 
      } 
  } 
}

I haven't seen that shield before - it looks quite clever. I suspect that sleeping the Arduino using this shield will require an approach which is specific to this shield. Presumably the people selling the shields also provide sample code demonstrating how to use it. Have you found and run that?

The "stuff is with something called DS1337" is an RTC. An RTC is called like that because it is a Real Time Clock. It keeps the time. In the case of the "DS1337" you can have also alarms. When the time of RTC reaches the time that is configured in the alarm is activated one line that is attached to the Arduino that is called interruption (In this case, because the interruption came from outside the "Arduino chip" is called an external interruption).

What you really need to do is something like what is in the link that I give you, but instead of one button you will have the DS1337 (if you what, this device will push the button for you).

Pick this example, add the code for configuring the RTC, then add the function to save the alarm. Add the code for reading the sensors and write the value to the SD card. Then you need to change the loop() function to do what you want. And what you want? You want that every time the Arduino wakes from sleep it reads the sensors, save the value to the SD card (don't forget to close the SD card) read the time, set the alarm to time+15min and then go to sleep again.

So, like I try to say in my first reply, you need to study very well the code from the link, and then add the other stuffs.

The thing is that the designer of the shield is on vacations, so I have to figure this out.
Luis, thanks a lot for the explanation. Now I understand what I have to do.

Anyway, do I need to connect the Rx to the D2? Even if I am not using a switch?
Thanks

I don't know if I understand the question, but if you are talking about the example of the link the answer is yes. But you must note that you must change the D2 to other pin. You must chose the pin where the INT1 or the INT2 (from the RTC) are connected. Do you have the schematic of the shield?

My question was if I need to connect the Rx to the D2 to make the Arduino go to sleep and wake up automatically. I know in the example I need this because I tried.
But know I understand that what I need to do is with the DS1337. I'm having problems with this RTC, it seems codes are overlapping.
I'm following the next web site tutotial:

This is my code right now:

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_MLX90614.h>
#include <avr/power.h>
#include <avr/sleep.h>

#include <DS1337.h>
DS1337 RTC = DS1337();
RTC_DS1307 RTC;

const int chipSelect = 8;
Adafruit_MLX90614 mlx = Adafruit_MLX90614();




File myFile;

void setup()
{
  pinMode(PIN_INT2, INPUT);
  digitalWrite(PIN_INT2, HIGH);
  RTC.start();
  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Wire.begin(); //IR Sensor
  RTC.begin();  //IR Sensor
  mlx.begin();  //IR Sensor

  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  pinMode(10, OUTPUT);

  if (!SD.begin(8)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");

    DateTime now = RTC.now();   //IR SENSOR
    myFile.print(now.day(), DEC);
    myFile.print('.');
    myFile.print(now.month(), DEC);
    myFile.print('.');
    myFile.print(now.year(), DEC);
    myFile.print("\t");

    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print("\t");

    myFile.print("SensorTemp="); 
    myFile.print("\t") ; 
    myFile.print(mlx.readAmbientTempC()); 
    myFile.print("\t*C\tCorn=\t"); 
    myFile.print(mlx.readObjectTempC()); 
    myFile.println("\t*C");
    myFile.println();

    // close the file:
    myFile.close();
    Serial.println("done.");
  } 
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } 
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 
}

void DS1337::snooze(unsigned long 900)  //seconds to snooze

void loop()
{
  // nothing happens after setup
}

What you are trying to say with:

...it seems codes are overlapping.

?

I think that you are using things that you don't must use. If you use this:

#include "RTClib.h"

I think you cant use:

#include <DS1337.h>

Even this 2 lines, I think they can't be used together:

DS1337 RTC = DS1337();
RTC_DS1307 RTC;

This:

#include <Adafruit_MLX90614.h>

I don't know why do you use it.

Try to use the code of the example give with the library DS1337.h.
The code can be found:

Again, thanks a lot Luis!
I think I'm having a problem "calling" the RTC. Because i'm using it to tag the reading, and then another library to set the alarm or sleeping code.
The thing is that the DS1337 example gives me an error right away:

DS1337:17: error: 'PIN_LED' was not declared in this scope
DS1337:18: error: 'PIN_INT2' was not declared in this scope

Is there any other library missing?

The reason why the next code is there:

Adafruit_MLX90614.h
is because I'm using a IR Adafruit Temperature Sensor

I believe my problem is in a little code line that overlaps or is calling a missing library, but I'm no sure!

Isn't the shield supposed to do all that? That's what the web page says and the review seems to confirm.

I think so, but there's no example in the web site about how to program the Arduino to make it sleep with that shield. That's why I'm looking for just the DS1337 RTC

My project:

OK. I understand why you use the Adafruit library. But you MUST use only one library for the RTC. If one have one set of functions that you need and other have other set, you must use the library closest what you want, the remain you must try to in first place try to code it in the sketch and then include it in the library that you chose.

Looking at Dead Bugs' example code now.
It seems to read sensors and put the Arduino to sleep.
When Arduino wakes up it runs the sketch, reads the sensors and goes to sleep as a virtual while loop.

The functions called are in ino files that need to be included or replaced with your own/

BTW, this could use a run through the IDE Autoformat tool for readability.

DLS_ShieldTest.ino

//-------------------------------------------------------------------------------
// This is a test sketch for the DLS 2.0 shield
// Dead Bug Protorypes
// 07.02.2014
//-------------------------------------------------------------------------------


#include <Wire.h>
#include <SD.h>
#include <OneWire.h>

#define DS1337_ADDRESS 0x68
#define ONE_WIRE_BUS 10
#define SD_CHIPSELECT 8

String strLogline = "";

void setup()
{
Serial.begin(9600);
delay(1000);
GetDate();
getTemp();
getLight();
getVolt();
WriteSD();
}

void loop()
{
while (true)
{
SetAlarm();
delay(500);
}

}

Photocell.ino

//-------------------------------------------------------------------------------
// This function will add the analog value of A0
// Dead Bug Protorypes
// 07.02.2014
//-------------------------------------------------------------------------------

void getLight(){
  int photocellReading;
  
  photocellReading = analogRead(0);
  
  strLogline += photocellReading;
  strLogline += ';';
  
}

All the parts are here, including the RTC code.

Hi everyone, I made it work!
The main problem was that I was not putting the libraries in the same folder of the .INO
I thought I just had to put it in arduino/libraries.

Right now the only problem I have is that the Arduino is turning off when it has to change the HOUR. Strange problem, but still..

What is the code right now? Because that is a bug that is hidden somewhere.

This is the code:

#include <Wire.h>
#include <SD.h>
#include <OneWire.h>
#define DS1337_ADDRESS      0x68
#define ONE_WIRE_BUS        10
#define SD_CHIPSELECT       8

String strLogline = "";

void setup()
{
	Serial.begin(9600);
	delay(1000);
	GetDate();
	WriteSD();

}

void SetAlarmMin(uint8_t AntallMin)
{
Wire.begin(DS1337_ADDRESS);
Wire.beginTransmission(DS1337_ADDRESS);
Wire.write((byte) 0);
Wire.endTransmission();
Wire.requestFrom(DS1337_ADDRESS, 2);
Wire.read(); // hopper over sekunder
uint8_t mm = bcd2bin(Wire.read());
mm = mm + AntallMin;

Wire.beginTransmission(DS1337_ADDRESS);
Wire.write((byte) 7);
Wire.write(0b00000000); // Alarm 1 sekunder
Wire.write(bin2bcd(mm)); // Alarm 1 minutter
Wire.write(0b10000000); // Alarm 1 timer (flagg satt)
Wire.write(0b10000000); // Alarm 1 dato (flagg satt)
Wire.write(0b00000000); // Alarm 2 minutter
Wire.write(0b00000000); // Alarm 2 timer (flagg satt)
Wire.write(0b00000000); // Alarm 2 dato (flagg satt)
Wire.write(0b00011101); // skru av ocillator og enable alarm 1
Wire.write((byte) 0); // Nullstill alt
Wire.endTransmission();
}


void loop()
{
	SetAlarmMin(10); // Sets the alarm 10 minutes for now
	}

So the next line I think is going to solve the problem:

if (mm > 59) mm = mm - 60;

But I'm testing it right now,, I let you know how it goes!