# program to assist in the calibration of one axis of adxl335 
# - triple-axis accelerometer running at Vcc = 3.3V and
# connected to edison arduino board with ADC reference set to 3.3V
# this program uses libmraa. Make sure that MRAA on edison is updated 
# to the latest version. 

import math
import mraa

try:
    # set pin labeled "A2" on the edison arduino board as analog input
    x_out = mraa.Aio(2)
except:
    print("you don't have an ADC")

# MRAA gives the option of setting the bit precision for the analog value read by ADC
# since Edison arduino board has 12bit ADC, precision is set to 12 bits below.
x_out.setBit(12)

# read the accelerometer x-axis digital value
def read_accelerometer_datum():
	return x_out.read()

def digital_to_analog(adc_reading):
	#calculate the full digital range that the 12bit ADC reading can have
        full_digital_range = pow(2, 12) - 1

        # convert digital value back to voltage. Assumes ADC reference voltage = 3.3V
        # follow the jumper settings described previously to ser reference voltage to 3.3V
        analog_voltage = math.floor(adc_reading * 3.3 / full_digital_range)
	return analog_voltage

# Get the average of 3 accelerometer readings
ax_analog = 0.0
for i in range(3):
	ax_digital = read_accelerometer_datum()
	ax_analog = ax_analog + digital_to_analog(ax_digital)
ax_analog = ax_analog / 3.0

# print out the voltage reading. This reading is used for offset and Sensitivity calc
print "Voltage reading from the accelerometer = ", ax_analog

