DE-ACCM2G2 accelerometer solution

Hello, all. I have tested this function with my solar panel vertical axis and it returns 0 to 90 degrees with the DE-ACCM2G2 accelerometer from Dimension Engineering. In order to get it working, I had to orient the module at about a 20 degree angle from vertical, otherwise readings were erroneous. Although the module is 2-axis, I only needed the Y-axis, thus only 3 connections were used as indicated below.

scottfromscott

float get_panel_altitude(){
	/*
		read raw voltage on channel 3, then convert it to degrees 0..90 using pin Y only
		see: https://www.pololu.com/docs/0J18/2   for explanation of analog functions
		see: http://www.dimensionengineering.com/DE-ACCM2G2.htm for DE-ACCM2G2 specs

		Baby O  <-> DE-ACCM2G2 <-> 7805	<->	Battery (14AH, AGM)

		Pc3-------------Y				Vin---------+12V
		vin------------Vin--------5Vout
		Gnd------------Gnd---------Gnd----------Gnd
		

     DE-ACCM2G2
   VCC+-----+GND
      |     |
      |     |        ^
      |     |        | Y-axis, -> X-axis
      |     |
     Y+-----+X
	*/

	float de_accm2g2_mv;
   unsigned char calibrated;
  	
	//set_analog_mode(MODE_10_BIT); // maximum resolution (1024 steps)
	set_millivolt_calibration(read_vcc_millivolts()); // recommended in docs
   de_accm2g2_mv = analog_read_average_millivolts(3, 100); // get average of accelerometer values
   
   calibrated = 1; // set calibrated to 0 for raw millivolt output during calibration
                   // set calibrated to 1 to return degrees after calibration 

   if (calibrated)
   {
      // NOTE: table endpoints of 990 and 2310 are min and max millivolts de_accm2g2 is capable of -- do not remove from table
      //       with calibrated set to 0, take readings every 10 degrees starting with 0; and ending with 90
      //       using a protractor or similar angle measurement device.
      //       place these mv readings between the 990, and 2310 in the de_accm2g2_mv_tbl[] lookup table 
      //       then set calbrated to 1 and test your new degree readings against the protractor.

      //             Degrees:     (-10),   0,  10,  20,  30,  40,  50,  60,  70,  80,  90,(100)  
      float de_accm2g2_mv_tbl[]  = {990,1051,1105,1187,1283,1375,1491,1604,1723,1819,1924,2310};
      int i=0;
      float de_accm2g2_deg;
      while (de_accm2g2_mv >= de_accm2g2_mv_tbl[i+1]) i++;
      de_accm2g2_deg = (de_accm2g2_mv - de_accm2g2_mv_tbl[i]) / (de_accm2g2_mv_tbl[i+1] - de_accm2g2_mv_tbl[i]) * 10 + 10 * (i-1);
      return de_accm2g2_deg;   
   }
   else return de_accm2g2_mv;
}