Servo error

Hi, can you help me plz, i have an error in my code, but i can’t find it.


US.Suspend();
                    RAM(number, SD, true);  
                    ServoMove(85, servoBase);
                    for (int i = 85; i >= 0; i = i--)
                    {
//THE CODE STOP HERE
                        ServoMove(i, servoBase);
                        Thread.Sleep(10);
                        US.Resume();
                        Thread.Sleep(10);
                        int ScanDroite_lent1 = usVal;
                        Thread.Sleep(10);
                        US.Suspend();
                        Thread.Sleep(10);
                        ServoMove((i+1), servoBase);
                        Thread.Sleep(10);
                        US.Resume();
                        Thread.Sleep(10);
                        int ScanDroite_lent2 = usVal;
                        Thread.Sleep(10);
                        US.Suspend();
                        Thread.Sleep(10);
                        if (ScanDroite_lent1 > ScanDroite_lent2 && ScanDroite_lent2 - ScanDroite_lent1 < 5)
                        {
                            if (orientation == "yx")
                            {
                                ListeX[number] = (int)((ScanDroite_lent2 * ScanDroite_lent2 - ScanAvant * ScanAvant) ^ (1 / 2)) + posX;
                                ListeY[number] = posY + ScanAvant;
                            }
                            else
                            {
                                ListeY[number] = (int)((ScanDroite_lent2 * ScanDroite_lent2 - ScanAvant * ScanAvant) ^ (1 / 2)) + posY;
                                ListeX[number] = posX + ScanAvant;
                            } number++;
                        }
                        if (ScanDroite_lent2 < ScanDroite_lent1 && ScanDroite_lent1 - ScanDroite_lent2 < 5)
                        {
                            if (orientation == "yx")
                            {
                                ListeY[number] = (int)((ScanDroite_lent1 * ScanDroite_lent1 - ScanDoite * ScanDoite) ^ (1 / 2)) + posY;
                                ListeX[number] = posX + ScanDoite;
                            }
                            else
                            {
                                ListeX[number] = (int)((ScanDroite_lent1 * ScanDroite_lent1 - ScanDoite * ScanDoite) ^ (1 / 2)) + posX;
                                ListeY[number] = posY + ScanDoite;
                            } number++;
                        }
                    }





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

        public static void ServoMove(float angle, PWM servoBase)
        {

            float MinAngle = 0;
            float MaxAngle = 180;
            double MaxUs = 2.40;

            if (angle > MaxAngle)
                angle = MaxAngle;

            if (angle < MinAngle)
                angle = MinAngle;

            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);
            }
            
        }

thx

What does “stop” mean? An exception?

What device are you using?

i put some debug.print in my code and after the loop for, they aren’t printed

there is no exception being printed?

Exception System.IO.IOException - CLR_E_FILE_IO (5)

#### Message: 
#### Microsoft.SPOT.IO.VolumeInfo::.ctor [IP: 0000] ####
#### Microsoft.SPOT.IO.RemovableMedia::MessageHandler [IP: 0022] ####

i think there is an problem with the loop for, because if i just do :


for(int i=85;i>=0;i--){
ServoMove(i,sevoBase);}

there are an error

or if i do :


for(int i=85;i>=0;i--){
ServoMove(10,sevoBase);}

too
but normally ServoMove works

Error is from either if the first 2 lines…
not the servo stuff, error is to do with sd card.

ii don’t understand this, if i use sd card there are an error ?
because i use an sd card in my programm

i want to count down from 85 to 0

@ Tirmit - look closer at the error message.

1 Like

ah, thx, it doesn’t work because i always did :

//FileHandle.Close();
            //SD.UnmountFileSystem();

and than opened the file again.

Super :slight_smile:

For your line:


for (int i = 85; i >= 0; i = i--)

you don’t need the i = i-- on the end.

Just use the following


for (int i = 85; i >= 0; i--)

This will decrement i for you and is easier to understand. :slight_smile:

Okay, this line of code is EPIC! I can almost guarantee that if it were included on a college exam 99 times out of 100 the student would get the wrong answer.

for (int i = 85; i >= 0; i = i--){...} 

Why?

i-- means give me the current value of i THEN decrement it.

After i is decremented what value is assigned to i via the assignment operator ? If you said the value of i returned before the decrement you win.

In other words

for (int i = 85; i >= 0; i = i--){...} 

is an infinite loop.

Edit: back to the OP, the reason the servo never moves is because i always = 85;

ServoMove(i, servoBase);

Love those kind of questions Make you pause and think.

Too true.