Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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 compiled for: Arduino Pro Mini 3.3V
- - Source Code created on: 2024-09-27 22:49:28
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* read and provide pot value via wifi */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Arduino.h> // Include Arduino library for basic functions
- /*
- * 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 with addressPin
- Pangodream_18650_CL(int addressPin) {
- _addressPin = addressPin;
- _convFactor = DEF_CONV_FACTOR;
- _reads = DEF_READS;
- _initVoltsArray();
- }
- // Constructor with addressPin and convFactor
- Pangodream_18650_CL(int addressPin, double convFactor) {
- _addressPin = addressPin;
- _convFactor = convFactor;
- _reads = DEF_READS;
- _initVoltsArray();
- }
- // Constructor with addressPin, convFactor, and reads
- Pangodream_18650_CL(int addressPin, double convFactor, int reads) {
- _addressPin = addressPin;
- _convFactor = convFactor;
- _reads = reads;
- _initVoltsArray();
- }
- // Default constructor
- Pangodream_18650_CL() {
- _addressPin = DEF_PIN;
- _convFactor = DEF_CONV_FACTOR;
- _reads = DEF_READS;
- _initVoltsArray();
- }
- // Get the battery charge level (0-100)
- int getBatteryChargeLevel() {
- double volts = getBatteryVolts();
- return _getChargeLevel(volts);
- }
- // Get the battery voltage
- double getBatteryVolts() {
- int total = 0;
- for (int i = 0; i < _reads; i++) {
- total += _analogRead(_addressPin);
- }
- return _analogReadToVolts(total / _reads);
- }
- int getAnalogPin() {
- return _addressPin;
- }
- int pinRead() {
- return analogRead(_addressPin);
- }
- double getConvFactor() {
- return _convFactor;
- }
- 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() {
- // Initialize the voltage to charge level mapping
- for (int i = 0; i <= 100; i++) {
- _vs[i] = (i / 100.0) * 4.2; // Assuming 4.2V is the max voltage for a fully charged Li-Ion battery
- }
- }
- int _getChargeLevel(double volts) {
- // Determine the charge level based on voltage
- for (int i = 0; i <= 100; i++) {
- if (volts < _vs[i]) {
- return i - 1; // Return the charge level
- }
- }
- return 100; // If voltage is above the max, return 100%
- }
- int _analogRead(int pinNumber) {
- return analogRead(pinNumber);
- }
- double _analogReadToVolts(int readValue) {
- return (readValue / 1023.0) * _convFactor; // Convert analog read value to volts
- }
- };
- #endif
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t pot_Potentiometer_Vout_PIN_A0 = A0;
- // 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);
- // 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 */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement