Skip to content

Instantly share code, notes, and snippets.

@krobro
Last active August 29, 2015 14:16
Show Gist options
  • Save krobro/80bd60e660ac83bd0087 to your computer and use it in GitHub Desktop.
Save krobro/80bd60e660ac83bd0087 to your computer and use it in GitHub Desktop.
//
// BatteryStatus
//
// A simple script that displays green when the battery
// voltage is ok and red when it is getting low.
//
// NOTES:
// 1 - this is the table of actual battery pack
// voltages and analog sensor readings
//
// Battery Voltage | sensorValue
// 4.7 | 773
// 3.2 | 578
// 1.6 | 320
// 0 | 0
#define GREEN_LED 1
#define RED_LED 0
// run once, when the sketch starts
void setup()
{
// setup the RED and GREEN LED's
pinMode(BILED, OUTPUT);
}
// run over and over again
void loop() {
// read battery sensor value and map it to 0-5 volts
int sensorValue = analogRead(BATTERY);
int mappedSensorValue = map(sensorValue, 0, 800, 0, 5);
if (mappedSensorValue < 3)
{
// battery is low ... show red
digitalWrite(BILED, RED_LED);
} else {
// battery is ok ... show green
digitalWrite(BILED, GREEN_LED);
}
// pause for 1 sec
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment