The Last Engineer Standing
The emergency lights flickered one final time before dying completely. In the suffocating darkness of Sublevel 7, you pressed your palm against the cold metal door of the abandoned power management station. The building's AI had been your silent partner for months, seamlessly juggling the delicate dance between solar collection, battery storage, and power distribution. But that partnership ended three hours ago when the cascade failure ripped through the central processors.
Your tablet's screen cast an eerie blue glow across rows of dead battery banks. Twelve massive lithium arrays, each capable of powering half the facility, sat useless without intelligent charging control. The solar collectors on the roof were still functional, still drinking in precious photons, but with no brain to manage the flow, the raw power had nowhere to go. Worse yet, the manual override panels were locked behind biometric scanners that only responded to the now-defunct AI.
You pulled out your HERO XL board and a handful of components from your emergency kit. If the building's brain was dead, you'd have to become it. The photoresistor in your palm would stand in for the massive solar array sensors. A simple LED would represent the critical lighting systems that kept the underground passages navigable. And somewhere in the code you were about to write lay the difference between a slow death in the dark and a fighting chance at survival.
The weight of responsibility settled on your shoulders like a lead blanket. Every line of code would determine how efficiently precious energy flowed from collection to storage to consumption. Push the batteries too hard, and they'd degrade within days. Be too conservative, and the lights would fail when you needed them most. The AI had made it look effortless. Time to find out if a human engineer could match a machine's precision.
What You'll Learn
When you finish this lesson, you'll be able to build a sophisticated battery management system that rivals the building's original AI controller. You'll master reading analog sensor data to simulate solar panel output, implement intelligent charging logic that protects battery health, and create a real-time monitoring system using the Serial Plotter.
More specifically, you'll understand how to use floating-point mathematics to track precise energy levels, implement hysteresis in charging control to prevent rapid on-off cycling, and combine multiple systems (power management and user interface) in a single control loop. You'll also learn to use the map() function for data conversion and discover how professional battery systems balance charging speed against longevity.
Understanding Battery Management Systems
Before we dive into code, think about your smartphone. Ever notice how it charges quickly to 80%, then slows down for that final 20%? Or how it might shut down at 5% charge even though the battery isn't completely empty? That's not a bug—it's intelligent battery management protecting your phone's long-term health.
Real battery management systems walk a tightrope between performance and longevity. Push a lithium battery to 100% charge too often, and the chemistry starts breaking down, reducing its lifespan from years to months. Let it discharge completely, and you risk permanent damage. Professional systems like the one you're building use "charge windows"—they stop charging at 90% and cut power at 10%, leaving the battery in its happy zone.
But here's where it gets interesting: batteries hate rapid on-off cycling almost as much as extreme charge levels. If you restart charging every time the battery drops from 90% to 89%, you'll wear out the chemistry with constant state changes. Smart controllers implement hysteresis—they wait until the battery drops to maybe 85% before resuming charge. It's like a thermostat with a dead zone that prevents your furnace from clicking on and off every few seconds.
Your photoresistor is standing in for a massive solar panel array, but the principles are identical. Light intensity varies constantly throughout the day, and your controller needs to respond smoothly to these changes while protecting the battery from harmful charge patterns. The building's AI handled millions of these micro-decisions every day. Time to see if you can match its intelligence.
Wiring Your Emergency Power Controller

This circuit builds on your previous light switch project, adding solar simulation capabilities. Each connection serves a critical role in the power management ecosystem:
- LED on pin 22: Represents your critical lighting system. In the real facility, this would control entire lighting grids.