estimating battery run time I have a project which runs a continuous loop and listens for the push of a button to light an led and activate a servo motor on an Arduino Uno (R2). The arduino is powered by 4 AA batteries, and the servo is powered by a 9V. I'm wondering how long I can leave this running off battery power. Does anyone know how to estimate this? As a side note, if anyone spots a way to improve battery life in my sketch / wiring, I'd love to know. 

Below is my code and attached is the wiring diagram for the project (minus the batteries).

    #include &lt;Servo.h&gt;

const int buttonPin = 2;     // the number of the pushbutton pin
boolean buttonPushed = false;

const int ledPin =  13;      // the number of the LED pin


const int servoPin = 9;
Servo servo;
int angle = 0;
int servoStartAngle = 90;
int servoEndAngle = 10;

boolean servoActivated = false;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

int photoCellPin = 2;
int photoCellThreshold = 500;
int photoCellActivated = false;


void setup() {
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  servo.attach(servoPin);
  servo.write(servoStartAngle);

}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  int photoCellVal = analogRead(photoCellPin);

  if (photoCellVal &gt; photoCellThreshold){
   photoCellActivated = true; 
  } else {
   photoCellActivated = false; 
  }

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW &amp;&amp; buttonPushed == false) {     
    // turn LED on:    
    buttonPushed = true;
    Serial.println("button pushed");
  } else if (buttonState == LOW &amp;&amp; buttonPushed == true){
   buttonPushed = false;
   Serial.println('deactivate');
  } 



  if (buttonPushed == true){
   digitalWrite(ledPin, HIGH); 
  } else {
   digitalWrite(ledPin, LOW); 
  }

  if (buttonPushed &amp;&amp; servoActivated == false &amp;&amp; photoCellActivated == true){
    servoActivated = true;
    Serial.println("activate motor");

    for(angle = servoStartAngle; angle &gt; servoEndAngle; angle--){                                  
      servo.write(angle);
      Serial.println(angle);
      delay(15);    
    } 
  } else if (buttonPushed == false &amp;&amp; servoActivated == true){ //deactivate
    for(angle = servoEndAngle; angle &lt; servoStartAngle; angle++){                                  
      servo.write(angle);
      Serial.println(angle);
      delay(15);    
    }
    delay(200);
    servoActivated = false; 

  }

  delay(200);
}
I'm wondering how long I can leave this running off battery power. Does anyone know how to estimate this?


If your circuit draws 1A @ 12V, a 12V 1Ah battery will power it for approximately one hour. A 9V alkaline typically contains about 565mAh. You can use the datasheets to sum all the power consumption, but its probably easier just to measure it with a multimeter.


  As a side note, if anyone spots a way to improve battery life in my
  sketch / wiring, I'd love to know.


-Check out the ATTiny series (parametric); they are tiny, low-power, and Arduino-compatible; perfect when you only need a couple of pins.

-Use an interrupt to monitor the button's pin; the MCU can sleep until the interrupt fires.

Generally:
1) The interrupt will set a flag so main() knows that something happened, then wake the processor.
2) Main() resumes, checks the flag, turns on/off the LED accordingly, tells the servo where to go, waits for the servo to get there (delay()), clears the flag, and then puts the MCU back to sleep.

-As others have pointed out, delay() is bad. delay() tells the MCU to do nothing (NO-OP) repeatedly, for a period of time. This is different from having nothing to do because it's asleep.