If I undertsnad you correctly the problem is that the brake is being applied even when the spindle wasn't actually on at certain times. The code below (untested) might give you an idea. It only fires the brake if the spindle was actually on. There may be better ways of doing this.
Code:
'M5 macro
Const SpindleOnLED = 11 '(CW Or CCW)
Dim spindleOn As Boolean
spindleOn = getOEMLED(SpindleOnLED)
doSpinStop() 'stop spindle
'if spindle on then apply brake
If spindleOn Then
SetMasterOutput(12, 1) 'brake on
While isMoving()
sleep 10
Wend
sleep 2000 'apply brake for 2 seconds
SetMasterOutput(12, 0) 'brake off
End If (It should go without saying that E-stop should not rely on this or any other software. i.e. as well as everything else, the spindle brake should be applied by hardware following an e-stop too.)