HC SR04

Dependencies:   mbed HC_SR04_Ultrasonic_Library

main.cpp

Committer:
Batoch
Date:
2019-05-03
Revision:
1:b47bfaaa417a
Parent:
0:97403de5e127
Child:
2:4b0821fe5e20

File content as of revision 1:b47bfaaa417a:

#include "ultrasonic.h"
#include "mbed.h"


void dist(int distance);
void dist2(int distance);


ultrasonic mu(A0, A1, .2, 1, &dist);    //Set the trigger pin to A0 and the echo pin to A1
                                        //have updates every .2 seconds and a timeout after 1
                                        //second, and call dist when the distance changes
ultrasonic mu2(A3, A4, .2, 1, &dist2);


void dist(int distance)
{
    //put code here to execute when the distance has changed
    mu.pauseUpdates();
    printf("Distance1 %d mm\r\n", distance);
    mu2.startUpdates();
}

void dist2(int distance)
{
    //put code here to execute when the distance has changed
    mu2.pauseUpdates();
    printf("Distance2 %d mm\r\n\n", distance);
    mu.startUpdates();
}




int main()
{
    mu.startUpdates();//start measuring the distance
    wait_ms(100);
    mu2.startUpdates();
    while(1)
    {
        //Do something else here
        mu.checkDistance();
        mu2.checkDistance();     //call checkDistance() as much as possible, as this is where
                                //the class checks if dist needs to be called.
    }
}