Advertisement
pleasedontcode

Battery Monitor rev_08

Sep 27th, 2024
42
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:59:14
  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. // Removed WiFiPicker as it is not compatible with Arduino Pro Mini 3.3V
  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 // GPIO34 is not available on 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, Convertion 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, Convertion 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;               //!< Convertion 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. // No WiFiPicker instance as it is not compatible with Arduino Pro Mini 3.3V
  107.  
  108. void setup(void)
  109. {
  110.     // Initialize serial communication
  111.     Serial.begin(115200);
  112.    
  113.     // Print a message indicating that the setup is complete
  114.     Serial.println("Setup complete. No WiFi functionality available.");
  115.  
  116.     // Set the potentiometer pin as input
  117.     pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
  118. }
  119.  
  120. void loop(void)
  121. {
  122.     // Read the potentiometer value
  123.     int potValue = analogRead(pot_Potentiometer_Vout_PIN_A0);  // Read the analog value from the potentiometer
  124.    
  125.     // Print the potentiometer value to the serial monitor
  126.     Serial.print("Potentiometer Value: ");
  127.     Serial.println(potValue);
  128.    
  129.     // Add a small delay to avoid flooding the serial output
  130.     delay(1000);  // Delay for 1 second
  131. }
  132.  
  133. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement