Advertisement
sspence65

Coulomb Counting

Jun 26th, 2018
3,340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement