New code:
#include <avr/sleep.h>
#include <avr/power.h>
enum {
FUNCTION, CONSTANT, PULL_UP, PULL_UP_2TO12, FLOATING, OUTPUT_LOW, OUTPUT_HIGH, OUTPUT_HIGH_2TO12};
void sleep(){
delay(100);
sleep_enable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //set sleep mode
pins(PULL_UP_2TO12);
powerDisable(FUNCTION);
MCUCR = _BV (BODS) | _BV (BODSE); // turn on brown-out enable select
MCUCR = _BV (BODS); // this must be done within 4 clock cycles of above
sleep_cpu();
}
void powerDisable(byte type){
if(type == FUNCTION){
power_adc_disable();
power_spi_disable();
power_timer0_disable();
power_timer1_disable();
power_timer2_disable();
power_twi_disable();
}
else if (type == CONSTANT){
ADCSRA = 0;
PRR = 0xff;
}
}
void pins(byte type){
if (type == OUTPUT_HIGH){ //Pins 2 - 13 output, HIGH
DDRB = 0x2f; //or 0
DDRD = 0xfc; //or 0
PORTB = 0x2f; //or 0x1f, 0
PORTD = 0xfc; //or 0
}
else if (type == OUTPUT_LOW){ //Pins 2 - 13 output, LOW
DDRB = 0x2f; //or 0
DDRD = 0xfc; //or 0
PORTB = 0; //or 0x1f, 0
PORTD = 0; //or 0
}
else if (type == PULL_UP){ //All pins input, pins 2 - 13 pull-up
DDRB = 0; //or 0
DDRD = 0; //or 0
PORTB = 0x2f; //or 0x1f, 0
PORTD = 0xfc; //or 0
}
else if (type == OUTPUT_HIGH_2TO12){ //Pins 2 - 13 output, pins 2 - 12 HIGH
DDRB = 0x2f; //or 0
DDRD = 0xfc; //or 0
PORTB = 0x1f; //or 0x1f, 0
PORTD = 0xfc; //or 0
}
else if (type == PULL_UP_2TO12){ //All pins input, pins 2 - 12 pull-up
DDRB = 0; //or 0
DDRD = 0; //or 0
PORTB = 0x1f; //or 0x1f, 0
PORTD = 0xfc; //or 0
}
else if (type == FLOATING){ //All pins input, floating
DDRB = 0; //or 0
DDRD = 0; //or 0
PORTB = 0; //or 0x1f, 0
PORTD = 0; //or 0
}
}
void setup(){
sleep();
}
void loop(){
}
I will test again next Saturday with 8.22V through Vin pin, hopefully. You can ignore the //or 0x1f comments.
I wish that the Arduino had a [manual] switch to disable the USB chip, so it won't waste power for no reason when running on battery power!
Testing Conditions:
BLANK SKETCH: 54.1mA
Awake with SPI, TWI, USART0 and ADC disabled: 43.7mA
Pins 2 - 13 output, HIGH: 41.4mA
Pins 2 - 13 output, pins 2 - 12 HIGH: 38.3mA
Pins 2 - 13 output, LOW: 38.3mA
All pins input, all floating: 31.4mA
All pins input, pins 2 - 13 pull-up: 31.5mA
All pins input, pins 2 - 12 pull-up: 31.4mA