Atmega 328p uses more mA sleeping than awake..!

Hi,

Been dooing my first real arduino project and I reached a standstill: To save power, I implemented a sleep mode, but I’m using 14 to 17 mA awake and up to 50 mA in sleep mode!! I’ve tried every thing I can think of, but can’t figure why this is happening. All I know is that this sleep mode code saves power in a test sketch with nothing else, but when I implement it on my sketch, it totally fails and uses up even more power.

Please, help!

Here’s my code:
(The Sleep Mode code is in bold)

#include <avr/sleep.h> 
#include <avr/wdt.h> 
#include <MenuBackend.h>    
#include <LiquidCrystal.h>  
#include <Wire.h>
#include <EEPROM.h>
#include "RTClib.h"

#define lenght 16.0
double percent=100.0;
unsigned char b;
unsigned int peace;
byte p1[8] = {
  0x10,
  0x10,
  0x10,
  0x10,
  0x10,
  0x10,
  0x10,
  0x10};
 
byte p2[8] = {
  0x18,
  0x18,
  0x18,
  0x18,
  0x18,
  0x18,
  0x18,
  0x18};
 
byte p3[8] = {
  0x1C,
  0x1C,
  0x1C,
  0x1C,
  0x1C,
  0x1C,
  0x1C,
  0x1C};
 
byte p4[8] = {
  0x1E,
  0x1E,
  0x1E,
  0x1E,
  0x1E,
  0x1E,
  0x1E,
  0x1E};
 
byte p5[8] = {
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F};


//****************************************************************

[b]void wake ()
{

  sleep_disable();
}  [/b]

RTC_DS1307 RTC;

int address = 0;
int longitude = 0;
int const5 = 5;
byte datahora;
byte value;
byte value1;
byte value2;
int myClock = 0;
int valor = 0;
int morada = 0;
int boLean = 0;
int powerCounter;
int powerDay;
int powerHour;
int powerTime;
int pwDay;
int pwHour;
int pwMinute;
int ANO = -1;
int MES = 0;
int DIA = 0;
int HORA = -1;
int MINUTO = -1;
int temp = 0;
int sensorMinute;
byte addressMinute;


float vout = 0.0;

const int motor1Pin = 0;    
const int motor2Pin = 1;    
const int enablePin = 12;
const int sensorPin = 2;     

int sensorState = 0;         

const int buttonPinLeft = 9;      // pin for the Up button
const int buttonPinRight = 11;    // pin for the Down button
const int buttonPinEsc = 10;     // pin for the Esc button
const int buttonPinEnter = 3;   // pin for the Enter button

int lastButtonPushed = 0;

int lastButtonEnterState = LOW;   
int lastButtonEscState = LOW;   
int lastButtonLeftState = LOW;   
int lastButtonRightState = LOW;   

long lastEnterDebounceTime = 0;  
long lastEscDebounceTime = 0;  
long lastLeftDebounceTime = 0; 
long lastRightDebounceTime = 0; 
long debounceDelay = 100;

LiquidCrystal lcd(7, 6, 5, 4, 8, 13);

//Menu variables
MenuBackend menu = MenuBackend(menuUsed,menuChanged);
//initialize menuitems
    MenuItem menuItem1 = MenuItem("Item1");
      MenuItem menuItem1SubItem1 = MenuItem("Item1SubItem1");
        MenuItem menuItem1SubItem1GO = MenuItem("Item1SubItem1GO");
      MenuItem menuItem1SubItem2 = MenuItem("Item1SubItem2");
        MenuItem menuItem1SubItem2GO = MenuItem("Item1SubItem2GO");
    MenuItem menuItem2 = MenuItem("Item2");
      MenuItem menuItem2SubItem1 = MenuItem("Item2SubItem1");
        MenuItem menuItem2SubItem1GO = MenuItem("Item2SubItem1GO");
      MenuItem menuItem2SubItem2 = MenuItem("Item2SubItem2");
        MenuItem menuItem2SubItem2HORA = MenuItem("Item2SubItem2HORA");
        MenuItem menuItem2SubItem2MINUTO = MenuItem("Item2SubItem2MINUTO");
        MenuItem menuItem2SubItem2EXIT = MenuItem("Item2SubItem2EXIT");
      MenuItem menuItem2SubItem3 = MenuItem("Item2SubItem3");
        MenuItem menuItem2SubItem3GO = MenuItem("Item2SubItem3GO");
      MenuItem menuItem2SubItem4 = MenuItem("Item2SubItem4");
        MenuItem menuItem2SubItem4GO = MenuItem("Item2SubItem4GO");
          MenuItem menuItem2SubItem4EXIT = MenuItem("Item2SubItem4EXIT");
    MenuItem menuItem3 = MenuItem("Item3");
      MenuItem menuItemDIA = MenuItem("ItemDIA");
      MenuItem menuItemMES = MenuItem("ItemMES");
      MenuItem menuItemANO = MenuItem("ItemANO");
      MenuItem menuItemHORA = MenuItem("ItemHORA");
      MenuItem menuItemMINUTO = MenuItem("ItemMINUTO");
      MenuItem menuItemEXIT = MenuItem("ItemEXIT");
    MenuItem menuItem4 = MenuItem("Item4");
      MenuItem menuItem4GO = MenuItem("Item4GO");

