/***************************************************** * function void range() measures range to an object * * using the SRF04 sonar ranging sensor. It displays * * range on the LCD display in mm, and sounds a tone * * proportional to the range (higher is closer). * *****************************************************/ #use "srf04_sonar.ic" void range() { int d, freq, a=-4, b=4200; float duration=0.1; printf("Press START..."); while (!start_button()) ; while(!stop_button()) { /* Loop forever */ d = sonar(); /* Get distance in mm */ printf("D=%d mm\n",d); freq = a*d+b; /* Compute frequency */ if (freq < 200) freq = 200; if (freq > 4000) freq = 4000; tone((float)freq,duration); /* Play tone */ sleep(0.03); } printf("\n"); /* Clear display */ }