Advertisement
pleasedontcode

Battery Monitor rev_06

Sep 27th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Battery Monitor
  13.     - Source Code compiled for: Arduino Pro Mini 3.3V
  14.     - Source Code created on: 2024-09-27 22:49:28
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* read and provide pot value via wifi */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Arduino.h>  // Include Arduino library for basic functions
  25.  
  26. /*
  27.  *  18650 Ion-Li battery charge
  28.  */
  29.  
  30. #ifndef Pangodream_18650_CL_h
  31. #define Pangodream_18650_CL_h
  32.  
  33. #include "Arduino.h"
  34.  
  35. #define DEF_PIN 34 // This pin is not compatible with Arduino Pro Mini 3.3V
  36. #define DEF_CONV_FACTOR 1.7
  37. #define DEF_READS 20
  38.  
  39. /*
  40.  * 18650 Ion-Li battery charge
  41.  * Calculates charge level of an 18650 Ion-Li battery
  42.  */
  43. class Pangodream_18650_CL {    
  44.   public:  
  45.    
  46.     // Constructor with addressPin
  47.     Pangodream_18650_CL(int addressPin) {
  48.       _addressPin = addressPin;
  49.       _convFactor = DEF_CONV_FACTOR;
  50.       _reads = DEF_READS;
  51.       _initVoltsArray();
  52.     }
  53.    
  54.     // Constructor with addressPin and convFactor
  55.     Pangodream_18650_CL(int addressPin, double convFactor) {
  56.       _addressPin = addressPin;
  57.       _convFactor = convFactor;
  58.       _reads = DEF_READS;
  59.       _initVoltsArray();
  60.     }
  61.    
  62.     // Constructor with addressPin, convFactor, and reads
  63.     Pangodream_18650_CL(int addressPin, double convFactor, int reads) {
  64.       _addressPin = addressPin;
  65.       _convFactor = convFactor;
  66.       _reads = reads;
  67.       _initVoltsArray();
  68.     }
  69.    
  70.     // Default constructor
  71.     Pangodream_18650_CL() {
  72.       _addressPin = DEF_PIN;
  73.       _convFactor = DEF_CONV_FACTOR;
  74.       _reads = DEF_READS;
  75.       _initVoltsArray();
  76.     }
  77.  
  78.     // Get the battery charge level (0-100)
  79.     int getBatteryChargeLevel() {
  80.       double volts = getBatteryVolts();
  81.       return _getChargeLevel(volts);
  82.     }
  83.  
  84.     // Get the battery voltage
  85.     double getBatteryVolts() {
  86.       int total = 0;
  87.       for (int i = 0; i < _reads; i++) {
  88.         total += _analogRead(_addressPin);
  89.       }
  90.       return _analogReadToVolts(total / _reads);
  91.     }
  92.  
  93.     int getAnalogPin() {
  94.       return _addressPin;
  95.     }
  96.  
  97.     int pinRead() {
  98.       return analogRead(_addressPin);
  99.     }
  100.  
  101.     double getConvFactor() {
  102.       return _convFactor;
  103.     }
  104.        
  105.   private:
  106.  
  107.     int    _addressPin;               //!< ADC pin used, default is GPIO34 - ADC1_6
  108.     int    _reads;                    // Number of reads of ADC pin to calculate an average value
  109.     double _convFactor;               //!< Conversion factor to translate analog units to volts
  110.     double _vs[101];                  // Array with voltage - charge definitions
  111.    
  112.     void   _initVoltsArray() {
  113.       // Initialize the voltage to charge level mapping
  114.       for (int i = 0; i <= 100; i++) {
  115.         _vs[i] = (i / 100.0) * 4.2; // Assuming 4.2V is the max voltage for a fully charged Li-Ion battery
  116.       }
  117.     }
  118.  
  119.     int    _getChargeLevel(double volts) {
  120.       // Determine the charge level based on voltage
  121.       for (int i = 0; i <= 100; i++) {
  122.         if (volts < _vs[i]) {
  123.           return i - 1; // Return the charge level
  124.         }
  125.       }
  126.       return 100; // If voltage is above the max, return 100%
  127.     }
  128.  
  129.     int    _analogRead(int pinNumber) {
  130.       return analogRead(pinNumber);
  131.     }
  132.  
  133.     double _analogReadToVolts(int readValue) {
  134.       return (readValue / 1023.0) * _convFactor; // Convert analog read value to volts
  135.     }
  136. };
  137.  
  138. #endif
  139.  
  140. /****** FUNCTION PROTOTYPES *****/
  141. void setup(void);
  142. void loop(void);
  143.  
  144. /***** DEFINITION OF ANALOG INPUT PINS *****/
  145. const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;
  146.  
  147. // Instantiate the battery charge level class
  148. Pangodream_18650_CL batteryMonitor(pot_Potentiometer_Vout_PIN_A0); // Create an instance of the battery monitor class
  149.  
  150. void setup(void)
  151. {
  152.     // Initialize serial communication
  153.     Serial.begin(115200);
  154.    
  155.     // Set the analog input pin mode
  156.     pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
  157. }
  158.  
  159. void loop(void)
  160. {
  161.     // Read the potentiometer value
  162.     int potValue = analogRead(pot_Potentiometer_Vout_PIN_A0);
  163.    
  164.     // Calculate the battery voltage from the potentiometer value
  165.     double batteryVoltage = batteryMonitor.getBatteryVolts();
  166.  
  167.     // Print the potentiometer value and battery voltage to the serial monitor
  168.     Serial.print("Potentiometer Value: ");
  169.     Serial.print(potValue);
  170.     Serial.print(" | Battery Voltage: ");
  171.     Serial.println(batteryVoltage);
  172.  
  173.     // Add a delay to avoid flooding the serial output
  174.     delay(1000);  // Delay for 1 second
  175. }
  176.  
  177. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement