Advertisement
pleasedontcode

Battery Monitor rev_07

Sep 27th, 2024
58
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:57:29
  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 // 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. // Instantiate the WiFiPicker object
  107. WiFiPicker wifiPicker;  // Create an instance of the WiFiPicker class
  108.  
  109. void setup(void)
  110. {
  111.     // Initialize serial communication
  112.     Serial.begin(115200);
  113.    
  114.     // Start the WiFiPicker service
  115.     wifiPicker.start();
  116.    
  117.     // Print the local IP address
  118.     Serial.print("Connected! Ip: ");
  119.     Serial.println(WiFi.localIP());
  120.  
  121.     // Set the potentiometer pin as input
  122.     pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
  123. }
  124.  
  125. void loop(void)
  126. {
  127.     // Read the potentiometer value
  128.     int potValue = analogRead(pot_Potentiometer_Vout_PIN_A0);  // Read the analog value from the potentiometer
  129.    
  130.     // Print the potentiometer value to the serial monitor
  131.     Serial.print("Potentiometer Value: ");
  132.     Serial.println(potValue);
  133.    
  134.     // Add a small delay to avoid flooding the serial output
  135.     delay(1000);  // Delay for 1 second
  136. }
  137.  
  138. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement