Main Site Documentation

Declaring pwm pin inside methode


#1

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


#2

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


#3

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


#4

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


#5

Do you call this method multiple times?


#6

Yes. Should I dispose pin after usage somehow ?


#7

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.


#8

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.


#9

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


#10

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.


#11

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


#12

That works, thank you.


#13

Great!