Hackscribble:
Something to check straight away is your 10 second timing while loop:while((endtime - starttime) <= 10000)Until either endtime or startime is updated, the comparison will always have the same answer. So you might want to check in what circumstances you update either of them
Or just change it to: while((millis()- starttime) <= 10000)