code work fine on usb power or external regulated power but not on battery.

Can you share your sketch with us, if you are not putting your device into a low power mode or turning it off then it may just be your battery running flat? Easiest way I can think to check that would be to read the battery voltage as part of your sketch and see if it goes down over the 1 hour it works for when on battery only.