Simvik

Battery percentage

Map a battery voltage in millivolts to 0-100 percent. An easy embedded C problem (Integers and data types), run and graded in the browser.

easy · Integers and data types · scaling · clamp

A Li-ion cell is treated as empty at 3000 mV and full at 4200 mV. Return the charge level as a percentage, assuming it is linear in between.

Round down, and clamp readings outside the range to 0 or 100.

Starter code

uint8_t battery_percent(uint16_t mv) {
  // 3000 mV -> 0, 4200 mV -> 100, linear in between, rounded down.
  return 0;
}

Example tests

Midpoint

printf("%u\n", battery_percent(3600));

Expected output:

50

Full

printf("%u\n", battery_percent(4200));

Expected output:

100

Learn it first

More Integers and data types problems