serial_debugger/serial_debugger.ino

69 lines
1.8 KiB
C++

// Varous system includes
#include <Arduino.h>
// Various library includes
#include <Adafruit_NeoPixel.h>
// Debugging via serial monitor (don't turn this on unless you're hacking on the firmware code)
#define DEBUG true
// Battery level measurement
#define VBATPIN A6
float measuredVBat;
float batteryPercent;
// NeoPixels
#define NUMPIXELS 1
Adafruit_NeoPixel pixels(NUMPIXELS, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
void setup() {
// Setup red LED to indicate device is on (in case we disable NeoPixel battery level later)
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
// Setup NeoPixels
pixels.begin();
pixels.clear();
pixels.setBrightness(10);
// Green : pixels.Color(0, 255, 0)
// Yellow : pixels.Color(255, 255, 0)
// Orange : pixels.Color(255, 128, 0)
// Red : pixels.Color(255, 0, 0)
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
pixels.show();
}
// Measure battery level and change NeoPixel accordingly
void batteryLevel() {
measuredVBat = analogRead(VBATPIN);
measuredVBat *= 2; // we divided by 2, so multiply back
measuredVBat *= 3.3; // Multiply by 3.3V, our reference voltage
measuredVBat /= 1024; // convert to voltage
batteryPercent = measuredVBat / 3.3;
if (batteryPercent >= 0.75) {
pixels.setPixelColor(0, pixels.Color(0, 255, 0));
}
else if (batteryPercent >= 0.50) {
pixels.setPixelColor(0, pixels.Color(255, 255, 0));
}
else if (batteryPercent >= 0.25) {
pixels.setPixelColor(0, pixels.Color(255, 128, 0));
}
else {
pixels.setPixelColor(0, pixels.Color(255, 0, 0));
}
pixels.show();
if (DEBUG) {
Serial.print("VBat: " ); Serial.println(measuredVBat);
Serial.print("Bat %: "); Serial.println(batteryPercent);
}
}
void loop() {
// Update battery level as appropriate
batteryLevel();
delay(250);
}