How to count the number of objects in a bottle?
A regular pill weighs less than 1g. The main challenge with your approach is to weigh this exactly. As the weighing range of affordable precision scales is quite limited, it's even more challenging to weigh the pills together with the bottle.
Nick's suggestion may help to calculate the exact weight of one object - but you still have the problem to accurately weigh differences of +-1 object.

I'd say you should either lower your requirements (use heavier objects or allow a tolerance on counting) - or buy a ready-to-use counting precision scale.