CCCPaste

Battery Monitor rev_05

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Battery Monitor
	- Source Code NOT compiled for: Arduino Pro Mini 3.3V
	- Source Code created on: 2024-09-27 22:44:47

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* read and provide pot value via wifi */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include   // https://github.com/Tvde1/WiFiPicker

/* 
 *  18650 Ion-Li battery charge
 */
  
#ifndef Pangodream_18650_CL_h
#define Pangodream_18650_CL_h

#include "Arduino.h"

#define DEF_PIN 34 // This pin is not compatible with Arduino Pro Mini 3.3V
#define DEF_CONV_FACTOR 1.7
#define DEF_READS 20

/*
 * 18650 Ion-Li battery charge
 * Calculates charge level of an 18650 Ion-Li battery
 */
class Pangodream_18650_CL {    
  public:  
    
    /*
    * Constructor
    * @param addressPin, ADC pin number where the voltage divider is connected to
    */
    Pangodream_18650_CL(int addressPin);
    
    /*
    * Constructor
    * @param addressPin, ADC pin number where the voltage divider is connected to
    * @param convFactor, Conversion factor for analog read units to volts
    */
    Pangodream_18650_CL(int addressPin, double convFactor);
    
    /*
    * Constructor
    * @param addressPin, ADC pin number where the voltage divider is connected to
    * @param convFactor, Conversion factor for analog read units to volts
    * @param reads, Number of reads of analog pin to calculate an average value
    */
    Pangodream_18650_CL(int addressPin, double convFactor, int reads);
    
    /*
    * Constructor
    */
    Pangodream_18650_CL();    

    /*
     * Get the battery charge level (0-100)
     * @return The calculated battery charge level
     */
    int getBatteryChargeLevel();
    double getBatteryVolts();
    int getAnalogPin();
    int pinRead();
    double getConvFactor();
       
  private:

    int    _addressPin;               //!< ADC pin used, default is GPIO34 - ADC1_6
    int    _reads;                    // Number of reads of ADC pin to calculate an average value
    double _convFactor;               //!< Conversion factor to translate analog units to volts
    double _vs[101];                  // Array with voltage - charge definitions
    
    void   _initVoltsArray();
    int    _getChargeLevel(double volts);
    int    _analogRead(int pinNumber);
    double _analogReadToVolts(int readValue);
    
};

#endif

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Instantiate the WiFiPicker object
WiFiPicker wifiPicker;  // Create an instance of the WiFiPicker class

// Instantiate the battery charge level class
Pangodream_18650_CL batteryMonitor(pot_Potentiometer_Vout_PIN_A0); // Create an instance of the battery monitor class

void setup(void)
{
    // Initialize serial communication
    Serial.begin(115200);
    
    // Start the WiFiPicker service
    wifiPicker.start();  

    // Print the local IP address
    Serial.print("Connected! Ip: ");
    Serial.println(WiFi.localIP());

    // Set the analog input pin mode
    pinMode(pot_Potentiometer_Vout_PIN_A0, INPUT);
}

void loop(void)
{
    // Read the potentiometer value
    int potValue = analogRead(pot_Potentiometer_Vout_PIN_A0);
    
    // Calculate the battery voltage from the potentiometer value
    double batteryVoltage = batteryMonitor.getBatteryVolts();

    // Print the potentiometer value and battery voltage to the serial monitor
    Serial.print("Potentiometer Value: ");
    Serial.print(potValue);
    Serial.print(" | Battery Voltage: ");
    Serial.println(batteryVoltage);

    // Add a delay to avoid flooding the serial output
    delay(1000);  // Delay for 1 second
}

/* END CODE */