The below code uses a hall sensor to calculate flow using interrupt on INT0.
While the flow is not present I want the Attiny to go to sleep. I am using the power down mode . While the attiny is awake the current usage is 24 mA with LED on , where as when Attiny goes to sleep the current drops to 3.5mA. But after attiny goes to sleep and the flow comes back, its not waking up and the led does not come on. Any idea why attiny is not waking up?
Thanks
#define GREEN PB0
#define RED PB1
#define hallsensor PB2
#define pin1 PB5
#define pin2 PB3
#define pin3 PB4
uint8_t mcucr1, mcucr2;
#ifndef cbi
#define cbi(sfr,bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr,bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
//#define BODS 7 //BOD Sleep bit in MCUCR
//#define BODSE 2 //BOD Sleep enable bit in MCUCR
#include <avr/power.h>
#include <avr/sleep.h>
#define BODS 7 //BOD Sleep bit in MCUCR
#define BODSE 2 //BOD Sleep enable bit in MCUCR
volatile int NbTopsFan; //measuring the rising edges of the signal
float Calc;
int count = 0;
float t = 0;
float greenVal = 0;
//float blueVal = 255;
void rpm () //This is the function that the interupt calls
{
NbTopsFan++; //This function measures the rising and falling edge of the hall effect sensors signal
}
void sleepNow()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
ACSR |= _BV(ACD); //disable the analog comparator
ADCSRA &= ~_BV(ADEN); //disable ADC
power_timer0_disable();
power_timer1_disable();
mcucr1 = MCUCR | _BV(BODS) | _BV(BODSE); //turn off the brown-out detector
mcucr2 = mcucr1 & ~_BV(BODSE);
MCUCR = mcucr1;
MCUCR = mcucr2;
sei();
sleep_enable();
power_timer0_enable();
power_timer1_enable();
//ensure interrupts enabled so we can wake up again
GIMSK |= _BV(INT0);
sleep_disable();
power_all_enable();
}
// The setup() method runs once, when the sketch starts
void setup() //
{
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
//pinMode(BLUE, OUTPUT);
digitalWrite(GREEN, HIGH);
digitalWrite(RED, HIGH);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
attachInterrupt(0, rpm, RISING); //and the interrupt is attached
analogWrite(GREEN, (int)greenVal);
disable_adc();
disable_ac();
disable_brown_out_detector();
disable_watchdog();
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void disable_brown_out_detector() {
mcucr1 = MCUCR | _BV(BODS) | _BV(BODSE);
mcucr2 = mcucr1 & ~_BV(BODSE);
MCUCR = mcucr1;
MCUCR = mcucr2;
}
void disable_adc() {
cbi(ADCSRA,ADEN);
}
void disable_ac() {
sbi(ACSR,ACD);
}
void disable_watchdog() {
cbi(WDTCR,WDIE);
}
void loop ()
{
NbTopsFan = 0;
sei();
delay (1000);
Calc = (NbTopsFan * 60 / 7.5);
if((Calc >0) )
{
analogWrite (GREEN, 255);
delay(500);
analogWrite(GREEN, 0);
}
if (Calc==0)
{
count++;
// Serial.print(count, DEC);
// Serial.print("\n");}
if (Calc>0)
count = 0;
if (count > 10) {
delay(100);
digitalWrite(GREEN,HIGH);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
disable_adc();
disable_ac();
disable_brown_out_detector();
disable_watchdog();
sei();
sleepNow();
}
}
}