ATtiny84 sleep issues

Hello, I am having some trouble with putting the ATtiny84 to sleep and waking it with an interrupt. Everything is working fine on the UNO so Im guessing the sleep library needs some modification to work with the ATtiny. I'm really just too much of a beginner to figure all this out.

For background info, I'm building a secret locking wood box with a servo latch mechanism , 3 pots, a rgb LED and a nice metal button on the lid. The pots are the code input, the button wakes the attiny from sleeping checks the state of the pots, then either indicates a correct code with a green light and triggers the servo, if wrong, a red light and and gives you 20 seconds to put in the right code before going back to sleep. If in those 20 seconds, you touch the button using capacitive touch sensing for 5 seconds, a new code will be set to whatever the pots values are and it will flash blue, become the right code and unlock. Here is my code so far, let me know if you spot some rookie mistakes.

I also need some pointer on more ways to make for more lower power consumption.

#include <Servo.h>
#include <CapacitiveSensor.h>
#include <avr/sleep.h>

int redPin = 6;
int greenPin = 7;
int bluePin = 8;
int buttonPin = 10;

int pot1 = A0;
int pot2 = A1;
int pot3 = A2;

int code1 = 1;
int code2 = 1;
int code3 = 1;

boolean state = LOW;

int val1;
int val2;
int val3;

int button;

int counter = 0;
int startCounter = 0;

Servo myservo;
CapacitiveSensor  cs_10_11 = CapacitiveSensor(4,5); 

unsigned long currentmillis = 0;
unsigned long switchmillis = 0;
unsigned long buttonmillis = 0;
unsigned long last_buttonmillis = 0;
unsigned long startmillis = 0;

void setup()
{
  
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(pot1, INPUT);
  pinMode(pot2, INPUT);
  pinMode(pot3, INPUT);
  
  cs_10_11.set_CS_AutocaL_Millis(20000);
  
  attachInterrupt(0, wake, RISING);
  
  myservo.attach(3);
  myservo.write(96);
  
  flashG();
}
void loop()
{
  currentmillis = millis();
  
  button = digitalRead(buttonPin);
 
  
  getPots();
  
//  Serial.println(val1);
//  Serial.println(val2);
//  Serial.println(val3);
  
  if(val1 == code1 && val2 == code2 && val3 == code3)
    {
      switchmillis = currentmillis;
      green();
      unlock();
      off();
      delay(1000);
      sleepNow();
    } 
    else{ 
      myservo.write(96);    //no explanation for why this is needed.
      red();
      
    }
  
  //////////////////////////////////////////////////////////////////
  
  long total1 =  cs_10_11.capacitiveSensor(30);
  
 
  if(total1 > 200)
  {
    if(currentmillis - switchmillis >= 5000)
    {
      counter++;
      switchmillis = currentmillis;
    }  
  }
  if(total1 <= 200)
  {
    switchmillis = currentmillis;
  }
  
  //////////////////////////////////////////////////////////////////
  
  //Serial.println(counter);
  if(counter == 1)
  {
    counter = 0;
    flashB();
    getPots();
    storeCode();
  }
  
  //////////////////////////////////////////////////////////////////
  
  //Serial.println(startCounter);
  if(button == 0)
  { 
    if(currentmillis - startmillis > 20000)
    {
      startCounter++;
      startmillis = currentmillis;
    }
  } 
  if(button == 1)
  {
    startmillis = currentmillis;
  }
  if(startCounter == 1)
  {
    off();
    startCounter = 0;
    sleepNow();
  }
}

void wake()
{

}

void sleepNow()         // here we put the arduino to sleep
{
    
  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. just a safety pin
 
  attachInterrupt(0, wake, RISING); // use interrupt 0 (pin 2) and run function
                                     // wakeUpNow when pin 2 gets LOW
 
  sleep_mode();            // here the device is actually put to sleep!!
                           // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
 
  sleep_disable();         // first thing after waking from sleep:
                           // disable sleep...
  detachInterrupt(0);      // disables interrupt 0 on pin 2 so the
                             // wakeUpNow code will not be executed
                             // during normal running time.
}
 
void flashG()
{
  off();
  for(int i = 0; i < 3; i++)
  {
    state = !state;
    digitalWrite(greenPin, state);
    delay(100);
  }
  digitalWrite(greenPin, LOW);
}