//****************************************************************
[b]ISR (WDT_vect) 
{
   wdt_disable();  
}  [/b]

(code tags added by moderator)

Rest of the code:

void setup(){ 
  
  lcd.createChar(0, p1);
  lcd.createChar(1, p2);
  lcd.createChar(2, p3);
  lcd.createChar(3, p4);
  lcd.createChar(4, p5);
  
  Wire.begin();
  RTC.begin();

  DateTime now = RTC.now();
  powerCounter = now.minute();

  pinMode(motor1Pin, OUTPUT); 
  pinMode(motor2Pin, OUTPUT); 
  pinMode(enablePin, OUTPUT);
  pinMode(sensorPin, INPUT);
  
  pinMode(A0, OUTPUT);
  pinMode(A1, INPUT);
  pinMode(A2, OUTPUT); 
  pinMode(A3, OUTPUT); 

  pinMode(buttonPinLeft, INPUT);
  pinMode(buttonPinRight, INPUT);
  pinMode(buttonPinEnter, INPUT);
  pinMode(buttonPinEsc, INPUT);
  
  digitalWrite(A0, HIGH);
  digitalWrite(A2, HIGH);

  lcd.begin(16, 2);
 
  menu.getRoot().add(menuItem1);
  menuItem1.addRight(menuItem2).addRight(menuItem3).addRight(menuItem4);
  menuItem1.add(menuItem1SubItem1).addRight(menuItem1SubItem2);
  menuItem1SubItem1.add(menuItem1SubItem1GO);
  menuItem1SubItem1GO.addRight(menuItem1SubItem1GO);
  menuItem1SubItem2.add(menuItem1SubItem2GO);
  menuItem2.add(menuItem2SubItem1).addRight(menuItem2SubItem2).addRight(menuItem2SubItem4).addRight(menuItem2SubItem3);
  menuItem2SubItem1.add(menuItem2SubItem1GO);
  menuItem2SubItem2.add(menuItem2SubItem2HORA).add(menuItem2SubItem2MINUTO).add(menuItem2SubItem2EXIT);
  menuItem2SubItem2HORA.addRight(menuItem2SubItem2HORA);
  menuItem2SubItem2MINUTO.addRight(menuItem2SubItem2MINUTO);
  menuItem2SubItem4.add(menuItem2SubItem4GO);
  menuItem2SubItem4GO.addRight(menuItem2SubItem4GO);
  menuItem2SubItem4GO.add(menuItem2SubItem4EXIT);
  menuItem2SubItem3.add(menuItem2SubItem3GO);
  menuItem3.add(menuItemANO).add(menuItemMES).add(menuItemDIA).add(menuItemHORA).add(menuItemMINUTO).add(menuItemEXIT);
  menuItemDIA.addRight(menuItemDIA);
  menuItemMES.addRight(menuItemMES);
  menuItemANO.addRight(menuItemANO);
  menuItemHORA.addRight(menuItemHORA);
  menuItemMINUTO.addRight(menuItemMINUTO);
  menuItemEXIT.addRight(menuItemEXIT);
  menuItem4.add(menuItem4GO);
  menu.toRoot();
//****************************************************************  
  
} 

//****************************************************************

void loop(){ 

  sensorState = digitalRead(sensorPin);

  
  if (sensorState == LOW) {     

  } 
  else {

    value = EEPROM.read(0);
    if (value < 101){
    
      DateTime now = RTC.now();
      sensorMinute = now.minute();
      address = 5 * value + 5;
      addressMinute = EEPROM.read(address);
      if (addressMinute != sensorMinute){
        address = address + 1;
        datahora = now.day();
        EEPROM.write(address, datahora);
        address = address + 1;
        datahora = now.month();
        EEPROM.write(address, datahora);
        address = address + 1;
        datahora = now.year();
        EEPROM.write(address, datahora);
        address = address + 1;
        datahora = now.hour();
        EEPROM.write(address, datahora);
        address = address + 1;
        datahora = now.minute();
        EEPROM.write(address, datahora);
  
        value = value + 1;
        EEPROM.write(0, value);
        address = 0;
        delay(1000);
        softReset();
      }
    } 
  }


  readButtons();  
  navigateMenus(); 


DateTime now = RTC.now();
powerDay = now.day();
powerTime = now.minute();
powerHour = now.hour();

if (powerTime >= powerCounter + 2){
  menu.toRoot();
  lcd.noDisplay();
  digitalWrite(A0, LOW);
  digitalWrite(A2, LOW);
  boLean = 1;
//****************************************************************
[b]
  ADCSRA = 0;  

  MCUSR = 0;     
  
  WDTCSR = _BV (WDCE) | _BV (WDE);
  
  WDTCSR = _BV (WDIE) | _BV (WDP3) | _BV (WDP0);    
  wdt_reset();
  
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
  sleep_enable();
 
  attachInterrupt (0, wake, CHANGE);
  attachInterrupt (1, wake, RISING);
 

  MCUCR = _BV (BODS) | _BV (BODSE);
  MCUCR = _BV (BODS); 
  sleep_cpu ();  
  detachInterrupt (0);
  detachInterrupt (1);

  sleep_disable();

} else{

  ADCSRA = 0;  

  MCUSR = 0;     

  WDTCSR = _BV (WDCE) | _BV (WDE);

  WDTCSR = _BV (WDIE) | _BV (WDP2) | _BV (WDP0);    
  wdt_reset();  
  
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
  sleep_enable();
 
  sleep_cpu ();  


  sleep_disable();
}  [/b]

Rest of the code:

value = EEPROM.read(1);
pwDay = value;
value = EEPROM.read(4);
pwHour = value;
value = EEPROM.read(5);
pwMinute = value;
if (powerTime == pwMinute){
  if (powerHour == pwHour){
    if (powerDay != pwDay){
       digitalWrite(motor1Pin, HIGH);
       digitalWrite(A3, HIGH); 
       delay(100);
       unsigned int valor = analogRead(A1);
       vout = ((valor * 4.9)/1023.0) * 2.2;
       if (vout >= 6){
         digitalWrite(motor1Pin, LOW);
         digitalWrite(A3, LOW);
         delay(100);
         cicloMotor();
         DateTime now = RTC.now();
         datahora = now.day();
         EEPROM.write(1, datahora);
         datahora = now.month();
         EEPROM.write(2, datahora);
         datahora = now.year();
         EEPROM.write(3, datahora);
       }
       digitalWrite(motor1Pin, LOW);
       digitalWrite(A3, LOW);
    }
  }
}
if (boLean == 0){
  verDataHora();
}



} 
//**************************************************************** 

void menuChanged(MenuChangeEvent changed){
  
  MenuItem newMenuItem=changed.to; 

      DateTime now = RTC.now();
      lcd.setCursor(0,0);
      lcd.print("                ");
      lcd.setCursor(0,0);
      myClock = now.day();
      if (myClock < 10){
        lcd.print ("0");
        lcd.print(myClock);
      }
      else{
        lcd.print(myClock);
      }
      lcd.print("-");
      myClock = now.month();
      if (myClock < 10){
        lcd.print ("0");
        lcd.print(myClock);
      }
      else{
        lcd.print(myClock);
      }
      lcd.print("-");
      lcd.print(now.year());
      lcd.print(" ");
      myClock = now.hour();
      if (myClock < 10){
        lcd.print("0");
        lcd.print(myClock);
        lcd.print(":"); 
        }
        else {
        lcd.print(myClock);
        lcd.print(":");
        }
      myClock = now.minute();
      if (myClock < 10){
        lcd.print("0");
        lcd.print(myClock); 
        }
        else {
        lcd.print(myClock);
        }
          
  if(newMenuItem.getName()==menu.getRoot()){
      
      boLean = 0;
      DateTime now = RTC.now();
      powerCounter = now.minute();
      lcd.setCursor(0,1); 
      lcd.print("Menu Principal  ");
      
  }else if(newMenuItem.getName()=="Item1"){
      boLean = 1;
      lcd.setCursor(0,1);
      lcd.print("Entrada Cevadoro");
      lcd.setCursor(0,0);  
      lcd.print("Menu Principal  ");
     
  }else if(newMenuItem.getName()=="Item1SubItem1"){
      boLean = 1;
      lcd.setCursor(0,1);
      lcd.print("Ver Entradas    ");
      lcd.setCursor(0,0);  
      lcd.print("Entrada Cevadoro");
      
  }else if(newMenuItem.getName()=="Item1SubItem1GO"){
      boLean = 1;
      value = EEPROM.read(0); 
      lcd.setCursor(0,0);
      lcd.print("                ");
      lcd.setCursor(0,0);
      lcd.print("Acessos:");
      temp = temp + 1;
      lcd.print(temp);
      lcd.print(" / ");
      lcd.print(value);
      lcd.setCursor(0,1);
      lcd.print("                ");
      
      if (morada <= 5){
        morada = 5;
      }
      morada = morada + 1;
      longitude = EEPROM.read(morada);
      if (longitude < 10) {
        lcd.setCursor(0,1);
        lcd.print("0");
        lcd.setCursor(1,1);
        lcd.print(longitude);
        lcd.setCursor(2,1);
        lcd.print("-");
      }
      else {
        lcd.setCursor(0,1);
        lcd.print(longitude);
        lcd.setCursor(2,1);
        lcd.print("-");
      }
      
      morada = morada + 1;
      longitude = EEPROM.read(morada);
      //int Mydatahora = (int)datahora;
      if (longitude < 10) {
        lcd.setCursor(3,1);
        lcd.print("0");
        lcd.setCursor(4,1);
        lcd.print(longitude);
        lcd.setCursor(5,1);
        lcd.print("-");
        
      }
      else {
        lcd.setCursor(3,1);
        lcd.print(longitude);
        lcd.setCursor(5,1);
        lcd.print("-");
      }
      morada = morada + 1;
      longitude = EEPROM.read(morada);
      //int Mydatahora = (int)datahora;
      if (longitude < 10) {
        lcd.setCursor(6,1);
        lcd.print("0");
        lcd.setCursor(7,1);
        lcd.print(longitude);
        lcd.setCursor(8,1);
        lcd.print(" ");
      }
      else {
        lcd.setCursor(6,1);
        lcd.print(longitude);
        lcd.setCursor(8,1);
        lcd.print(" ");
      }
      morada = morada + 1;
      longitude = EEPROM.read(morada);
      //int Mydatahora = (int)datahora;
      if (longitude < 10) {
        lcd.setCursor(9,1);
        lcd.print("0");
        lcd.setCursor(10,1);
        lcd.print(longitude);
        lcd.setCursor(11,1);
        lcd.print(":");
      }
      else {
        lcd.setCursor(9,1);
        lcd.print(longitude);
        lcd.setCursor(11,1);
        lcd.print(":");
      }
      morada = morada + 1;
      longitude = EEPROM.read(morada);
      //int Mydatahora = (int)datahora;
      if (longitude < 10) {
        lcd.setCursor(12,1);
        lcd.print("0");
        lcd.setCursor(13,1);
        lcd.print(longitude);
      }
      else {
        lcd.setCursor(12,1);
        lcd.print(longitude);
      }
      
      valor = value;
      if (morada==valor * const5 + 10){ 
          lcd.setCursor(0,0);
          lcd.print("Fim das entradas");
          lcd.setCursor(0,1);
          lcd.print("                ");
          delay(3000);
          softReset();
      }
} else if(newMenuItem.getName()=="Item1SubItem2"){
      boLean = 1;
      lcd.setCursor(0,1);
      lcd.print("Apagar Entradas ");
      lcd.setCursor(0,0);  
      lcd.print("Entrada Cevadoro");
} else if(newMenuItem.getName()=="Item1SubItem2GO"){
      boLean = 1;
      EEPROM.write(0, 0);
    for (int i = 6; i < 511; i++) {
      EEPROM.write(i, 0);
      }
      lcd.setCursor(0,1);
      lcd.print("                ");
      lcd.setCursor(0,0);
      lcd.print(" -Eliminando    "); 
      delay(1000);
      lcd.setCursor(0,0);
      lcd.print(" -Eliminando.   ");
      delay(1000);
      lcd.setCursor(0,0);
      lcd.print(" -Eliminando..  ");
      delay(1000);
      lcd.setCursor(0,0);
      lcd.print(" -Eliminando... ");
      delay(1000);
      lcd.setCursor(0,0);
      lcd.print(" -Eliminando....");
      delay(1000);
      lcd.setCursor(0,0); 
      lcd.print("  -Eliminadas-  ");
      lcd.setCursor(0,1);
      lcd.print("                ");
      delay(1000);
      softReset();
    
      //----------------------------
 }else if(newMenuItem.getName()=="Item2"){
      boLean = 1;
      lcd.setCursor(0,1);
      lcd.print("Comedouro       ");
      lcd.setCursor(0,0);  
      lcd.print("Menu Principal  ");
 }else if(newMenuItem.getName()=="Item2SubItem1"){
      boLean = 1;
      lcd.setCursor(0,1);
      lcd.print("Ultima Activacao");
      lcd.setCursor(0,0);  
      lcd.print("Comedouro       ");
[/quote]

Rest of the code:

 }else if(newMenuItem.getName()=="Item2SubItem1GO"){
      boLean = 1;
      lcd.setCursor(0,0);
      lcd.print("Ultima Activacao");
      lcd.setCursor(0,1); 
      lcd.print("                ");
      lcd.setCursor(0,1); 
      morada = 1; 
      longitude = EEPROM.read(morada);
      if (longitude < 10) {
        lcd.setCursor(0,1);
        lcd.print("0");
        lcd.setCursor(1,1);
        lcd.print(longitude);
        lcd.setCursor(2,1);
        lcd.print("-");
      }
      else {
        lcd.setCursor(0,1);
        lcd.print(longitude);
        lcd.setCursor(2,1);
        lcd.print("-");
      }
      
      morada = morada + 1;
      longitude = EEPROM.read(morada);
      //int Mydatahora = (int)datahora;
      if (longitude < 10) {
        lcd.setCursor(3,1);
        lcd.print("0");
        lcd.setCursor(4,1);
        lcd.print(longitude);
        lcd.setCursor(5,1);
        lcd.print("-");
        
      }
      else {
        lcd.setCursor(3,1);
        lcd.print(longitude);
        lcd.setCursor(5,1);
        lcd.print("-");
      }
      morada = morada + 1;
      longitude = EEPROM.read(morada);
      //int Mydatahora = (int)datahora;
      if (longitude < 10) {
        lcd.setCursor(6,1);
        lcd.print("0");
        lcd.setCursor(7,1);
        lcd.print(longitude);
        lcd.setCursor(8,1);
        lcd.print(" ");
      }
      else {
        lcd.setCursor(6,1);
        lcd.print(longitude);
        lcd.setCursor(8,1);
        lcd.print(" ");
      }
      morada = morada + 1;
      longitude = EEPROM.read(morada);
      //int Mydatahora = (int)datahora;
      if (longitude < 10) {
        lcd.setCursor(9,1);
        lcd.print("0");
        lcd.setCursor(10,1);
        lcd.print(longitude);
        lcd.setCursor(11,1);
        lcd.print(":");
      }
      else {
        lcd.setCursor(9,1);
        lcd.print(longitude);
        lcd.setCursor(11,1);
        lcd.print(":");
      }
      morada = morada + 1;
      longitude = EEPROM.read(morada);
      //int Mydatahora = (int)datahora;
      if (longitude < 10) {
        lcd.setCursor(12,1);
        lcd.print("0");
        lcd.setCursor(13,1);
        lcd.print(longitude);
      }
      else {
        lcd.setCursor(12,1);
        lcd.print(longitude);
      }

 }else if(newMenuItem.getName()=="Item2SubItem2"){
      boLean = 1;
      lcd.setCursor(0,1);
      lcd.print("Def Hora Activ. ");
      lcd.setCursor(0,0);  
      lcd.print("Comedouro       ");
}else if(newMenuItem.getName()=="Item2SubItem2HORA"){
      boLean = 1;
      lcd.setCursor(0,0);
      lcd.print("Def Hora Activ. ");
      HORA = HORA + 1;
      if (HORA > 23){
        HORA = 0;
      }
      lcd.setCursor(0,1);
      lcd.print("                ");
      lcd.setCursor(0,1);
      lcd.print("      ");
      if (HORA < 10){
        lcd.print("0");
        lcd.print(HORA);
      } else if (HORA >= 10){
        lcd.print(HORA);
      }
      value1 = HORA;
 }else if(newMenuItem.getName()=="Item2SubItem2MINUTO"){
      boLean = 1;
      lcd.setCursor(0,0);
      lcd.print("Def Min Activ.  ");
      MINUTO = MINUTO + 1;
      if (MINUTO > 59){
        MINUTO = 0;
      }
      lcd.setCursor(0,1);
      lcd.print("                ");
      lcd.setCursor(0,1);
      lcd.print("      ");
      if (MINUTO < 10){
        lcd.print("0");
        lcd.print(MINUTO);
      } else if (MINUTO >= 10){
        lcd.print(MINUTO);
      }
      value2 = MINUTO;
 }else if(newMenuItem.getName()=="Item2SubItem2EXIT"){
      EEPROM.write(4, value1);
      EEPROM.write(5, value2);
      softReset();
 }else if(newMenuItem.getName()=="Item2SubItem3"){
      boLean = 1;
      lcd.setCursor(0,1);
      lcd.print("Activar         ");
      lcd.setCursor(0,0);  
      lcd.print("Comedouro       ");
 }else if(newMenuItem.getName()=="Item2SubItem3GO"){
      boLean = 1;
      
      cicloMotor();
      softReset();
 }else if(newMenuItem.getName()=="Item2SubItem4"){
      boLean = 1;
      lcd.setCursor(0,1);
      lcd.print("Def Ciclo Activ.");
      lcd.setCursor(0,0);  
      lcd.print("Comedouro       ");
 }else if(newMenuItem.getName()=="Item2SubItem4GO"){
      boLean = 1;
      lcd.setCursor(0,0);
      lcd.print("                ");
      lcd.setCursor(0,0);
      lcd.print("Ciclo Actual:");
      value = EEPROM.read(511);
      lcd.print(value);
      lcd.setCursor(0,1);
      lcd.print("                ");
      lcd.setCursor(0,1);
      lcd.print("Novo Ciclo: ");
      valor = valor + 1;
      lcd.print(valor);
 }else if(newMenuItem.getName()=="Item2SubItem4EXIT"){
      boLean = 1;
      value = valor;
      EEPROM.write(511, value);
      softReset();
 }else if(newMenuItem.getName()=="Item3"){
      boLean = 1;
      lcd.setCursor(0,1);
      lcd.print("Definir DataHora");
      lcd.setCursor(0,0);  
      lcd.print("Menu Principal  ");
  }else if(newMenuItem.getName()=="ItemANO"){
      boLean = 1;
      lcd.setCursor(0,0);  
      lcd.print("Definir Ano     ");
      ANO = ANO + 1;
      temp = 2000 + ANO;
      if (temp > 3000){
        temp = 2000;
      }
      lcd.setCursor(0,1);
      lcd.print("                ");
      lcd.setCursor(0,1);
      lcd.print("      ");
      lcd.print(temp);
      
  }else if(newMenuItem.getName()=="ItemMES"){
      boLean = 1;
      lcd.setCursor(0,0);
      lcd.print("Definir Mes     ");
      MES = MES + 1;
      if (MES > 12){
        MES = 1;
      }
      lcd.setCursor(0,1);
      lcd.print("                ");
      lcd.setCursor(0,1);
      lcd.print("      ");
      if (MES < 10){
        lcd.print("0");
        lcd.print(MES);
      } else if (MES >= 10){
        lcd.print(MES);
      }
      
  }else if(newMenuItem.getName()=="ItemDIA"){
      boLean = 1;
      lcd.setCursor(0,0);
      lcd.print("Definir Dia     ");
      DIA = DIA + 1;
      if (DIA > 31){
        DIA = 1;
      }
      lcd.setCursor(0,1);
      lcd.print("                ");
      lcd.setCursor(0,1);
      lcd.print("      ");
      if (DIA < 10){
        lcd.print("0");
        lcd.print(DIA);
      } else if (DIA >= 10){
        lcd.print(DIA);
      }
      
  }else if(newMenuItem.getName()=="ItemHORA"){
      boLean = 1;
      lcd.setCursor(0,0);
      lcd.print("Definir Hora    ");
      HORA = HORA + 1;
      if (HORA > 23){
        HORA = 0;
      }
      lcd.setCursor(0,1);
      lcd.print("                ");
      lcd.setCursor(0,1);
      lcd.print("      ");
      if (HORA < 10){
        lcd.print("0");
        lcd.print(HORA);
      } else if (HORA >= 10){
        lcd.print(HORA);
      }
      
  }else if(newMenuItem.getName()=="ItemMINUTO"){
      boLean = 1;
      lcd.setCursor(0,0);
      lcd.print("Definir Minuto  ");
      MINUTO = MINUTO + 1;
      if (MINUTO > 59){
        MINUTO = 0;
      }
      lcd.setCursor(0,1);
      lcd.print("                ");
      lcd.setCursor(0,1);
      lcd.print("      ");
      if (MINUTO < 10){
        lcd.print("0");
        lcd.print(MINUTO);
      } else if (MINUTO >= 10){
        lcd.print(MINUTO);
      }
     
  }else if(newMenuItem.getName()=="ItemEXIT"){
      boLean = 1;
      RTC.adjust(DateTime(ANO, MES, DIA, HORA, MINUTO));
      ANO = -1;
      MES = 0;
      DIA = 0;
      HORA = -1;
      MINUTO = -1;
      temp = 0;  
      softReset();
  }else if(newMenuItem.getName()=="Item4"){
      boLean = 1;
      lcd.setCursor(0,1);
      lcd.print("Consumo Bateria ");
      lcd.setCursor(0,0);  
      lcd.print("Menu Principal  ");
  }else if(newMenuItem.getName()=="Item4GO"){
      boLean = 1;
      bateria();
  }
}

Rest of the code:

void menuUsed(MenuUseEvent used){

}

void softReset(){
  ANO = -1;
  MES = 0;
  DIA = 0;
  HORA = -1;
  MINUTO = -1;
  temp = 0;
  valor = 0;
  asm volatile ("  jmp 0");
}

void verDataHora(){

      DateTime now = RTC.now();
      lcd.setCursor(0,0);
      lcd.print("                ");
      lcd.setCursor(0,0);
      myClock = now.day();
      if (myClock < 10){
        lcd.print ("0");
        lcd.print(myClock);
      }
      else{
        lcd.print(myClock);
      }
      lcd.print("-");
      myClock = now.month();
      if (myClock < 10){
        lcd.print ("0");
        lcd.print(myClock);
      }
      else{
        lcd.print(myClock);
      }
      lcd.print("-");
      lcd.print(now.year());
      lcd.print(" ");
      myClock = now.hour();
      if (myClock < 10){
        lcd.print("0");
        lcd.print(myClock);
        lcd.print(":"); 
        }
        else {
        lcd.print(myClock);
        lcd.print(":");
        }
      myClock = now.minute();
      if (myClock < 10){
        lcd.print("0");
        lcd.print(myClock); 
        }
        else {
        lcd.print(myClock);
        }
    }
   
