you should never take medication while fasting. this isn't even from a purist point of view but for safety reasons. some medications can cause an ulcer if taken on an empty stomach. the body is much more sensitive while fasting.
next time try taking a warm shower to relax and help you sleep. and if you cannot deal then break with some wheatgrass or coconut juice. breaking the fast usually helps one sleep.
if you are having rapid heart beat you should see a doctor. the last thing you should be doing is typing in a forum for medical help.