timer + sleepmodus

Hello,
just have a question. I didnt find an answer in other threads about sleep mode
I want to let start ardunio executing a sketch after i pressed a button. So after uploading arduino uno should go to sleep modus.
I write it in the following way.

void sleep() //
{
Serial.println("going to sleep");
delay(50);

set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();

attachInterrupt (1, wake, HIGH);
sleep_disable();
detachInterrupt(1);

}

[/quote]

and

void wake () //
{
sleep_disable();
detachInterrupt(1);
Serial.println("waking up");

}

[/quote]
it works, but arduino needs some time to leave the sleep modus.

But i have a function in my sketch. I use a timer for it.
Timer1.initialize(8000000); //
Timer1.attachInterrupt(function); //

void function()
{....}

so if try to use these lines of code in my sketch, the sketch doesnt start.
I saw here Arduino Playground - HomePage written that its not alowed to use the timer in the function

void wake () //

[/quote] and i dont use a timer in this function., but thesketch doesnt work.

I'm sure that the fine folks at http://snippets-r-us.com will be happy to look at your snippets.

Here is the sketch. If I use a timer, the sketch does not start(does nothing)

   #include <avr/sleep.h>
#include <TimerOne.h>
#include <LiquidCrystal_I2C.h>   // 
#include <Sensirion.h>       // 
#include <Wire.h>             
#include <DS1307RTC.h>
#include <Time.h>
#include <SD.h>

const int PWM_pin =  5;
const int Lightsensor = 2;
const int switch = 3;
const uint8_t dataPin  =  6; // 
const uint8_t clockPin =  7; // 
const int chipSelect   = 10;
static boolean Start = true;
byte pwm=200; 

float Temperature=0; //
float Humidity=0;  
float Taupunkt=0;   

volatile unsigned long x = 0; // 
unsigned long previousMillis = 0; 
volatile unsigned long y = 0;

Sensirion Temperatursensor = Sensirion(dataPin, clockPin);
LiquidCrystal_I2C lcd(0x20,16,2);

void setup(){
  Serial.begin(9600);
  lcd.init(); 
  lcd.backlight();
  
  pinMode(PWM_pin, OUTPUT);
  pinMode(Lightsensor, INPUT); // 
  pinMode(switch,INPUT);

  attachInterrupt(0, count, RISING);
  //attachInterrupt(1, wake, HIGH); 
 Timer1.initialize(8000000); // 
 Timer1.attachInterrupt(mesure); // 
  
}

void mesure() 
{ 
...
}
  
 void wake ()       // 
{
  sleep_disable();
  detachInterrupt(1);
  Serial.println("waking up");
  
}



void loop (){
  
  if (digitalRead(switch)==HIGH)  {    // 
    if (Start){  // 
      y=0;
      Serial.println("Start Motor.");  // 
      analogWrite(PWM_pin,pwm);
      previousMillis = millis();          //
      
      x = 0;                              // 
      Start = false;
    }
    if (millis()-previousMillis>1000){  //  
     
      
      previousMillis = millis();         // Startzeit setzen
     Serial.print("RPM= ");Serial.print(x*60);Serial.println("RPM");  // 
      if ( x*60<2520) 
         { analogWrite(5,pwm++);}
       if ( x*60>2550)
          { analogWrite(5, pwm--);}
        if ( x*60==0)
        { analogWrite( 5, 0);}
      
      y+=x;
      x = 0;                                // 
  
  }
    
    
  }else {                       // 
    Start = true;
   
   sleep();
  }  
} // end loop()

void count(){
  x++; 
             // 
             
}


void sleep()              // 
{
    Serial.println("going to sleep");
    delay(50);
    
  
    
    set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
    sleep_enable();
    
    attachInterrupt (1, wake, HIGH);
    sleep_disable();
    detachInterrupt(1);
    
}

Does anybody has an idia?

Can anybody say please, how can I avoid using a Timer if want some function works for example every 10 seconds?

Can anybody say please, how can I avoid using a Timer if want some function works for example every 10 seconds?

