abc
Dependencies: mbed Servo HC_SR04_Ultrasonic_Library
main.cpp@0:fa67d6421193, 2019-03-05 (annotated)
- Committer:
- kociol1994
- Date:
- Tue Mar 05 10:13:44 2019 +0000
- Revision:
- 0:fa67d6421193
Ana
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kociol1994 | 0:fa67d6421193 | 1 | #include "mbed.h" |
kociol1994 | 0:fa67d6421193 | 2 | #include "Servo.h" |
kociol1994 | 0:fa67d6421193 | 3 | #include "ultrasonic.h" |
kociol1994 | 0:fa67d6421193 | 4 | |
kociol1994 | 0:fa67d6421193 | 5 | |
kociol1994 | 0:fa67d6421193 | 6 | void dist(int distance) |
kociol1994 | 0:fa67d6421193 | 7 | { |
kociol1994 | 0:fa67d6421193 | 8 | //put code here to execute when the distance has changed |
kociol1994 | 0:fa67d6421193 | 9 | printf("Distance %d mm\r\n", distance); |
kociol1994 | 0:fa67d6421193 | 10 | } |
kociol1994 | 0:fa67d6421193 | 11 | |
kociol1994 | 0:fa67d6421193 | 12 | Servo myservo(D9); |
kociol1994 | 0:fa67d6421193 | 13 | Serial pc(SERIAL_TX, SERIAL_RX); |
kociol1994 | 0:fa67d6421193 | 14 | |
kociol1994 | 0:fa67d6421193 | 15 | ultrasonic mu(D8, D10, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9 |
kociol1994 | 0:fa67d6421193 | 16 | //have updates every .1 seconds and a timeout after 1 |
kociol1994 | 0:fa67d6421193 | 17 | //second, and call dist when the distance changes |
kociol1994 | 0:fa67d6421193 | 18 | |
kociol1994 | 0:fa67d6421193 | 19 | //------------------------------------------------------------------------------------------------- |
kociol1994 | 0:fa67d6421193 | 20 | int main() { |
kociol1994 | 0:fa67d6421193 | 21 | |
kociol1994 | 0:fa67d6421193 | 22 | myservo.calibrate (0.001, 45.0); // kalibracja serva |
kociol1994 | 0:fa67d6421193 | 23 | mu.startUpdates(); //start measuring the distance |
kociol1994 | 0:fa67d6421193 | 24 | |
kociol1994 | 0:fa67d6421193 | 25 | while(1) { |
kociol1994 | 0:fa67d6421193 | 26 | for(int i=(-25); i<35; i++) { |
kociol1994 | 0:fa67d6421193 | 27 | myservo.position(i); |
kociol1994 | 0:fa67d6421193 | 28 | wait(0.5); |
kociol1994 | 0:fa67d6421193 | 29 | printf("Pozycja %d ", i); |
kociol1994 | 0:fa67d6421193 | 30 | mu.checkDistance(); |
kociol1994 | 0:fa67d6421193 | 31 | } |
kociol1994 | 0:fa67d6421193 | 32 | for(int i=35; i>(-25); i--) { |
kociol1994 | 0:fa67d6421193 | 33 | myservo.position(i); |
kociol1994 | 0:fa67d6421193 | 34 | wait(0.5); |
kociol1994 | 0:fa67d6421193 | 35 | printf("Pozycja %d ", i); |
kociol1994 | 0:fa67d6421193 | 36 | mu.checkDistance(); |
kociol1994 | 0:fa67d6421193 | 37 | } |
kociol1994 | 0:fa67d6421193 | 38 | } |
kociol1994 | 0:fa67d6421193 | 39 | } |