void cicloMotor(){
  digitalWrite(enablePin, LOW);
  digitalWrite(motor1Pin, LOW);  
  digitalWrite(motor2Pin, LOW);
  value = EEPROM.read(511);
  for (int i = 0; i < value; i++) {
    i;
    digitalWrite(motor1Pin, LOW);   
    digitalWrite(motor2Pin, HIGH);
    digitalWrite(enablePin, HIGH); 
    delay(2000);
  
    digitalWrite(enablePin, LOW);
    delay(500);
    digitalWrite(motor1Pin, HIGH);  
    digitalWrite(motor2Pin, LOW);
    digitalWrite(enablePin, HIGH);
    delay(2000);
  
    digitalWrite(enablePin, LOW);
    digitalWrite(motor1Pin, LOW);  
    digitalWrite(motor2Pin, LOW);
    delay(500);
  }
}

void bateria(){
 
 digitalWrite(motor1Pin, HIGH);
 digitalWrite(A3, HIGH); 
 delay(200);
 unsigned int valor = analogRead(A1);
 vout = ((valor * 4.9)/1023.0) * 2.2;
 percent = ((vout-6)/3) * 100.0;
 delay(500);
 lcd.setCursor(0,0);
 lcd.print("                ");
 lcd.setCursor(0,0);
 lcd.print("B:");
 lcd.print(percent);
 lcd.print("%|V:");
 lcd.print(vout);
 lcd.setCursor(0,1);
 lcd.print("                ");
 lcd.setCursor(0,1);
 double a=lenght/100*percent;
 // drawing black rectangles on LCD
 
  if (a>=1) {
 
    for (int i=1;i<a;i++) {
 
      lcd.write(4);
 
      b=i;
    }
 
    a=a-b;
 
  }
 
  peace=a*5;
 
// drawing charater's colums
 
  switch (peace) {
 
  case 0:
     lcd.print((char)0);
     lcd.blink();
    break;
 
  case 1:
    lcd.print((char)0);
 
    break;
 
  case 2:
    lcd.print((char)1);
    break;
 
  case 3:
    lcd.print((char)2);
    break;
 
  case 4:
    lcd.print((char)3);
    break;
 
  }
 
digitalWrite(motor1Pin, LOW);
digitalWrite(A3, LOW); 
}



