user6341: 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 <Servo.h>

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 > photoCellThreshold){
   photoCellActivated = true; 
  } else {
   photoCellActivated = false; 
  }

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



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

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

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

  }

  delay(200);
}


user6888: I can support what mheavers said in a comment above. Use a Bareduino or any kind of custom Board, use sleep-modes and avoid delay(). I wrote a blog-post about my experiences with this setup here, if you get it right the ATmel can go down below 1mA in sleep. In your case an extreme scenario (depending on your usage pattern) could be to wake-up the Arduino with an interrupt triggered through the sensor and otherwise sleep completely. You will find example on the net where people used a setup like this for remote controls for instance.

user6980: 
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.

6341: <p>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. </p>

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

<pre><code>    #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);
}
</code></pre>
 6888: <p>I can support what mheavers said in a comment above. Use a Bareduino or any kind of custom Board, use sleep-modes and avoid delay(). I wrote a blog-post about my experiences with this setup <a href="https://holadimake.wordpress.com/2015/01/02/building-a-cellular-tracker-part3-running-on-batteries-for-months/" rel="nofollow">here</a>, if you get it right the ATmel can go down below 1mA in sleep. In your case an extreme scenario (depending on your usage pattern) could be to wake-up the Arduino with an interrupt triggered through the sensor and otherwise sleep completely. You will find example on the net where people used a setup like this for remote controls for instance.</p>
 6980: <blockquote>
  <p>I'm wondering how long I can leave this running off battery power. Does anyone know how to estimate this?</p>
</blockquote>

<p>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.</p>

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

<p>-Check out the ATTiny series (<a href="http://www.atmel.com/v2PFResults.aspx#(actives:!(8238,8394,8362,8282,8431,8300,8358,8392,8378,8445,8236,8449,8474,8248,8264,8447,8256,8254,8286,8462,8429,8458,8466,8400,8302,8278),data:(area:&#39;&#39;,category:&#39;34864[33180[33086]]&#39;,pm:!((i:8238,v:!(1,7)),(i:8394,v:!(0,7)),(i:8362,v:!(5,12)),(i:8282,v:!(5)),(i:8431,v:!(1,8)),(i:8300,v:!(1,5)),(i:8358,v:!(1,19)),(i:8392,v:!(0,1)),(i:8378,v:!n),(i:8445,v:!(5,6,9)),(i:8236,v:!(0,9)),(i:8449,v:!(1,3)),(i:8474,v:!(0)),(i:8248,v:!(0,1)),(i:8264,v:!(1,2)),(i:8447,v:!(0,1)),(i:8256,v:!(1,4)),(i:8254,v:!(3,16)),(i:8286,v:!(0)),(i:8462,v:!(0,2)),(i:8429,v:!(1,2)),(i:8458,v:!(0,1)),(i:8466,v:!(4)),(i:8400,v:!(0,7)),(i:8302,v:!(0)),(i:8278,v:!(0))),view:table),sc:1)" rel="nofollow">parametric</a>); they are tiny, low-power, and Arduino-compatible; perfect when you only need a couple of pins.</p>

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

<p>Generally:<br>
1) The interrupt will set a flag so main() knows that something happened, then wake the processor.<br>
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.</p>

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