Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Coulomb Counting

sspence65 Jun 26th, 2018 (edited) 150 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1.  
  2. unsigned long startMillis;  //some global variables available anywhere in the program
  3. unsigned long currentMillis;
  4. const int period = 1000;  //the value is a number of milliseconds
  5. const byte ledPin = 13;    //using the built in LED
  6. float current = .5;
  7. float totalCoulumbs = 0.0;
  8.  
  9. void setup()
  10. {
  11.   Serial.begin(115200);  //start Serial in case we need to print debugging info
  12.   pinMode(ledPin, OUTPUT);
  13.   startMillis = millis();  //initial start time
  14. }
  15.  
  16. void loop()
  17. {
  18.   currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  19.   if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  20.   {
  21.     totalCoulumbs = totalCoulumbs + current;
  22.     Serial.print("Total Coulumbs = ");
  23.     Serial.println(totalCoulumbs);
  24.     Serial.print("Total Ah = ");
  25.     Serial.println(totalCoulumbs/3600.0);
  26.     digitalWrite(ledPin, !digitalRead(ledPin));  //if so, change the state of the LED.  Uses a neat trick to change the state
  27.     startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  28.   }
  29. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top