Just use a voltage regulator that has built in step up and step down. You can find one that always output 5V. Just be careful you have enough current i.e. cut the power when the current drops below 500mA. Maybe also use a huge cap.