KARTOFLE / Mbed 2 deprecated Sonar

Dependencies:   mbed Servo HC_SR04_Ultrasonic_Library

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Servo.h"
00003 #include "ultrasonic.h"
00004 
00005 
00006 void dist(int distance)
00007 {
00008     //put code here to execute when the distance has changed
00009     printf("Distance %d mm\r\n", distance);
00010 }
00011 
00012 Servo myservo(D9);
00013 Serial pc(SERIAL_TX, SERIAL_RX);
00014 
00015 ultrasonic mu(D8, D10, .1, 1, &dist);    //Set the trigger pin to D8 and the echo pin to D9
00016                                          //have updates every .1 seconds and a timeout after 1
00017                                          //second, and call dist when the distance changes
00018                                          
00019 //-------------------------------------------------------------------------------------------------
00020  int main() {
00021      
00022      myservo.calibrate (0.001, 45.0);   // kalibracja serva
00023      mu.startUpdates();                 //start measuring the distance
00024  
00025      while(1) {
00026          for(int i=(-25); i<35; i++) {
00027              myservo.position(i);
00028              wait(0.5);
00029              printf("Pozycja %d ", i);
00030              mu.checkDistance();
00031          }
00032          for(int i=35; i>(-25); i--) {
00033              myservo.position(i);
00034              wait(0.5);
00035              printf("Pozycja %d ", i);
00036              mu.checkDistance();
00037          }
00038      }
00039  }