View Single Post
  #11   Spotlight this post!  
Unread 29-01-2007, 08:10
DanDon's Avatar
DanDon DanDon is offline
ohhh MY god
AKA: Dan Hoizner
FRC #0375 (The Robotic Plague)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Staten Island, NY
Posts: 1,432
DanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond repute
Send a message via ICQ to DanDon Send a message via AIM to DanDon Send a message via MSN to DanDon
Re: Reading battery voltage in software

Quote:
Originally Posted by bear24rw View Post
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
    }

The only problem that I see with that is, if your voltage is let's say 8.004, I believe that the way you are printing the variables would print 8.4.

If I recall correctly %03d forces three digits to be printed, which using the above example, would display 8.004.
__________________