Skip to content

Instantly share code, notes, and snippets.

@krobro
Last active August 29, 2015 14:16
Show Gist options
  • Save krobro/d5650ebbff077c67392f to your computer and use it in GitHub Desktop.
Save krobro/d5650ebbff077c67392f to your computer and use it in GitHub Desktop.
//
// BatteryStatusSerial
//
// A simple test script that prints the battery pack voltage to the
// serial monitor on your computer.
//
// NOTES:
// 1 - this is the table of actual voltage and sensor readings
//
// Battery Voltage | sensorValue
// 4.7 | 773
// 3.2 | 578
// 1.6 | 320
// 0 | 0
// run once, when the sketch starts
void setup()
{
// setup Serial library at 9600 bps
Serial.begin(9600);
}
// run over and over again
void loop()
{
// read battery sensor value and map it to 0-5 volts
int sensorValue = analogRead(BATTERY); // read battery value
int mappedSensorValue = map(sensorValue, 0, 800, 0, 5);
// Print battery value out
Serial.print("Battery reads: ");
Serial.print(mappedSensorValue);
Serial.print(" Volts (");
Serial.print(sensorValue);
Serial.println(")");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment