arduino code for end detection