And since no one can guarantee that adc_count would not go below 5824, I would add this:

if adc_count<5824 then ad_count=5824 just before.

Ioannis