I wanted the ATTiny to go to sleep when no flow is present.
As programmed , ATTiny goes to sleep in 10 seconds and the current draw reduced to 2.o mA.
In awake mode and LED on- current draw is 24 mA.
But when the flow returns , the LED does not come on, and ATtiny does not wake up.
Accrding to the datasheet Attiny should wake up on the interrupt on INT0 and that’s where hall sesnsor interrupt is connevted.
Any suggestions?
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
#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;
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
sleep_disable();
}
void sleepNow()
{ sei();
sleep_cpu();
Calc=0;
count=0;
}
// The setup() method runs once, when the sketch starts
void setup() //
{
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(pin1, OUTPUT);
pinMode(pin2, 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, 0);
analogWrite(RED, 255);
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; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
//cli(); //Disable interrupts
Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/hour
if(Calc >0)
{
delay(500);
analogWrite(RED, 0);
analogWrite (GREEN, 255);
delay(500);
analogWrite(RED, 255);
analogWrite (GREEN, 0);
}
if (Calc==0)
{
count++;
if (Calc>0)
count = 0;
if (count > 10) {
delay(100);
digitalWrite(GREEN,HIGH);
digitalWrite(RED,HIGH);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
disable_adc();
disable_ac();
disable_brown_out_detector();
disable_watchdog();
sei();
sleepNow();
}
}
}