Index: branches/analog/code/projects/libdragonfly/analog.c
===================================================================
--- branches/analog/code/projects/libdragonfly/analog.c	(revision 1377)
+++ branches/analog/code/projects/libdragonfly/analog.c	(revision 1378)
@@ -30,7 +30,8 @@
  * Contains functions for manipulating the ADC on the Dragonfly board.
  * 
  * @author Colony Project, CMU Robotics Club
- * code mostly taken from fwr analog file (author: Tom Lauwers)
+ * originally taken from fwr analog file (author: Tom Lauwers)
+ * loop code written by Kevin Woo and James Kong
  **/
 
 #include <util/delay.h>
@@ -57,6 +58,19 @@
  * Call analog_init before reading from the analog ports.
  *
  * @see analog8, analog10, analog_get8, analog_get10
+ *
+ * @bug First conversion takes a performance penalty of
+ * 25 vs. 13 ADC clock cycles of successive conversions.
+ * Analog_init should run a dummy conversion to pre-empt
+ * this.
+ *
+ * For good 10-bit precision, ACD clock must be between
+ * 50kHz and 200kHz. Currently, ADC clock is fixed at
+ * 125kHz using 1/64prescalar. However, most code uses
+ * 8-bit precision which can work at ADC clock speeds
+ * higher than 200kHz. Experimental tests needed to
+ * determine highest clock speed for accurate 8-bit ADC.
+ *
  **/
 void analog_init(int start_conversion) {
 	for (int i = 0; i < 11; i++) {
@@ -81,7 +95,7 @@
 	// Bit 3 - Enable ADC Interrupt (required to run free-running mode)
 	// Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/64 = 125kHz
 	ADCSRA = 0;
-	ADCSRA |= _BV(ADEN) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
+	ADCSRA |= _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
 	
 	// Set external mux lines to outputs
 	DDRG |= 0x1C;
@@ -155,11 +169,11 @@
  * Starts the analog update loop. Will continue to run
  * until analog_stop_loop is called.
  *
- * @see analog_stop_loop
+ * @see analog_stop_loop, analog_loop_running
  **/
 void analog_start_loop(void) {
-	//Start the conversion
-	ADCSRA |= _BV(ADSC);
+	//Start the conversion, enable ADC interrupt
+	ADCSRA |= _BV(ADSC) | _BV(ADIE);
 	adc_loop_running = 0x1;
 }
 
@@ -169,13 +183,39 @@
  * is interrupted. No further updates will be made until
  * the loop is started again.
  *
- * @see analog_start_loop
+ * @param Whether or not to block until current conversion
+ * to complete. Use WAIT_UNTIL_FINISHED or
+ * SKIP_WAIT.
+ *
+ * @see analog_start_loop, analog_loop_running
  **/
-void analog_stop_loop(void) {
+void analog_stop_loop(int skip_wait) {
 	//Stop the conversion
 	adc_loop_running = 0x0;
+	
+	if(skip_wait) {
+		//Stop the interrupt
+		ADCSRA &= _BV(ADIE);
+		return;
+	}
+		
+	//Wait for the ADC interrupt flag to clear / conversion to complete
+	while(ADCSRA & _BV(ADIF));
+	//Stop the interrupt
+	ADCSRA &= _BV(ADIE);
 }
+
 /**
+ * Returns the status of loop. 1 for running.
+ * 0 for not running.
+ *
+ * @see analog_start_loop, analog_stop_loop
+ **/
+int analog_loop_running(void) {
+	return adc_loop_running;
+}
+
+/**
  * Reads an 8-bit number from an analog port.
  * analog_init must be called before using this function.
  * The analog loop must also be stopped before using this
Index: branches/analog/code/projects/libdragonfly/analog.h
===================================================================
--- branches/analog/code/projects/libdragonfly/analog.h	(revision 1377)
+++ branches/analog/code/projects/libdragonfly/analog.h	(revision 1378)
@@ -36,7 +36,8 @@
  * to analog.
 	
  * @author Colony Project, CMU Robotics Club, based on firefly
- * code by Tom Lauwers
+ * originally taken from fwr analog file (author: Tom Lauwers)
+ * loop code written by Kevin Woo and James Kong
  */
 
 #ifndef _ANALOG_H
@@ -91,6 +92,11 @@
 /** @brief Analog port for the battery voltage detector **/
 #define BATT_PORT  AN11
 
+/** @brief Wait for current conversion to complete. Parameter for analog_stop_loop **/
+#define WAIT_UNTIL_FINISHED 0
+/** @brief Don't wait for current conversion to complete. Parameter for analog_stop_loop **/
+#define SKIP_WAIT 1
+
 #define ADC_START 1
 #define ADC_STOP 0
 
@@ -110,6 +116,8 @@
 void analog_start_loop(void);
 /** @brief Stops the analog loop. Doesn't do anything if the loop is already stopped. **/
 void analog_stop_loop(void);
+/** @brief Returns the status of the analog loop. **/
+int analog_loop_running(void);
 /** @brief Returns an 8-bit analog value from the look up table. Use this instead of analog_get8. **/
 unsigned int analog8(int which);
 /** @brief Returns an 10-bit analog value from the look up table. Use this instead of analog_get10. **/
Index: branches/analog/code/projects/libdragonfly/bom.c
===================================================================
--- branches/analog/code/projects/libdragonfly/bom.c	(revision 1377)
+++ branches/analog/code/projects/libdragonfly/bom.c	(revision 1378)
@@ -167,8 +167,12 @@
  **/
 void bom_refresh(int bit_field) {
     int i;
+	int loop_running;
     
-    analog_stop_loop();
+	//Check analog loop status
+	loop_running = analog_loop_running();
+    if(loop_running)
+		analog_stop_loop();
     
     for(i = 0; i < NUM_BOM_LEDS; i++) {
         if(bit_field & 0x1) {
@@ -178,7 +182,9 @@
         bit_field = bit_field >> 1;
     }
     
-    analog_start_loop();
+	//Restore analog loop status
+	if(loop_running)
+		analog_start_loop();
 }
 
 /**
