Arduino And Sensors Part II

In case you missed Part I you can find it here . Part two really takes what we did in part I and put it all together using the following items:

In the Video I demonstrate how it all works together and what is the code looks like. Here is a sample code that you can find in the downloadable zip file above.

struct MotorSpeed{
     Speed;
    int Direction;
};
struct MotorSpeed GetMotorSpeed(int distance)
{
    struct MotorSpeed speed;
    if(distance >=30)
    {
         speed.Direction = FORWARD;
         speed.Speed = map(distance,30,40,125,255);
    }
    else if(distance >=22)
    {
         speed.Direction = FORWARD;
         speed.Speed = map(distance,22,29,70,125);
    }
    else if(distance >=15)
    {
         speed.Direction = FORWARD;
         speed.Speed = map(distance,15,21,50,70);
    }
    else if(distance >=8)
    {
         speed.Direction = BRAKE;
         speed.Speed = 0;
    }
    else if(distance >=4)
    {
         speed.Direction = BACKWARD;
         speed.Speed = map(distance,4,7,50,100);
    }
    else
    {
     speed.Direction = BACKWARD;
     speed.Speed = 200;
    }
    return speed;
}

You can download the code file here

In case you are interested in the Arduino Base I 3D Printed, you can find the stl file Here