Timer0 and sleep mode with attiny85

i am trying to use timer0 and sleep mode with attiny85,i mean,i put the microcontroller to sleep and if i press the button for a time it performs an action,if the button is pressed after another time it performs another action,so i set the timer0 to take the time but when i compile i get an error.


#include<avr/io.h>
#include <avr/interrupt.h>
#include<util/delay.h>
#include <avr/sleep.h>
#define F_CPU 16500000UL

  


unsigned int contador=0;
unsigned long int contador1=0;
int Led = 4;      // LED to show the action of a interrupt
int boton = 2;            // active LOW, ground this pin momentary to wake up
int Motor =0;
byte stateButton;

unsigned int contm1=0;
unsigned int contm2=0;



unsigned int contador2=0;

unsigned long Temp2;


ISR (TIMER0_OVF_vect)      //Interrupt vector for Timer0
{
  
}


void timer_setup()
{
  
  TCCR0A=0x00;   //Normal mode
  TCCR0B=0x00;
  TCCR0B |= (1<<CS00)|(1<<CS02);   //prescaling with 1024
  sei(); //enabling global interrupt
  TCNT0=0;
  TIMSK|=(1<<TOIE0); //enabling timer0 interrupt
  
}


void setup()
{

  timer_setup();
 // pinMode(ledPin, OUTPUT);         // sets the digital pin as output
  pinMode(Led, OUTPUT);   // sets the digital pin as output
  pinMode(Motor, OUTPUT);
  pinMode(boton, INPUT);        // sets the digital pin as input
  //digitalWrite(wakePin, HIGH);
}


void sleepNow(){         
 
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here
    sleep_enable();                          // enables the sleep bit in the mcucr register so sleep is possible
    attachInterrupt(0, wakeUpNow, LOW);     // use interrupt 0 (pin 2) and run function wakeUpNow when pin 2 gets LOW
  //  digitalWrite(ledPin, LOW); 
    
    sleep_mode();                          // here the device is actually put to sleep!!
    
    sleep_disable();                       // first thing after waking from sleep: disable sleep...
    detachInterrupt(0);                    // disables interrupton pin 3 so the wakeUpNow code will not be executed during normal running time.
    //attachInterrupt(digitalPinToInterrupt(0),despertar, LOW);
   
   // delay(250);                           // wait 2 sec. so humans can notice the interrupt LED to show the interrupt is handled
    //digitalWrite (interruptPin, LOW);       // turn off the interrupt LED

}



void wakeUpNow()
{        // here the interrupt is handled after wakeup

  //execute code here after wake-up before returning to the loop() function
  // timers and code using timers (serial.print and more...) will not work here.
 //digitalWrite(interruptPin, HIGH);
}



void loop(){

    /*--------- codigo para leer botones ---------------*/

  //digitalWrite(Motor,LOW);  

  if((!digitalRead(boton))&(!(stateButton)))
  {
    contador1=0;
    contm1=millis();
    stateButton=1;
  }
  else if ((digitalRead(boton))&(stateButton))
  {
    stateButton=0;
    contm2=(millis()-contm1);
    
  Temp2=contador1;

   //if((200<Temp2)&(Temp2<700))
  if((10<contm2)&(contm2<500))
  {
   
    contador=1;
    contador1=0;
    contm1=millis();
      
  }
  //else if((700<=contador1)&(contador1<=1500))
  else if ((500<=contm2)&(contm2<=1000))
  {
   
    contador2++;
    contador1=0;
    contm1=millis();
    if(contador2>2)
    {
      contador2=0;
    }
   
  }
     
  }

  if((digitalRead(boton))&(contador==1))
  {
   //Serial.println("encendiendo led por 5 seg");
   //Serial.println("contador es:");
    //Serial.println(contador);
    digitalWrite(Led,HIGH);
   // digitalWrite(Motor,HIGH);
    delay(5000);
    digitalWrite(Led,LOW);
   // digitalWrite(Motor,LOW);
    contador=0;
    contador2=0;
    
  }
  if((digitalRead(boton))&(contador2==2))
  {
    //Serial.println("encendiendo led por 10 seg");
    //Serial.println("contador2 es:");
    //Serial.println(contador2);

    digitalWrite(Led,HIGH);
  //  digitalWrite(Motor,HIGH);
    delay(10000);
    digitalWrite(Led,LOW);
   // digitalWrite(Motor,LOW);
    contador=0;
    contador2=0;
    
  }

   contm2=(millis()-contm1);
  
  //if((!digitalRead(InterruptPin))&(1500<contador1))
  if((!digitalRead(boton))&(1000<contm2))
  {
    digitalWrite(Led,HIGH);
   // digitalWrite(Motor,HIGH);
    contador=0;
   
  }
  if(((digitalRead(boton)))&(contador!=1)&(contador!=2))
  {
    digitalWrite(Led,LOW);
   // digitalWrite(Motor,LOW);
    contador1=0;    
    
  }





    /*--------------------------------------------------*/
  

  

  
  sleepNow();                      // sleep function called here
  
}

**the error is:**

wiring.c.o (symbol from plugin): In function __vector_5': (.text+0x0): multiple definition of __vector_5'
C:\Users\jesus\AppData\Local\Temp\arduino_build_382338\sketch\otro.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
exit status 1
Erro compilando para a placa ATtiny25/45/85





The arduino core already uses TIMER0 overflow to keep track of mills() so that is why you are getting the error. It is best to use another timer.

Also, in C/C++ the logical 'and' operator is && which is different than the bit-wise and operator &
You use it throughout your code in your if() statements.

What is your intent for Timer0? As far as I know, it does not work during power down sleep mode.

The wakeup operations are external or pin change interrupts and the watchdog timer.

this is rigth, i am using &, wrong, i am using && now. thanks, i not use timer0, i resolved with delay() and use a counter variable, this is funcional.

i resolved now with delay() and counter variabel, because is most easy to press a button and read the time when this is low or high.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.