/* // Example code for operating Compass and Sonar in tandem // // Make sure that you include the compass code and the sonar // code in the same directory. Remove the "main" methods from // them so there are no discrepencies. */ #use "alc_cmps03.ic" // comment out the main function #use "srf04-3.ic" // comment out the main function // RELAY connection: // the Sonar is connected to the Normally Closed (NC) pin // while the compass is connected to the Normally Open (NO) pin void main() { int i; while(1) { // read compass motor(3,100); // make sure the NO pin goes through to the Timer sleep(.1); for(i = 0; i < 10; i++) // calls compass 10 times { printf("[%d]compass: %d\n",i, compass()); sleep(.3); } // read sonar motor(3,0); //make sure the NC pin goes through to the Timer sleep(.1); sonar_init(); //needs to be called **each** time so that the timer is reset. sleep(.1); for(i = 0; i < 10; i++) { int result; float distance; result= sonar_sample(); distance=(float)result*1.1/4000.0; //turns the timer value into distance if (result != -1) printf("[%d]sonar: %d -> %f\n",i, result, distance); else printf("@@@@@@@\n"); //printout if error tone((float)(result/8),0.2); sleep(.3); } } }