I made project using atmel-328p. I want to run it over a year. I am using power_down mode & 5500 mAh li-ion battery still battery discharges in 1 day. I am using standard code as explain on arduino site. code is:
String uniqueid;
char value;
volatile int NbTopsFan; //measuring the rising edges of the signal
uint32_t Calc;
int hallsensor = 2; //The pin location of the sensor
int power = 4;
// ph sensor
define SensorPin 0 //pH meter Analog output to Arduino Analog Input 0
define Offset 0.00 //deviation compensate
unsigned long int avgValue; //Store the average value of the sensor feedback
// temp sensor
OneWire ds(8);
byte i;
byte present = 0;
byte data[12];
byte addr[8];
int HighByte, LowByte, SignBit, Whole, Fract, TReading, Tc_100, FWhole;
int wakePin = 3; // pin used for waking up
int sleepStatus = 0; // variable to store a request for sleep
int countsleep = 0; // counter
void wakeUpNow() // here the interrupt is handled after wakeup
{// execute code here after wake-up before returning to the loop() function
}
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 setup()
{
pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
Serial.begin(9600); //This is the setup function where the serial port is
pinMode(power, OUTPUT);
//initialised,
attachInterrupt(0, rpm, RISING); //and the interrupt is attached
//initialised,
//and the interrupt is attached
// temp sensor pin 8
if ( !ds.search(addr))
{
// Serial.println("temp sensor is not connected");
delay(1000);
ds.reset_search();
return;
}
if ( OneWire::crc8( addr, 7) != addr[7])
{
// Serial.println("CRC not valid!");
delay(1000);
return;
}
pinMode(wakePin, INPUT);
attachInterrupt(1, wakeUpNow, LOW); // use interrupt 1 (pin 3) and run function
}
void sleepNow() // here we put the arduino to sleep
{
countsleep = 0;
ADCSRA = 0;
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
// turn off brown-out enable in software
MCUCR = bit (BODS) | bit (BODSE); // turn on brown-out enable select
MCUCR = bit (BODS); // this must be done within 4 clock cycles of above
sleep_cpu ();
attachInterrupt(1,wakeUpNow, LOW); // 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(1); // disables interrupt 0 on pin 2 so the
// wakeUpNow code will not be executed
}
void loop()
{
countsleep++;
delay(1000); // waits for a second
// compute the serial input
if (Serial.available())
{
String content = "";
char character;
// to read serial data coming
while(Serial.available()>0)
{
character = Serial.read();
content.concat(character);
}
String uniqueidread = content.substring(0,16);
String parameter = content.substring(16,17);
String checkuuid = content.substring(0,3);
String putinsleep = content.substring(0,5);
if (uniqueidread == uniqueid && parameter == "5")
{ // Serial.flush();
digitalWrite(power,HIGH);
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
digitalWrite(power,LOW);
getTemp();
delay(1000);
getTemp();
// printTemp();
Serial.print("Temp is: ");
if (SignBit)
{
Serial.print(" - ");
}
if( Whole == 0)
{
Serial.print("temp sensor is not connected");
}
else
{
Serial.print(Whole);
Serial.print(" C / ");
Serial.print(FWhole);
Serial.print(" F");
}
Serial.print(", Current Reading = ");
Serial.print (Calc, DEC); //Prints the number calculated above
Serial.print(" L/hour");
phvalue();
Serial.println("S=m");
delay(100);
sleepNow();
}
if ( content == "sleep")
{
Serial.println("S=m");
delay(100); // this delay is needed, the sleep
sleepNow(); // sleep function called here
}
}
if (countsleep >= 10)
{
Serial.println("T=m");
delay(100); // this delay is needed, the sleep
//function will provoke a Serial error otherwise!!
sleepNow(); // sleep function called here
}
}
void getTemp()
{
int foo, bar;
ds.reset();
ds.select(addr);
ds.write(0x44,1);
present = ds.reset();
ds.select(addr);
ds.write(0xBE);
for ( i = 0; i < 9; i++) {
data[i] = ds.read();
}
LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) {
TReading = -TReading;
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
Whole = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;
if (Fract > 49) {
if (SignBit) {
--Whole;
} else {
++Whole;
}
}
if (SignBit) {
bar = -1;
} else {
bar = 1;
}
foo = ((Whole * bar) * 18); // celsius to fahrenheit conversion section
FWhole = (((Whole * bar) * 18) / 10) + 32;
if ((foo % 10) > 4) { // round up if needed
++FWhole;
}
}
void printTemp(void)
{
Serial.println("");
Serial.print("Temp is: ");
if (SignBit)
{
Serial.print(" - ");
}
Serial.print(Whole);
Serial.print(" C / ");
Serial.print(FWhole);
Serial.print(" F");
}
void phvalue()
{
int buf[10]; //buffer for read analog
for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
{
buf[i]=analogRead(SensorPin);
delay(10);
}
for(int i=0;i<9;i++) //sort the analog from small to large
{
for(int j=i+1;j<10;j++)
{
if(buf[i]>buf[j])
{
int temp=buf[i];
buf[i]=buf[j];
buf[j]=temp;
}
}
}
avgValue=0;
for(int i=2;i<8;i++) //take the average value of 6 center sample
avgValue+=buf[i];
float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
phValue=3.5*phValue+Offset; //convert the millivolt into pH value
Serial.print(" pH:");
Serial.print(phValue,2);
Serial.print(" ");
}