Code for battery level?

Is there any sample code that can be used in a sketch to indicate battery levels?

For a project we are working on we want to have 3 LED’s Green, Yellow, Red as a visual indicator of the battery status. So Green being battery is charged, yellow battery is getting low, red time to change battery.

Or use one LED and solid color is good, blinking means battery will need changed.

I was searching online and think I found that we can use a LM339 Quad Comparator

Or just use a voltage divider and some extra lines of code ?.

Or you could use the LM3914 bar-graph LED display chip.

Thank you guys. I am going to search on these also.

Another chip to search for is the TLC3704 quad comparator.