counting and sending results

This Flow Chart describes what you have wanted in your original post as to the actions to be taken by your program. You may work yourself to convert this Flow Chart into Arduino/C Programming codes.