void  readButtons(){  
  int reading;
  int buttonEnterState=LOW;      
  int buttonEscState=LOW;           
  int buttonLeftState=LOW;           
  int buttonRightState=LOW;             

  //Enter button
                  
                  reading = digitalRead(buttonPinEnter);

                  
                  if (reading != lastButtonEnterState) {
                    
                    lastEnterDebounceTime = millis();
                  } 
                  
                  if ((millis() - lastEnterDebounceTime) > debounceDelay) {
                    
                    buttonEnterState=reading;
                    lastEnterDebounceTime=millis();
                  }
                  
                  
                  lastButtonEnterState = reading;
                  

    //Esc button               
                  
                  reading = digitalRead(buttonPinEsc);

                  
                  if (reading != lastButtonEscState) {
                    
                    lastEscDebounceTime = millis();
                  } 
                  
                  if ((millis() - lastEscDebounceTime) > debounceDelay) {
                    
                    buttonEscState = reading;
                    lastEscDebounceTime=millis();
                  }
                  
                  
                  lastButtonEscState = reading; 
                  
                     
   //Down button               
                  
                  reading = digitalRead(buttonPinRight);

                  
                  if (reading != lastButtonRightState) {
                    
                    lastRightDebounceTime = millis();
                  } 
                  
                  if ((millis() - lastRightDebounceTime) > debounceDelay) {
                    
                    buttonRightState = reading;
                   lastRightDebounceTime =millis();
                  }
                  
                  
                  lastButtonRightState = reading;                  
                  
                  
    //Up button               
                  
                  reading = digitalRead(buttonPinLeft);

                  
                  if (reading != lastButtonLeftState) {
                    
                    lastLeftDebounceTime = millis();
                  } 
                  
                  if ((millis() - lastLeftDebounceTime) > debounceDelay) {
                    
                    buttonLeftState = reading;
                    lastLeftDebounceTime=millis();;
                  }
                  
                  
                  lastButtonLeftState = reading;  

                  
                  if (buttonEnterState==HIGH){
                    lastButtonPushed=buttonPinEnter;

                  }else if(buttonEscState==HIGH){
                    lastButtonPushed=buttonPinEsc;

                  }else if(buttonRightState==HIGH){
                    lastButtonPushed=buttonPinRight;

                  }else if(buttonLeftState==HIGH){
                    lastButtonPushed=buttonPinLeft;

                  }else{
                    lastButtonPushed=0;
                  }                  
}

