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);
}