int i; /* a loop counter */ int num; /* the number of readings to average */ int ticks; /* the # of clock ticks returned for */ /* a single reading */ int ticksAvg; /* the average of num readings */ void main(void) { num = 20; /* set the number of readings to average */ sonar_init(); /* initialize the sonar function */ /* must be called before sonar() */ printf("press start\n"); while(1) { /* loop continually */ if(start_button()) { /* take num readings every time */ /* start is pushed */ ticksAvg = 0; /* initialize to 0 */ for(i=0;i 0) { /* occasionaly the sonar() function fails */ /* and returns a neg. value, discard it */ ticksAvg += ticks; /* sum ticks to get average */ } } printf("ticks = %d\n",ticksAvg); /* print the result */ } } }