Advertisement
pleasedontcode

Battery Monitor rev_05

Sep 27th, 2024
54
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 NOT compiled for: Arduino Pro Mini 3.3V
  14.     - Source Code created on: 2024-09-27 22:44:47
  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 <WiFiPicker.h>  // https://github.com/Tvde1/WiFiPicker
  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.     /*
  47.     * Constructor
  48.     * @param addressPin, ADC pin number where the voltage divider is connected to
  49.     */
  50.     Pangodream_18650_CL(int addressPin);
  51.    
  52.     /*
  53.     * Constructor
  54.     * @param addressPin, ADC pin number where the voltage divider is connected to
  55.     * @param convFactor, Conversion factor for analog read units to volts
  56.     */
  57.     Pangodream_18650_CL(int addressPin, double convFactor);
  58.    
  59.     /*
  60.     * Constructor
  61.     * @param addressPin, ADC pin number where the voltage divider is connected to
  62.     * @param convFactor, Conversion factor for analog read units to volts
  63.     * @param reads, Number of reads of analog pin to calculate an average value
  64.     */
  65.     Pangodream_18650_CL(int addressPin, double convFactor, int reads);
  66.    
  67.     /*
  68.     * Constructor
  69.     */
  70.     Pangodream_18650_CL();    
  71.  
  72.     /*
  73.      * Get the battery charge level (0-100)
  74.      * @return The calculated battery charge level
  75.      */
  76.     int getBatteryChargeLevel();
  77.     double getBatteryVolts();
  78.     int getAnalogPin();
  79.     int pinRead();
  80.     double getConvFactor();
  81.        
  82.   private:
  83.  
  84.     int    _addressPin;               //!< ADC pin used, default is GPIO34 - ADC1_6
  85.     int    _reads;                    // Number of reads of ADC pin to calculate an average value
  86.     double _convFactor;               //!< Conversion factor to translate analog units to volts
  87.     double _vs[101];                  // Array with voltage - charge definitions
  88.    
  89.     void   _initVoltsArray();
  90.     int    _getChargeLevel(double volts);
  91.     int    _analogRead(int pinNumber);
  92.     double _analogReadToVolts(int readValue);
  93.    
  94. };
  95.  
  96. #endif
  97.  
  98. /****** FUNCTION PROTOTYPES *****/
  99. void setup(void);
  100. void loop(void);
  101.  
  102. /***** DEFINITION OF ANALOG INPUT PINS *****/
  103. const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;
  104.  
  105. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  106. // Instantiate the WiFiPicker object
  107. WiFiPicker wifiPicker;  // Create an instance of the WiFiPicker class
  108.  
  109. // Instantiate the battery charge level class
  110. Pangodream_18650_CL batteryMonitor(pot_Potentiometer_Vout_PIN_A0); // Create an instance of the battery monitor class
  111.  
  112. void setup(void)
  113. {
  114.     // Initialize serial communication
  115.     Serial.begin(115200);
  116.    
  117.     // Start the WiFiPicker service
  118.     wifiPicker.start();  
  119.  
  120.     // Print the local IP address
  121.     Serial.print("Connected! Ip: ");
  122.     Serial.println(WiFi.localIP());
  123.  
  124.     // Set the analog input pin mode
  125.     pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
  126. }
  127.  
  128. void loop(void)
  129. {
  130.     // Read the potentiometer value
  131.     int potValue = analogRead(pot_Potentiometer_Vout_PIN_A0);
  132.    
  133.     // Calculate the battery voltage from the potentiometer value
  134.     double batteryVoltage = batteryMonitor.getBatteryVolts();
  135.  
  136.     // Print the potentiometer value and battery voltage to the serial monitor
  137.     Serial.print("Potentiometer Value: ");
  138.     Serial.print(potValue);
  139.     Serial.print(" | Battery Voltage: ");
  140.     Serial.println(batteryVoltage);
  141.  
  142.     // Add a delay to avoid flooding the serial output
  143.     delay(1000);  // Delay for 1 second
  144. }
  145.  
  146. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement