Just a bit of personal info, hoping it might help. That used to happened to me when I was still drinking tea on a regular basis. It caused this weird heart arrhytmia, especially when lying down, that wouldn't go away. Apparently the heart can be sensitive or alergic to certain specific foods. Coffee never gave me problems, though.
So I'd say monitor your diet for a specific trigger and also try to have a medical practitioner check your pulse while on the crisis, not afterwards. In those days an M.D. did told me that doing that would have given a more exact diagnostic.
Wishing you a complete recovery,
R.U.