Shop OBEX P1 Docs P2 Docs Learn Events
Sonar Ping — Parallax Forums

Sonar Ping

DomanikDomanik Posts: 233
edited 2014-10-13 11:29 in Learn with BlocklyProp
I'm playing with an ActivityBot, Ping and sound and came up with this code to beep on a speaker as an object is detected. The rate and freq. change as the object gets closer. Kids love it and they can grasp what the code is doing when they have feedback and can make their own changes.
/*   Sonar.c
   
 */
 #include "simpletools.h"                      // Include simple tools
 #include "ping.h"
 

 int main()                                    // Main function
 {
  while(1)                                                                    // Repeat indefinitely

   {
     int inDist = ping(15);                                                    // Get cm distance from Ping)))

     int inchDist = inDist/148;
     print("Dist = %d inches\n", inchDist);         // Display distance

     pause(100 + inDist/100);
     if ( inDist > 4000)  pause(150);

     if ( inDist > 8000)  pause(200);
     if ( inDist > 10000) pause(250);
     if ( inDist < 12200)  freqout(26, 80, (12200 -inDist) / 15 + 300);

         else

       {

         pause(100);

         freqout(26,  40, 550);  

         freqout(26, 250, 300);  

  }

}

  square_wave(26, 1, 3000);                                         //save to play with later

[INDENT]square_wave_stop();
[/INDENT]
   
  }


Dom...

Comments

  • George SuttonGeorge Sutton Posts: 180
    edited 2014-10-13 11:29
    Domanik wrote: »
    I'm playing with an ActivityBot, Ping and sound and came up with this code to beep on a speaker as an object is detected. The rate and freq. change as the object gets closer. Kids love it and they can grasp what the code is doing when they have feedback and can make their own changes.

    Dom...


    This is a really great idea, one that I plan to use with my kids. Thanks for posting it.
Sign In or Register to comment.