void flashB()
{
  digitalWrite(bluePin, HIGH);
  delay(200);
  digitalWrite(bluePin, LOW);
  delay(100);
  digitalWrite(bluePin, HIGH);
  delay(200);
  digitalWrite(bluePin, LOW);
  delay(100);
  digitalWrite(bluePin, HIGH);
  delay(200);
  digitalWrite(bluePin, LOW);  
}

int adjust(int in)
{
  if(in > 1010){
    return 1;
  }
  else if(in > 950){
    return 2;
  }
  else if(in > 900){
    return 3;
  }
  else if(in > 630){
    return 4;
  }
  else if(in > 480){
    return 5;
  }
  else if(in > 190){
    return 6;
  }
  else if(in > 80){
    return 7;
  }
  else if(in > 30){
    return 8;
  }
  else if(in > 3){
    return 9;
  }
}
void getPots()
{
  val1 = analogRead(pot1);
  val2 = analogRead(pot2);
  val3 = analogRead(pot3);
  adjustPots();
}
void adjustPots()
{
  val1 = adjust(val1);
  val2 = adjust(val2);
  val3 = adjust(val3);
}
void storeCode()
{
  code1 = val1;
  code2 = val2;
  code3 = val3;
}
void red()
{
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, LOW);
}
void green()
{
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, HIGH);
  digitalWrite(bluePin, LOW);
}
void blue()
{
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, HIGH);
}
void off()
{
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, LOW);
}
void unlock()
{
  myservo.write(75);
  delay(750);
  myservo.write(96);
  delay(250);
}

I am stuck. I need to get an ATtiny84 to wake up from sleep mode with an interrupt(button push) Ive got everything working on the uno's 328 but no luck on the ATtiny84. Please help!

Do not cross-post. Threads merged.

mind helping anyone?

Not a single person on this forum has any experience with the ATtiny84? I cant believe how insanely difficult it is to get some beginner information on this.

Why are there different pinout's that don't agree with my sketch.
Why does sleep function work on the UNO (only when using USB power) wall worts or my power supply cause it to never go to sleep.
Why wont the interrupts work on the ATtiny? Why are the PCINT pins always different from different resources?

What Core are you using? I've written sketches for the ATtiny84 using this Core:

http://code.google.com/p/arduino-tiny/

Different Cores have different pinouts.

thanks for getting back to me. Im not too sure what I used, Ill try that one out. Thanks, you had no problems putting it to sleep with the avr/sleep library?

rclymer:
Im not too sure what I used, Ill try that one out. Thanks, you had no problems putting it to sleep with the avr/sleep library?

I've only slept an ATmega328. This evening if I get a chance (and if you had no luck with the Core I linked to) I will try to sleep my ATtiny84 and wake it from an interrupt.

EDIT:

Why are you attaching a RISING interrupt? Don't you want to use the internal pullup and wake on FALLING?

Figured it out!

The secret is in the datasheet. Page 49:

9.2 External Interrupts
Note that recognition of falling or rising edge interrupts on INT0 requires the presence of an
I/O clock, as described in “Clock Sources” on page 25.

When your ATtiny84 is in SLEEP_MODE_PWR_DOWN, the clock is stopped, so a rising or falling edge cannot be detected... d'oh. :slight_smile:

Fortunately, you can also wake on LOW, even without a clock.

This works:

// 
// http://code.google.com/p/arduino-tiny/ pinouts
//
//                  ATtiny24/44/84
//                      +-\/-+
//                VCC  1|    |14  GND
//                  0  2|    |13  10/A0
//                  1  3|    |12  9/A1
//              RESET  4|    |11  8/A2
//      INT0        2  5|    |10  7/A3
//               3/A7  6|    |9   6/A4     SCK
//      MOSI     4/A6  7|    |8   5/A5     MISO
//                      +----+

#include <avr/sleep.h>

// ====================================================
void system_sleep()
{
sleep_enable(); // get ready to sleep
sleep_mode(); // actually go to  sleep
// sleeping ... 
sleep_disable(); // wake up fully
}

// ====================================================
void wake()
{
}

// ====================================================
void setup()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); 
pinMode(2, INPUT_PULLUP);
attachInterrupt(0, wake, LOW);
system_sleep();
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);
}

// ====================================================
void loop()
{
}

For power saving you could turn off the ADC:

Why are you attaching a RISING interrupt? Don't you want to use the internal pullup and wake on FALLING?

Very good point, I must have missed that. Although it still works like it should, hmm . I gave up on the ATtiny for this project but Im very thankful to have some answers.

For power saving you could turn off the ADC:

Yes, I have already updated the sketch after reading your guide. Thanks a ton for writing all of them, they have been one of the main sources for my self-education.