void navigateMenus() {
  MenuItem currentMenu=menu.getCurrent();
  DateTime now = RTC.now();
  
  switch (lastButtonPushed){
    case buttonPinEnter:
      if(!(currentMenu.moveDown())){  
        digitalWrite(A0, HIGH);
        digitalWrite(A2, HIGH);
        lcd.begin(16, 2);
        lcd.display();
        powerCounter = now.minute();
        menu.use();
      }else{  
        digitalWrite(A0, HIGH);
        digitalWrite(A2, HIGH);
        lcd.begin(16, 2);        
        lcd.display();
        powerCounter = now.minute();
        menu.moveDown();
       } 
      break;
    case buttonPinEsc:
      digitalWrite(A0, HIGH);
      digitalWrite(A2, HIGH);
      lcd.begin(16, 2);
      powerCounter = now.minute();
      softReset();  //back to main
      break;
    case buttonPinRight:
      //digitalWrite(A0, HIGH);
      //digitalWrite(A2, HIGH);
          
      lcd.display();
      powerCounter = now.minute();
      menu.moveRight();
      break;      
    case buttonPinLeft:
      //digitalWrite(A0, HIGH);
      //digitalWrite(A2, HIGH);
           
      lcd.display();
      powerCounter = now.minute();
      menu.moveLeft();
      break;      
  }
  
  lastButtonPushed=0; //reset the lastButtonPushed variable
delay(100);
}

//****************************************************************

HELP!

Wow, there is a ton of code there. Do you actually have enough RAM for all those strings? What Arduino are you running this on?

Since most of this logic seems unrelated to the power consumption I suggest you write a minimal sketch that just contains the logic to do with power management and see if you get similar results.

It might also be interesting to find out whether the power is being consumed by the Arduino itself, or something attached to it. Have you tried disconnecting all the external devices to see whether that affects the consumption?

Wow, there is a ton of code there.

Yep. and I know some of this stuff could have been done in a more efficient manner, but that would require more time to learn... But I'm getting there.

Do you actually have enough RAM for all those strings?

Don't actually know... But I assume I have, cause it all works. The only thing that doesn't work like it should is the power management part.

What Arduino are you running this on?

It's not an Arduino. It's an Atmega 328p chip with Arduino bootloader at 16mHz, with a RTC, a vibration sensor, one L293 H-bridge, one low drop voltage regulator, one 16X2 LCD and 4 buttons and a few other components.

Since most of this logic seems unrelated to the power consumption I suggest you write a minimal sketch that just contains the logic to do with power management and see if you get similar results.

I did, and it works fine. The hardware setup was the same, and at XXmA resolution, it read 0mA in sleep mode and 15 mA while awake. As soon as I add the rest of the program, it hits the fan...

It might also be interesting to find out whether the power is being consumed by the Arduino itself, or something attached to it. Have you tried disconnecting all the external devices to see whether that affects the consumption?

