Target detection and collision avoidance

Hello All,

I’m using FEZ Cerbot and Distance US3 module to do obstacle detection, I used same code shared by twkisner

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace Cerbot
{
public partial class Program
{
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{

        // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.

        
        Debug.Print("Program Started");
        multicolorLed.GreenBlueSwapped = true;  //my RGB LED has the blue and green flipped

        // plays the scale
        cerbotController.StartBuzzer(261, 600);
        cerbotController.StartBuzzer(293, 600);
        cerbotController.StartBuzzer(329, 600);
        cerbotController.StartBuzzer(349, 600);
        cerbotController.StartBuzzer(392, 600);
        cerbotController.StartBuzzer(440, 600);
        cerbotController.StartBuzzer(493, 600);
        cerbotController.StartBuzzer(523, 600);

        char_Display.PrintString("William's Cerbot");

        new Thread(RobotEyeSlide).Start();
        new Thread(DoEdgeDetect).Start();



    }

    void RobotEyeSlide()
    {
        ushort Eyes = 0;
        int direction = 1;
        int ledIndex = 0;
        while (true)
        {
            ledIndex += direction;
            Eyes = (ushort)(0x1 << ledIndex);
            cerbotController.SetLEDBitmask(Eyes);
            Thread.Sleep(30);

            if (ledIndex <= 0 || ledIndex >= 15)
            {
                direction *= -1;

            }
        }
    }


    

    void DoEdgeDetect()  //does edge detect and distance with US3
    {

        const int RUN_SPEED = 70;
        const int REVERSE_SPEED = -70;
        distance_US3.AcceptableErrorRate = 0;
        int j;
        const double SENSOR_THRESHOLD = 10;

        while (true)
        {
            Thread.Sleep(1);

            bool leftOverEdge = cerbotController.GetReflectiveReading(CerbotController.ReflectiveSensors.Left) > SENSOR_THRESHOLD;
            bool rightOverEdge = cerbotController.GetReflectiveReading(CerbotController.ReflectiveSensors.Right) > SENSOR_THRESHOLD;

            if (!rightOverEdge && !leftOverEdge)
            {

                //if it detects edges, stops

                multicolorLed.TurnColor(GT.Color.Red);
                cerbotController.SetMotorSpeed(0, 0);
                cerbotController.StartBuzzer(1000, 100);
                char_Display.SetCursor(1, 0);
                char_Display.PrintString("Put Cerbot Down!");
                Thread.Sleep(2000);
            }

            else
            {
                Thread.Sleep(100);

                j = distance_US3.GetDistanceInCentimeters(5);


                if (j == distance_US3.SENSOR_ERROR)
                {
                    char_Display.SetCursor(1, 0);
                    char_Display.PrintString("Sensor error");
                }
                else
                {
                    char_Display.SetCursor(1, 0);
                    char_Display.PrintString("Distace away=" + j);
                }

                if (j <= 5 || j >= 40)
                {
                    multicolorLed.TurnColor(GT.Color.Cyan);

                    cerbotController.SetMotorSpeed(RUN_SPEED, RUN_SPEED);

                }
                else
                {

                    //if it detects object within 40 centimetes, stops, backs up, turns, beeps then goes forward

                    multicolorLed.TurnColor(GT.Color.Magenta);
                    cerbotController.SetMotorSpeed(0, 0);
                    cerbotController.SetMotorSpeed(REVERSE_SPEED, REVERSE_SPEED);
                    Thread.Sleep(250);
                    cerbotController.SetMotorSpeed(REVERSE_SPEED, RUN_SPEED);
                    Thread.Sleep(500);
                    cerbotController.SetMotorSpeed(0, 0);
                    cerbotController.StartBuzzer(2000, 500);
                    cerbotController.SetMotorSpeed(RUN_SPEED, RUN_SPEED);

                }
            }


            
        }
    }


}

}

The code works good and able to detect obstacles .
Now I’m trying to do target detection e.g red circle on the floor as a target, and the robot has to stop when reaches this target

Can you please assist me on modifying the code and the other modules should I use ?

King regards
Sara