Your problem might be related to the issue mentioned in
https://www.pentaxforums.com/forums/115-pentax-k-5/162331-mounted-yn-560-flash-problem.html
It's my impression that the trigger circuit (center pin, maybe PC contact) in the K5 has changed from previous models. As a result, the flash will only fire once. The change might have been 'reversed' in later models (I don't know, you're experience implies it was).
To solve the issue, you have to do some investigation. You need something in between the K5 and the flash that electronically decouples the flash from camera. Off-camera flash with radio triggers will do the trick (but might not suite your needs).
pTTL bounce flash on the K5 is a known issue (there are numerous threads here); try the AF540FGZ in auto-thyristor mode or full manual. It should fire consistently. If not, there is something wrong with the camera.