Wrote this over the summer when playing around with the edu rc. It prints out a float
Code:
current_voltage = rxdata.rc_main_batt; // Store current voltage
average_voltage += current_voltage; // Add the current voltage to the average stack
voltage_count++; // Increase the average stack count
if (voltage_count == 39) // If the stack is 40
{
average_voltage = (average_voltage / 40) * 0.038 + 0.05; // Get the average and apply formula
i = (int)average_voltage; // Truncate decimals
i2 = (int) ((average_voltage - i) * 1000); // Subtract to get decimals only and then multiply by 1000
printf("Average: %d.%d \n",i,i2); // Print the two together to make it look like a decimal
average_voltage = 0; // Reset the stack
voltage_count = 0; // Rest the counter
}