Hi, I have the following setup:
- SparkFun Artemis Thing Plus
https://www.sparkfun.com/products/15574
1.1 3.7V 522332 400mAh Lipo battery Rechargeable Lithium Polymer ion Battery Pack with PH2.0mm JST Connector
https://www.amazon.com/Battery-Recharge … B07BTWMM8H
- SparkFun Photodetector Breakout - MAX30101 (Qwiic)
https://www.sparkfun.com/products/16474
- SparkFun Micro OLED Breakout (Qwiic)
https://www.sparkfun.com/products/22495
The Artemis Thing Plus is the main board, the Lipo battery is connected to the JST connector, and it works perfectly. The MAX30101 is a light sensor, and the Micro OLED Breakout is the display. All parts work beautifully together, BUT now I want to add to my code to read the battery level. The battery works perfectly for ours, and it is charged via USB-C.
I want to see the battery level percentage displayed on my OLED.
This is my current code:
#include <SFE_MicroOLED.h>
#include <Wire.h>
#include "MAX30105.h"
#define PIN_RESET 9
#define DC_JUMPER 1
MAX30105 particleSensor;
MicroOLED oled(PIN_RESET, DC_JUMPER);
long unblockedValue; // Average IR at power up
String multiplyChar(char c, int n) {
String result = "";
for (int i = 0; i < n; i++) {
result += c;
}
return result;
}
void displayMeasurement(int rLevel) {
oled.clear(PAGE);
oled.setCursor(0, 0);
int calibratedReading = f(rLevel);
int centerPadding = 4 - String(calibratedReading).length();
String paddingText = multiplyChar(' ', centerPadding);
if (rLevel == 0) {
oled.setFontType(1);
oled.print("Please load sample!");
oled.display();
return;
}
oled.setFontType(3);
oled.print(paddingText);
oled.print(calibratedReading);
Serial.println("real:" + String(rLevel));
Serial.println("agtron:" + String(calibratedReading));
Serial.println("===========================");
oled.display();
}
int f(int x) {
int intersectionPoint = 117;
float deviation = 0.165;
return round(x - (intersectionPoint - x) * deviation);
}
void setup() {
Serial.begin(9600);
Wire.begin();
oled.begin(); // Initialize the OLED
oled.clear(ALL); // Clear the display's internal memory
oled.clear(PAGE); // Clear the buffer.
delay(100); // Delay 100 ms
oled.setFontType(3);
// Initialize sensor
if (particleSensor.begin(Wire, I2C_SPEED_FAST) == false) // Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1)
;
}
// The variable below calibrates the LED output on your hardware.
byte ledBrightness = 135;
byte sampleAverage = 4; // Options: 1, 2, 4, 8, 16, --32--
byte ledMode = 2; // Options: 1 = Red only, --2 = Red + IR--, 3 = Red + IR + Green
int sampleRate = 50; // Options: 50, 100, 200, 400, 800, 1000, 1600, --3200--
int pulseWidth = 411; // Options: 69, 118, 215, --411--
int adcRange = 16384; // Options: 2048, --4096--, 8192, 16384
particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); // Configure sensor with these settings
particleSensor.setPulseAmplitudeRed(0);
particleSensor.setPulseAmplitudeGreen(0);
particleSensor.disableSlots();
particleSensor.enableSlot(2, 0x02); // Enable only SLOT_IR_LED = 0x02
// Update to ignore readings under 30.000
unblockedValue = 30000;
}
void loop() {
int rLevel = particleSensor.getIR();
long currentDelta = rLevel - unblockedValue;
if (currentDelta > (long)100) {
displayMeasurement(rLevel / 1000);
} else {
displayMeasurement(0);
}
delay(100);
}
Please, anything would help. I’ve read about this, but I’m unsure how to proceed. If it helps, forget about what the light sensor does. I want to be able to display the battery level percentage.