Your program already has an example of how to do it

    if (millis()-previousMillis>1000){  //  
     
      
      previousMillis = millis();         // Startzeit setzen

UKHeliBob:

Can anybody say please, how can I avoid using a Timer if want some function works for example every 10 seconds?

Your program already has an example of how to do it

    if (millis()-previousMillis>1000){  //  

previousMillis = millis();        //

But I cannot use this lines outside of loop ()? I mean in setup ()

But I cannot use this lines outside of loop ()? I mean in setup ()

OK. I'll bite. Why can't you?

Motorcontrol.ino: In function 'void setup()':
Motorcontrol:52: error: a function-definition is not allowed here before '{' token
Motorcontrol:191: error: expected `}' at end of input

             #include <avr/sleep.h>
#include <TimerOne.h>
#include <LiquidCrystal_I2C.h>   // 
#include <Sensirion.h>       // 
#include <Wire.h>             
#include <DS1307RTC.h>
#include <Time.h>
#include <SD.h>

const int PWM_pin =  5;
const int Lightsensor = 2;
const int switchpin = 3;
const uint8_t dataPin  =  6; // 
const uint8_t clockPin =  7; // 
const int chipSelect   = 10;
static boolean Start = true;
byte pwm=200; 

float Temperature=0; //
float Humidity=0;  
float Taupunkt=0;   

volatile unsigned long x = 0; // 
unsigned long previousMillis = 0; 
volatile unsigned long y = 0;

Sensirion Temperatursensor = Sensirion(dataPin, clockPin);
LiquidCrystal_I2C lcd(0x20,16,2);

void setup(){
  Serial.begin(9600);
  lcd.init(); 
  lcd.backlight();
  
  pinMode(PWM_pin, OUTPUT);
  pinMode(Lightsensor, INPUT); // 
  pinMode(switchpin,INPUT);

  attachInterrupt(0, count, RISING);
  //attachInterrupt(1, wake, HIGH); 
  if (millis()-previousMillis>8000){  //  
     
     mesure();
      
      previousMillis = millis();         //

}

void mesure()
{ 
 
 {
  Serial.begin (9600);
   
   Temperatursensor.measure(&Temperature, &Humidity, &Taupunkt);  // 
  
  
 
  Serial.print (x);
  lcd.print(x);
  File mesureFile = SD.open("mesured.txt", FILE_WRITE); // 

  if (mesureFile)
  {
    //Schreibe auf SD-Karte
    mesureFile.print("Temperature: ");  
    mesureFile.print(Temperature);      // 
    mesureFile.println("°C");
    

   mesureFile.print("Humidity: ");
    mesureFile.print(Humidity);
    mesureFile.println("%");
    mesureFile.println ("cycles");
    mesureFile.print   (x);

 

    mesureFile.print(day());
    mesureFile.print(":");
    mesureFile.print(month());
    mesureFile.print(":");
    mesureFile.print(year());
    mesureFile.print("; ");
    mesureFile.print(hour());
    mesureFile.print(":");
    mesureFile.print(minute());
    mesureFile.print(":");
    mesureFile.print(second());
    mesureFile.println("; ");

    mesureFile.close();
  }
  else
  {
    Serial.println("Erroo");
     digitalWrite(A0, HIGH);
     
  }
 
 
  
 }
}
  
  
 void wake ()       // 
{
  sleep_disable();
  detachInterrupt(1);
  Serial.println("waking up");
  
}



void loop (){
  
  if (digitalRead(switch)==HIGH)  {    // 
    if (Start){  // 
      y=0;
      Serial.println("Start Motor.");  // 
      analogWrite(PWM_pin,pwm);
      previousMillis = millis();          //
      
      x = 0;                              // 
      Start = false;
    }
    if (millis()-previousMillis>1000){  //  
     
      
      previousMillis = millis();         // Startzeit setzen
     Serial.print("RPM= ");Serial.print(x*60);Serial.println("RPM");  // 
      if ( x*60<2520) 
         { analogWrite(5,pwm++);}
       if ( x*60>2550)
          { analogWrite(5, pwm--);}
        if ( x*60==0)
        { analogWrite( 5, 0);}
      
      y+=x;
      x = 0;                                // 
  
  }
    
    
  }else {                       // 
    Start = true;
   
   sleep();
  }  
} // end loop()

void count(){
  x++; 
             // 
             
}


void sleep()              // 
{
    Serial.println("going to sleep");
    delay(50);
    
  
    
    set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
    sleep_enable();
    
    attachInterrupt (1, wake, HIGH);
    sleep_disable();
    detachInterrupt(1);
    
}

The error has nothing to do with the use of miilis() for timing.
Try Auto Format on you code for a clue as to the problem. It will probably be more obvious what the problem is if you put each opening and closing brace on its own line.

Can you tell me please what can be the problem if i use Timer. initialize from the first sketch (third post)?

Let's establish a baseline. Does this slightly modified example from the Timer1 library work ?

#include <TimerOne.h>

void setup() 
{
  pinMode(13, OUTPUT);     
  Timer1.initialize(8000000);
  Timer1.attachInterrupt( timerIsr ); // attach the service routine here
}

void loop()
{
}

void timerIsr()
{
  // Toggle LED
  digitalWrite( 13, digitalRead( 13 ) ^ 1 );

If so, then you know that the Timer1 library is working and we can go on from there.

Yes, the sketch works.
The question of the thread was, why does timer1 not work with the sleep mode? Are there some especialnesses?