Advertisement
Guest User

Arduino Battery Capacity Tester

a guest
Mar 19th, 2011
1,460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. /*
  2. Battery capacity tester
  3. (C) 2011 Nick Hartman
  4.  
  5. This code calculates the capacity of a battery.
  6. More info can be found at www.nicko01.com.
  7. Code written for the Teensy microcontroller board.
  8. */
  9.  
  10. #include <LiquidCrystal.h>
  11. const int analogInPin = 9;
  12. const int analogRVoltage = 8;
  13. LiquidCrystal lcd(15, 14, 2, 3, 4, 5);
  14. int sensorValue = 0;
  15. int time = 0;
  16. int totalCurrent;
  17. double rVoltage;
  18. double totalMah = 0;
  19.  
  20. void setup() {
  21.   analogWrite(10,215);
  22.   lcd.begin(16,2);
  23.   lcd.clear();
  24.   lcd.setCursor(0,0);
  25.   lcd.print("Battery Tester");
  26.   lcd.setCursor(0,1);
  27.   lcd.print("Initializing...");  
  28.   delay(5000);  // gives the computer time to recognize the USB keyboard
  29.   lcd.clear();
  30.   lcd.setCursor(0,0);
  31.   lcd.print("Running test.");
  32. }
  33.  
  34. void loop() {
  35.   for(int i=0;i<60;i++) {
  36.     sensorValue = analogRead(analogInPin);
  37.     rVoltage = analogRead(analogRVoltage)/204.8;
  38.     totalCurrent = rVoltage*100;    
  39.     lcd.clear();
  40.     lcd.setCursor(0,0);
  41.     lcdPrintDouble(sensorValue/68.2666*1.01626,2); // The 1.01626 is for calibration. This is different for each setup.
  42.     lcd.print("V");
  43.     lcd.print(" T=");
  44.     lcd.print(time/60);
  45.     lcd.print(" min");
  46.     lcd.setCursor(0,1);
  47.     lcdPrintDouble(totalCurrent,0);
  48.     lcd.print("mA ");
  49.     lcdPrintDouble(totalMah,1);
  50.     lcd.print("mAh");
  51.     delay(1000);  
  52.     time++;
  53.   }  
  54.   totalMah = totalMah + totalCurrent/60.0; // Calculates one minute worth of discharge and adds to the total
  55.   sendToComputer();  
  56. }
  57.  
  58.  void lcdPrintDouble( double val, byte precision){
  59.   // prints val on a ver 0012 text lcd with number of decimal places determine by precision
  60.   // precision is a number from 0 to 6 indicating the desired decimial places
  61.   // example: printDouble( 3.1415, 2); // prints 3.14 (two decimal places)
  62.   // note: not my code
  63.  
  64.   /* Not needed in my code
  65.   if(val < 0.0){
  66.     lcd.print('-');
  67.     val = -val;
  68.   }
  69.   */
  70.  
  71.   lcd.print (int(val));  //prints the int part
  72.   if( precision > 0) {
  73.     lcd.print("."); // print the decimal point
  74.     unsigned long frac;
  75.     unsigned long mult = 1;
  76.     byte padding = precision -1;
  77.     while(precision--)
  78.   mult *=10;
  79.  
  80.     if(val >= 0)
  81.  frac = (val - int(val)) * mult;
  82.     else
  83.  frac = (int(val)- val ) * mult;
  84.     unsigned long frac1 = frac;
  85.     while( frac1 /= 10 )
  86.  padding--;
  87.     while(  padding--)
  88.  lcd.print("0");
  89.     lcd.print(frac,DEC) ;
  90.   }
  91. }
  92.  
  93. void sendToComputer( ) {      // insert the data into a spreadsheet on the connected computer
  94.   Keyboard.print(sensorValue/68.2666*1.01626);
  95.  
  96.   Keyboard.set_key1(KEY_RIGHT);
  97.   Keyboard.send_now();
  98.   Keyboard.set_key1(0);
  99.   Keyboard.send_now();
  100.  
  101.   Keyboard.print(totalMah);
  102.  
  103.   Keyboard.set_key1(KEY_RIGHT);
  104.   Keyboard.send_now();
  105.   Keyboard.set_key1(0);
  106.   Keyboard.send_now();
  107.  
  108.   Keyboard.print(totalCurrent);
  109.  
  110.   Keyboard.set_key1(KEY_DOWN);
  111.   Keyboard.send_now();
  112.   Keyboard.set_key1(0);
  113.   Keyboard.send_now();
  114.  
  115.   Keyboard.set_key1(KEY_LEFT);
  116.   Keyboard.send_now();
  117.   Keyboard.set_key1(0);
  118.   Keyboard.send_now();
  119.  
  120.   Keyboard.set_key1(KEY_LEFT);
  121.   Keyboard.send_now();
  122.   Keyboard.set_key1(0);
  123.   Keyboard.send_now();
  124.  
  125.   Keyboard.set_modifier(MODIFIERKEY_CTRL);
  126.   Keyboard.set_key1(KEY_S);
  127.   Keyboard.send_now();
  128.   Keyboard.set_modifier(0);
  129.   Keyboard.set_key1(0);
  130.   Keyboard.send_now();
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement