Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Battery capacity tester
- (C) 2011 Nick Hartman
- This code calculates the capacity of a battery.
- More info can be found at www.nicko01.com.
- Code written for the Teensy microcontroller board.
- */
- #include <LiquidCrystal.h>
- const int analogInPin = 9;
- const int analogRVoltage = 8;
- LiquidCrystal lcd(15, 14, 2, 3, 4, 5);
- int sensorValue = 0;
- int time = 0;
- int totalCurrent;
- double rVoltage;
- double totalMah = 0;
- void setup() {
- analogWrite(10,215);
- lcd.begin(16,2);
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Battery Tester");
- lcd.setCursor(0,1);
- lcd.print("Initializing...");
- delay(5000); // gives the computer time to recognize the USB keyboard
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Running test.");
- }
- void loop() {
- for(int i=0;i<60;i++) {
- sensorValue = analogRead(analogInPin);
- rVoltage = analogRead(analogRVoltage)/204.8;
- totalCurrent = rVoltage*100;
- lcd.clear();
- lcd.setCursor(0,0);
- lcdPrintDouble(sensorValue/68.2666*1.01626,2); // The 1.01626 is for calibration. This is different for each setup.
- lcd.print("V");
- lcd.print(" T=");
- lcd.print(time/60);
- lcd.print(" min");
- lcd.setCursor(0,1);
- lcdPrintDouble(totalCurrent,0);
- lcd.print("mA ");
- lcdPrintDouble(totalMah,1);
- lcd.print("mAh");
- delay(1000);
- time++;
- }
- totalMah = totalMah + totalCurrent/60.0; // Calculates one minute worth of discharge and adds to the total
- sendToComputer();
- }
- void lcdPrintDouble( double val, byte precision){
- // prints val on a ver 0012 text lcd with number of decimal places determine by precision
- // precision is a number from 0 to 6 indicating the desired decimial places
- // example: printDouble( 3.1415, 2); // prints 3.14 (two decimal places)
- // note: not my code
- /* Not needed in my code
- if(val < 0.0){
- lcd.print('-');
- val = -val;
- }
- */
- lcd.print (int(val)); //prints the int part
- if( precision > 0) {
- lcd.print("."); // print the decimal point
- unsigned long frac;
- unsigned long mult = 1;
- byte padding = precision -1;
- while(precision--)
- mult *=10;
- if(val >= 0)
- frac = (val - int(val)) * mult;
- else
- frac = (int(val)- val ) * mult;
- unsigned long frac1 = frac;
- while( frac1 /= 10 )
- padding--;
- while( padding--)
- lcd.print("0");
- lcd.print(frac,DEC) ;
- }
- }
- void sendToComputer( ) { // insert the data into a spreadsheet on the connected computer
- Keyboard.print(sensorValue/68.2666*1.01626);
- Keyboard.set_key1(KEY_RIGHT);
- Keyboard.send_now();
- Keyboard.set_key1(0);
- Keyboard.send_now();
- Keyboard.print(totalMah);
- Keyboard.set_key1(KEY_RIGHT);
- Keyboard.send_now();
- Keyboard.set_key1(0);
- Keyboard.send_now();
- Keyboard.print(totalCurrent);
- Keyboard.set_key1(KEY_DOWN);
- Keyboard.send_now();
- Keyboard.set_key1(0);
- Keyboard.send_now();
- Keyboard.set_key1(KEY_LEFT);
- Keyboard.send_now();
- Keyboard.set_key1(0);
- Keyboard.send_now();
- Keyboard.set_key1(KEY_LEFT);
- Keyboard.send_now();
- Keyboard.set_key1(0);
- Keyboard.send_now();
- Keyboard.set_modifier(MODIFIERKEY_CTRL);
- Keyboard.set_key1(KEY_S);
- Keyboard.send_now();
- Keyboard.set_modifier(0);
- Keyboard.set_key1(0);
- Keyboard.send_now();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement