Ideas? How to make arduino sleep & wake up...

Dear members,

thanks for the very helpful responses. I was able to make a lot of progress in my sketch, but there are a few things that I can’t figure out, and hence, was hoping to ask for some advice.

Below is the code that I have so far. I still can’t make it define and implement the time during which the data is collected (time X2), and also output the data as needed (see below for details).

Any and all thoughts & suggestions would be greatly appreciated…

/* This is the draft sketch for using with an outdoor battery-powered arduino that is meant to collect and record weather information (air temperature,
    humidity, date & time) and create a log file (a data logger is used that automatically records anything sent to the serial output). 

The outline of the sketch (which is still not complete...):
 1. arduino powers up & goes directly to sleep for X1 seconds
 2. after X1 seconds, wakes up for X2 seconds and powers up the various sensors/instruments
 3. It prints lines to serial output lines that contain the information from the various sensors (see example further below) for Y amount of times 
 4. After the X2 seconds pass by, the arduino shuts power off to all sensors/instruments
 5. Then, it goes to sleep for X1 seconds
 8. repeat starting at Step #2
 
 
 NOTE: This script is not yet fully functional. So... What is still missing? This sketch still needs to do the following:
 - define & implement X2 duration
- make the sketch do #3 & #4
 
 
        Many thanks to trendski for the valuable sleep & wake up sketch template

*/


/*===============================================================================*/
// Ok, let's start with some housekeeping...
// Load libraries & variables needed
#include <avr/sleep.h>      // Library for sleeping
#include <avr/wdt.h>        // Library for sleeping
#define ledPINgrn 12       // LED on when logging (yellow)
#define ledPINred 13       // LED on when sleeping (red)
#include <Wire.h>          // Library for time
#include "RTClib.h"        // Library for time
RTC_DS1307 RTC;            // Library for time
#include "DHT.h"            // Library for DHT22 (Temp & Humidity)
#define DHTPIN 2            // pin to which the data cable is connected to
#define DHTTYPE DHT22       // DHT22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);   // Library for DHT22 (Temp & Humidity)


/*===============================================================================*/
// Here we are going to define the power pins for our different instruments
// Power for datalogger:
int p1Pin = 6; // define power pin
float volt_pwr1 = 3.3; // volts for the data logger

// Power for DHT22
int p2Pin = 4;         // define pin (this uses 5v, so no need to use a PWM pin)

// Power for DS1307 RTC clock
//int p3Pin = 8;   // Why don't I get any serial output nor sleep/wakeup activity when I activate the power through this pin????!!!
                   
byte old_ADCSRA;
// watchdog interrupt 
ISR (WDT_vect) 
{
  wdt_disable();  // disable watchdog
}

/*===============================================================================*/
void setup()    // Here we will do the general SETUP (will run only once!!!!!!)
  {
      Serial.begin(9600);            // Set-up the serial output rate

              pinMode(p1Pin, OUTPUT);        // initialize the digital pin as an output.
              pinMode(p2Pin, OUTPUT);        // initialize the digital pin as an output.
      //      pinMode(p3Pin, OUTPUT);        // initialize the digital pin as an output.
      
      dht.begin();
      Wire.begin();                  // Calling time module
      RTC.begin();                   // Calling time module
     
      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__));
      }

  // This will set up the LEDs we will use to track if sleeping or awake
  pinMode(ledPINred, OUTPUT); 
  pinMode(ledPINgrn, OUTPUT); 
  digitalWrite (ledPINred, LOW);
  digitalWrite (ledPINgrn, LOW);
}



// Start of Datalogger methods**********
void LogData ()    // Here we will run the "LogData" command (and all the things within it) (will run only once!!!!!!)
  {
     
      digitalWrite (ledPINgrn, HIGH);    //Turn the LED on when logging (to show us what's going on)

      // Lets set-up the amount of voltage needed for the data logger    
      int v_num_pwr1 = volt_pwr1 * 51;   // This ranges from 0 (0 volts) to 255 (5 Volts); Example: 3.3v * 51 = 168.3
      analogWrite(p1Pin, v_num_pwr1);    // power up p1Pin
      
      // Lets set-up the amount of voltage needed for the DHT22    
      digitalWrite(p2Pin, HIGH);    // power up p2Pin
      delay(1000);                   // give the sensor a moment to "reset" from the powerup
            
      // Lets set-up 5v on for the clock
//      digitalWrite(p3Pin, HIGH);

      // Let's start printing some stuff to the serial output...  
      Serial.println("Now, time to start printing data...");
      
    
/* ****** Starting at this point, how can I do to send the following "print to serial output" code 
                so that it prints "time, humidity & temp" for Y amount of times while the arduino is awake,
                then power things off and go to sleep??? :-(
*/


// start some kind of loop here that runs for XXX times?

     
    // Start printing the info we need
    // Print the date...    
        DateTime now = RTC.now();
     

        Serial.print(now.month(), DEC);
        Serial.print('/');
        Serial.print(now.day(), DEC);
        Serial.print('/');       
        Serial.print(now.year(), DEC);
        Serial.print(' ');
        Serial.print(now.hour(), DEC);
        Serial.print(':');
        Serial.print(now.minute(), DEC);
        Serial.print(':');
        Serial.print(now.second(), DEC);
        Serial.print(" %\t");
     
              
    // Start printing Humidity & Temperature...     
     float h = dht.readHumidity();
     float t = dht.readTemperature();

    // check if returns are valid, if they are NaN (not a number) then something went wrong!
    if (isnan(t) || isnan(h)) {
      Serial.println("Failed to read from DHT");
    } else {
      Serial.print("Humidity: "); 
      Serial.print(h);
      Serial.print(" %\t");
      Serial.print("DHT Temp C: "); 
      Serial.print(t);
      Serial.println();
    }
 


/*   Basically, what I would like to achieve is to print this::
4/24/2012 18:59:2 %	Humidity: 51.20 %	DHT Temp C: 25.60
4/24/2012 18:59:4 %	Humidity: 51.20 %	DHT Temp C: 25.60
4/24/2012 18:59:6 %	Humidity: 51.20 %	DHT Temp C: 25.60
4/24/2012 18:59:8 %	Humidity: 51.20 %	DHT Temp C: 25.60
4/24/2012 18:59:10 %	Humidity: 51.20 %	DHT Temp C: 25.60
4/24/2012 18:59:12 %	Humidity: 51.20 %	DHT Temp C: 25.60
4/24/2012 18:59:14 %	Humidity: 51.20 %	DHT Temp C: 25.60
4/24/2012 18:59:16 %	Humidity: 51.20 %	DHT Temp C: 25.60
  etc. etc.. (this keeps printing Y amount of times and finishes before time X2, so that everything can get logged well and not shut
  down abruptly)
  
  */

// end of a loop here??


  
      
      delay(5000); // Keep the LED and text showing for 5 sec
  }
// End of Datalogger methods*******



// Sleeping routines...
void loop () 
{   
  old_ADCSRA = ADCSRA; //save old status of ADC
  
  //=============SLEEP ROUTINES=============
  for (int myLoop= 0; myLoop < 1; myLoop++) // SLEEP-LOOP loop around and give delay of 1 cycle = 8 sec  (1 cycle = 8 sec)

  {
   
    Serial.println("Going to sleep now...");
    digitalWrite (ledPINred, HIGH); //led on when sleeping

  
    // disable ADC
    ADCSRA = 0;  

    // clear various "reset" flags
    MCUSR = 0;     
    // allow changes, disable reset
    WDTCSR = _BV (WDCE) | _BV (WDE);

    // set interrupt mode and an interval 
    WDTCSR = _BV (WDIE) | _BV (WDP3) | _BV (WDP0);    // set WDIE, and 8 seconds delay

    wdt_reset();  // pat the dog

    set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
    sleep_enable();

    // turn off brown-out enable in software
    MCUCR = _BV (BODS) | _BV (BODSE);
    MCUCR = _BV (BODS); 
    
    sleep_cpu ();  //nighty-night :)
  }

  // cancel sleep as a precaution
  sleep_disable(); 


 digitalWrite (ledPINred, LOW); //red led off when awake & logging


  //++++++++++++++++++++WAKE and CALL DATALOGGER+++++++
  ADCSRA = old_ADCSRA; //enable ADC need to enable so ADC can get sensor data


 LogData (); // now awake so as to power the sensors up, start logging data, and then go to sleep



 digitalWrite (ledPINgrn, LOW);    //indicate finished logging 
 analogWrite(p1Pin, LOW);          // power down p1Pin
 digitalWrite(p2Pin, LOW);         // power down p2Pin
// digitalWrite(p3Pin, LOW);       // power down p2Pin

  
} // end