US and servo problem

Hello,
i am doing a robot, with servo motor and ultrasonic sensor. But when i use the 2 at the same time, nothing work. Servo and US cant’ work together.
help plz.
us sensor : http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
my code :



//US method
public static int USdetect(TristatePort _pin) 
        {
           
            const int TicksPerMicrosecond = (int)TimeSpan.TicksPerMillisecond / 1000;
            
            
            _pin.Active = true; 
            _pin.Write(true);
            Thread.Sleep(5);
            _pin.Write(false);
            _pin.Active = false;
            bool high = false;

            do {
                high = _pin.Read();
            }
            while (!high);
            
            long startTime = System.DateTime.Now.Ticks;

            while (high) {
                high = _pin.Read();
            }

       
            int ticks = (int)(System.DateTime.Now.Ticks - startTime)>>1;

            int microSeconds = (ticks / TicksPerMicrosecond);

 
            return (microSeconds / 29);}

//servo method


 public static void ServoStop(PWM servoBase)
         {
                 servoBase.Set(false);}

         public static void ServoMove(float angle, PWM servoBase,bool precis)
         {   float MinAngle = 0;
             float MaxAngle = 180;
             double MaxUs = 2.40;
             
             if (angle > MaxAngle)
                 angle = MaxAngle;

             if (angle < MinAngle)
                 angle = MinAngle;
             if (precis == true && angle==0)
                 {for (int i=90; i >= angle; i--){
                         servoBase.SetPulse(20000000, (uint)((MaxUs - (0.01 * i)+0.02) * 1000000));
                         Thread.Sleep(10);}}

             else if (precis == true && angle == 180)
             {for (int i = 90; i <= angle; i++){
                     servoBase.SetPulse(20000000, (uint)((MaxUs - (0.0111 * i)) * 1000000));
                     Thread.Sleep(10);}}
             
             else if (angle == 0) 
             {
                 servoBase.SetPulse(20000000, 2450000);
                 Thread.Sleep(1000);
                 ServoStop(servoBase);
             }
             else if (angle == 180)
             {
                 servoBase.SetPulse(20000000, 500000);
                 Thread.Sleep(1000);
                 ServoStop(servoBase);
             }
             else
             {
                 servoBase.SetPulse(20000000, (uint)((MaxUs - (0.0111 * angle)) * 1000000));
                 Thread.Sleep(1000);
                 ServoStop(servoBase);}}



public static void Main()                                                               
        {   
            bool Play = true;
            TristatePort _pin = new TristatePort((Cpu.Pin)FEZ_Pin.Digital.Di52, false, false, Port.ResistorMode.Disabled);
            PWM servoBase = new PWM((PWM.Pin)FEZ_Pin.PWM.Di9);

            while (Play==true)
            {
                    USdetect(_pin);
                    ServoMove(180, servoBase, false);
                    Thread.Sleep(1000);
                    ServoMove(90, servoBase, false);
                    Thread.Sleep(1000);
 }
        }



it can be a wire connection problem ?

Move your US and servo code into separate threads

1 Like

Ah, ok, it work :wink:

but not very good… :frowning:

i changed the main() code:

  int r= USdetect(_pin);
                    Thread.Sleep(1000);
                    if (r < 10)
                    {
                        ServoMove(180, servoBase, false);
                        Thread.Sleep(1000);
                        ServoMove(90, servoBase, false);
                        Thread.Sleep(1000);
                    }

and it stopped to work

need help …

What kind of values do you get for r?

using System;
using System.Threading;
using Microsoft.SPOT;

namespace MFConsoleApplication1
{
    public class Program
    {
        private static int usVal;
        public static void Main()
        {
            Thread usThread = new Thread(ReadUs);
            usThread.Start();

            while (true)
            {
                if (r < 10)
                    {
                        ServoMove(180, servoBase, false);
                        Thread.Sleep(1000);
                        ServoMove(90, servoBase, false);
                        Thread.Sleep(1000);
                    }
            }
        }

        private static void ReadUs()
        {
            for (; ; )
            {
                usVal = USdetect(_pin);
                Thread.Sleep(1000);
            }
                
        }
        private static int USdetect()
        {
            return 1;
        }
    }
}
1 Like

r = int values :distance

i solved my problem with

servoBase = null;
             Debug.GC(true);

if that fixed your problem, then I suspect you didn’t articulate what the problem was very effectively to us. You do realise what those statements do, right, and what the effect would be?