First Draft, serial print change based on distance

main.cpp

Committer:
liam94
Date:
2021-12-16
Revision:
1:a1795335ef8c
Parent:
0:506531d0531c
Child:
2:3ace5b4ae9a7

File content as of revision 1:a1795335ef8c:

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

 void dist(int distance)
{
    if (distance > 250){
    printf("Safe Distance %dmm\r\n", distance);
    }
    else if (distance <= 250 and distance > 150){
    printf("object detected %dmm\r\n", distance);
    }
    else if (distance <= 150 and distance > 80){
    printf("object detected Closer %dmm\r\n", distance);
    }
    else if (distance <= 80 and distance > 60){
    printf("Even Closer %dmm\r\n", distance);
    }
    else if (distance <= 60 and distance > 40){
    printf("Very Very Close %dmm\r\n", distance);
    }
    else if (distance <= 40 and distance > 0){
    printf("You've Hit a Wall %dmm\r\n", distance);
    }
}

ultrasonic mu(PTD0, PTC12, .1, 1, &dist);    //Set the trigger pin to PTD0 and the echo pin to PTC12
                                        //have updates every .1 seconds and a timeout after 1
                                        //second, and call dist when the distance changes

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