No, I didn't, but that's a good idea. I'm going to start stripping stuff from the borad and measure what happens. If it's not hardware (I assumed it isn't, but it could be), I'll start deleting chunks of code and measure it. Gonna try it as soon as I get home. This stuff is driving me crazy! My wife is starting to hate electronics... XD

vander:
It's not an Arduino. It's an Atmega 328p chip

It sounds as if you are losing about an extra 50mA when your real sketch sleeps compared to your test sketch. I haven't reviewed all that code but I suppose you aren't trying to do anything that should use power when it's asleep. Perhaps it's 'just' one of the eternal devices which is responding poorly when your Arduino sleeps, but it might also be that the code is doing something you don't expect. Memory corruption due to a bug in your code or due to running out of RAM could possibly cause that sort of wierd behaviour so I suggest you find out how much free memory you've actually got. Nothing stands out as being huge, but you've got lots of variable and strings in there that will all be eating away at your free memory.

I didn’t go thru all the code - is there something that is switching on/off while the code is running, that ends up going to full on when sleeping & thus drawing more current? The IO pins stay at the last high/low level you set them when you go into sleep mode, so if something was left on it then stays on.

I'll bet the reason is the 1M resistor across the resonator for the 328P. When the oscillator is running, the CMOS is switching between high and low states.

When the oscillator is stopped, the oscillator circuit is self-biased to approximately 1/2 VCC and both the top and bottom MOSFETS of the output stage (of the oscillator circuit) are in the linear, conducting region.

PeterH:

vander:
It's not an Arduino. It's an Atmega 328p chip

Memory corruption due to a bug in your code or due to running out of RAM could possibly cause that sort of wierd behaviour so I suggest you find out how much free memory you've actually got.

I didn't even knew it was possible to see memory usage in Atmega chips. I'm embaraced to even ask, but how do I do that?

Krupski:
I'll bet the reason is the 1M resistor across the resonator for the 328P. When the oscillator is running, the CMOS is switching between high and low states.

When the oscillator is stopped, the oscillator circuit is self-biased to approximately 1/2 VCC and both the top and bottom MOSFETS of the output stage (of the oscillator circuit) are in the linear, conducting region.

Gonna need a few minutes alone with your post and Google to even understand what you just said...
That said, Across the 16mHz I have 2 22p ceramic capacitors, but no resistor. Is there a 1M resistor in the chip itself?

If you have 16 MHz and two 22pF caps, that is not the cause of the current draw.

CrossRoads:
I didn't go thru all the code - is there something that is switching on/off while the code is running, that ends up going to full on when sleeping & thus drawing more current? The IO pins stay at the last high/low level you set them when you go into sleep mode, so if something was left on it then stays on.

I don't think so, but I'll check and get back to you all.

You shouldn't be embarrassed, because it's not part of the Arduino API and nobody knows until they're told, but there are a couple of different ways to measure the amount of free memory - you will find the code in the playground. One common version just allocates increasingly large chunks of memory from the heap until a failure occurs, and reports the last successful size as the free amount. I don't like that one - it's not actually telling you how much memory is free, only how much you could get from malloc(); that's pretty irrelevant unless you're using dynamic memory allocation. The other one compares the address of the end of the stack with the address of the end of the heap, and this is the one I suggest you use. When the stack runs into the heap, you're out of memory and will start trampling over your data.

Thanks for all your support. With your help, I figured what was going on, but not why it was happening…

The culprit was the pot I have on the LCD. I use two analog pins to power the backlight and the LCD. Before I put the chip to sleep, I was setting the two analog pins to low to shut down the LCD. I hooked the multimeter to the board and, as soon as it went to sleep, I started unplugging stuff. First, the sensor. It still read 50 mA. Then, the LCD’s backlight. Still read 50mA. When I unplugged the LCD pin, the multimeter started reading 4 to 7 mA. Then, I commented the “digitalWrite(A2, LOW);”, tried the new code and it worked!

Now I ask: If I set the A2 pin to low, why was it still being drained? I didn’t measure the actual pin to see if it was the one being drained, but I figure it was…

Anyway, thanks for your support! XD

If you want to power the +5V line of an LCD from a digital output pin, you should:

  1. Connect a 100 ohm resistor between the output pin and the LCD +5V, to limit the current spike when you power the LCD on or off and its decoupling capacitor charges or discharges; and

  2. Ensure that all the I/O lines from the mcu to the LCD are set to LOW before you power off the LCD.

I've done this in a design that powers off the LCD when going into sleep mode, and it works for me. I'm not familiar with the code for the lcd.noDisplay() call, but I suspect that it doesn't do (2). What's happening is that one or more of the inputs to the LCD is being driven high, and an input pin protection diode in the LCD is feeding that current into the +5V line, which you are holding low.

dc42:
If you want to power the +5V line of an LCD from a digital output pin, you should:

  1. Connect a 100 ohm resistor between the output pin and the LCD +5V, to limit the current spike when you power the LCD on or off and its decoupling capacitor charges or discharges; and

I have a 2K potentiometer betwen output and the LCD +5V, so that’s covered.

dc42:
2. Ensure that all the I/O lines from the mcu to the LCD are set to LOW before you power off the LCD.

I’ve done this in a design that powers off the LCD when going into sleep mode, and it works for me. I’m not familiar with the code for the lcd.noDisplay() call, but I suspect that it doesn’t do (2). What’s happening is that one or more of the inputs to the LCD is being driven high, and an input pin protection diode in the LCD is feeding that current into the +5V line, which you are holding low.

That’s probably it. I don’t see how it would drain my battery any other way. I’ll check when I get home.

Thanks! With this and a solar panel, my boar feeder will run forever ]:smiley: