# 8 Vex LEDS for a battery status indicator

Hi all,

I wanted to have 8 LED lights plugged into the cortex and use them as a battery status indicator. My plan is to have each light light up per 10 percent of battery. For example, the battery is at 50 percent, 4 lights would light up and continues to stay on. Let’s say that all of a sudden the battery is starting to die, the battery is now at 44 percent, then 3 LEDs would light up.

I would really appreciate it if I could have a small snippet of code on how to do this. we really need to see the battery percentage for competitions and etc.

Thanks for all of your help,
It’s really appreciated! Thanks!!!

You can monitor battery voltage, it’s not really battery capacity but the best we can do on the cortex.

You need to decide what the “low” value and “high” value are ie. when is the battery at full capacity and empty. Then calculate the percentage between these numbers, convert that into a smaller scale of between 0 and 7 to determine how many leds to turn on. Here is an example.

``````#pragma config(Sensor, dgtl1,  ,               sensorLEDtoVCC)
#pragma config(Sensor, dgtl2,  ,               sensorLEDtoVCC)
#pragma config(Sensor, dgtl3,  ,               sensorLEDtoVCC)
#pragma config(Sensor, dgtl4,  ,               sensorLEDtoVCC)
#pragma config(Sensor, dgtl5,  ,               sensorLEDtoVCC)
#pragma config(Sensor, dgtl6,  ,               sensorLEDtoVCC)
#pragma config(Sensor, dgtl7,  ,               sensorLEDtoVCC)
#pragma config(Sensor, dgtl8,  ,               sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

{
int batteryVoltageLow  = 6500;
int batteryVoltageHigh = 8000;
int batteryPercent;
int batteryLed;
int leds[8];
int i;

while(1) {
// figure out percent voltage between min and max.
batteryPercent = 100 * (nImmediateBatteryLevel - batteryVoltageLow) / (batteryVoltageHigh - batteryVoltageLow);

// clip to 0 to 100 range
if(batteryPercent <   0) batteryPercent =   0;
if(batteryPercent > 100) batteryPercent = 100;

// convert 0-100 to 0-7 (approximately)
batteryLed = batteryPercent / 12;

// all leds off
for(i=0;i<8;i++)
leds* = 0;

// now turn on the ones up to batteryLed;
for(i=0;i<batteryLed;i++)
leds* = 1;

// and actually turn on the leds (you could do this in a loop as well)
SensorValue dgtl1 ] = leds[0];
SensorValue dgtl2 ] = leds[1];
SensorValue dgtl3 ] = leds[2];
SensorValue dgtl4 ] = leds[3];
SensorValue dgtl5 ] = leds[4];
SensorValue dgtl6 ] = leds[5];
SensorValue dgtl7 ] = leds[6];
SensorValue dgtl8 ] = leds[7];

wait1Msec(20);
}
}
``````

**