Declaring pwm pin inside methode

This line works just fine


PWM pwm = new PWM((PWM.Pin)FEZ_Pin.PWM.Di10);

But when I put it in some methode’s body it throws following execption: System.InvalidOperationException’ occurred in GHIElectronics.NETMF.Hardware.dll

Methode example:


static public void Intensity(float p)
        {
            PWM pwm3 = new PWM((PWM.Pin)FEZ_Pin.PWM.Di10);
            pwm3.Set(100, 0);
            Debug.Print((p * 30).ToString());
            if (p * 30 < 100 && p * 30 > 30)
            {
                if (p * 30 > 80)
                    pwm3.Set(100, 80);
                else
                    pwm3.Set(100, (byte)(p * 30));
            }
            else d += 100;
        }

You did remove it from elsewhere, so you’re not trying to use it a second time are you?

No, I am not trying to use it a second time.

When I put mentioned line of code in while loop I get same exception. It only works outside loops and methodes.

Do you call this method multiple times?

Yes. Should I dispose pin after usage somehow ?

Yes, try to call Dispose method after you are done. But if you are using it multiple times take out of the method make it class member and keep it.

I’m using this methode:


 static public void Frequency(float f,int x)
        {
            PWM pwm = new PWM((PWM.Pin)FEZ_Pin.PWM.Di10);
            PWM pwm2 = new PWM((PWM.Pin)FEZ_Pin.PWM.Di9);
            pwm2.Set(100, 0);
            pwm.Set(100, 0);
            if (500000 / f < 200 && 500000 / f > 160)
            {
                d = 0;
                if (x == 0)
                {
                    pwm.Set(100, 45);
                    pwm2.Set(100, 0);
                    x = 1;
                }
                else
                {
                    pwm2.Set(100, 45);
                    pwm.Set(100, 0);
                    x = 0;

                }
            }
            else
            {
                if (f > 200)
                {
                    d += 100;
                    if (d > 500000 / f)
                    {
                        d = 0;
                        if (x == 1)
                        {
                            pwm2.Set(100, 45);
                            pwm.Set(100, 0);
                            x = 0;
                        }
                        else
                        {
                            pwm.Set(100, 45);
                            pwm2.Set(100, 0);
                            x = 1;
                        }
                        d = 0;
                    }
                }
            }
            pwm.Dispose();
            pwm2.Dispose();
        }

It still throws an exception.

I will just make it a class variable, instantiate it once and use it in the method.

Ok, I did something like this:
Class:


namespace FFT2
{
    public class pwm
    {
        public PWM pwm1 = new PWM((PWM.Pin)FEZ_Pin.PWM.Di9);
        public PWM pwm2 = new PWM((PWM.Pin)FEZ_Pin.PWM.Di10);
        public PWM pwm3 = new PWM((PWM.Pin)FEZ_Pin.PWM.Di8);
        public PWM pwm4 = new PWM((PWM.Pin)FEZ_Pin.PWM.Di5);
        public PWM pwm5 = new PWM((PWM.Pin)FEZ_Pin.PWM.Di6);
    }
}

main:


            pwm m = new pwm();
            PWM pwm1 = m.pwm2;
            PWM pwm2 = m.pwm2;
            PWM pwm3 = m.pwm3;
            PWM pwm4 = m.pwm4;
            PWM pwm5 = m.pwm5;
...
Frequency(r[0], x, pwm1,pwm2);//call

And methode:


static public void Frequency(float f, int x, PWM o,PWM j)
        {
            PWM pwm2 = o;
            PWM pwm1 = j;

            pwm2.Set(100, 0);
            pwm1.Set(100, 0);
            if (500000 / f < 200 && 500000 / f > 160)
            {
                d = 0;
                if (x == 0)
                {
                    pwm1.Set(100, 45);
                    pwm2.Set(100, 0);
                    x = 1;
                }
                else
                {
                    pwm2.Set(100, 45);
                    pwm1.Set(100, 0);
                    x = 0;

                }
            }
            else
            {
                if (f > 200)
                {
                    d += 100;
                    if (d > 500000 / f)
                    {
                        d = 0;
                        if (x == 1)
                        {
                            pwm2.Set(100, 45);
                            pwm1.Set(100, 0);
                            x = 0;
                        }
                        else
                        {
                            pwm1.Set(100, 45);
                            pwm2.Set(100, 0);
                            x = 1;
                        }
                        d = 0;
                    }
                }
            }
            pwm1.Dispose();
            pwm2.Dispose();

And I get same exception at this line:

public PWM pwm1 = new PWM((PWM.Pin)FEZ_Pin.PWM.Di9);

in class.

Agree with Architect, if this method is called from a loop.

By doing this, you’re cutting down on the overhead of instanciating 2 new objects and the work the garbage collector needs to do every time the method is called.

I would go from this:


static public void Frequency(float f,int xstatic public void Frequency(float f, int x, PWM o,PWM j)
        {
            PWM pwm2 = o;
            PWM pwm1 = j;
 
            pwm2.Set(100, 0);
            pwm1.Set(100, 0);

To this:


static public void Frequency(float f,int x, PWM pwm1, PWM pwm2)
{
            pwm2.Set(100, 0);
            pwm1.Set(100, 0);

(just saw the rest of your code)

Why are changing references around here???

 pwm m = new pwm();
            PWM pwm1 = m.pwm2;
            PWM pwm2 = m.pwm2;
            PWM pwm3 = m.pwm3;
            PWM pwm4 = m.pwm4;
            PWM pwm5 = m.pwm5;
...
Frequency(r[0], x, pwm1,pwm2);//call

Why not just do this:

pwm m = new pwm();
...
Frequency(r[0], x, m.pwm2, m.pwm3);//call

That works, thank you.

Great!