/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
#include <bluefruit.h>
const char TestString[12] = “Test String”;
// XIAO VBAT Measurement
//
// Set the GPIO PIN and ADC configuration for VBat measurements
void SetVBatMeasurement() {
pinMode(PIN_VBAT, INPUT); //Battery Voltage monitoring pin
pinMode(VBAT_ENABLE, OUTPUT); //Enable Battery Voltage monitoring pin
digitalWrite(VBAT_ENABLE, LOW); //Enable
analogReference(AR_INTERNAL_2_4); //Vref=2.4V
analogReadResolution(12); //12bits
}
// Perform a VBat voltage Read
#define VBatAdj (3.970 / 3.892) // 2% Uncertainty: in specifications
float ReadVbat() {
// Read VBAT voltage
pinMode(VBAT_ENABLE, OUTPUT); //Enable Battery Voltage monitoring pin
digitalWrite(VBAT_ENABLE, LOW); //Enable
digitalWrite(LED_RED, LOW); // turn the LED on (HIGH is the voltage level)
delay(200); // To be sure the pin voltage is stable
int VBatMin = 4096;
int VBatMax = 0;
int VBat = 0;
for (int i = 0; i < 10; i++) {
int v = analogRead(PIN_VBAT);
VBatMin = min(VBatMin, v);
VBatMax = max(VBatMax, v);
VBat += v;
delay(50);
}
VBat -= VBatMin; // Remove min ADC value
VBat -= VBatMax; // Remove max ADC value
VBat /= 8;
pinMode(VBAT_ENABLE, INPUT); //Disable Battery Voltage monitoring pin
digitalWrite(LED_RED, HIGH); // turn the LED off by making the voltage LOW
float Vbattery = ((510e3 + 1000e3) / 510e3) * 2.4 * VBat / 4096;
return (Vbattery * VBatAdj);
}
// Convert VBat in %
int ConvertVBatInPercent(float v) {
long int iv = (long int)(v * 1000.0);
long int val = map(iv, 3100, 4100, 0, 100);
return ((int)val);
}
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(115200);
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BLUE, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
delay(5000);
Serial.println("---- TEST XIAO");
// TestString = “TEST String”;
#define HICHG (13)
// pinMode(HICHG, OUTPUT); //Charge Current setting pin
// digitalWrite(HICHG, HIGH); //Charge Current 0:100mA, 1:50mA
SetVBatMeasurement();
Serial.println("- RUN LED -");
digitalWrite(LED_BLUE, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BLUE, HIGH); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(LED_GREEN, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_GREEN, HIGH); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(LED_RED, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_RED, HIGH); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
// the loop function runs over and over again forever
void loop() {
float vb = ReadVbat();
Serial.print(vb * 100);
Serial.print(F(", "));
Serial.println(ConvertVBatInPercent(vb));
delay(700);
Serial.println(TestString);
}