Community

How do I loop through the digital pins?

Just got my Meadow board yesterday and started playing with it. So far so good. I ran into an issue trying to loop through the digital pins. I don’t see a forum specifically for the Meadow board or coding, so I thought putting it here would make most sense. I don’t have any experience with Slack, but it you think it makes more sense to post questions like this there, just let me know.

My question is simply how best to loop through each of the digital pins 00 through 15 while initializing them and not have to hard code them. I can loop through .AllPins, but I don’t see a way to identify which ones are the digital pins. My first thought was to use an integer and count from 0 to 15, but I don’t see a way to access the pins this way. Just wondering if I am overlooking something obvious.

Is there a way to loop through just the digital pins? Or is there a way to loop through all pins and id the ones that are the digital pins?

Here’s my example. I’m having to hardcode this but would rather have a loop similar to what I have commented. Is this possible? Is there a better way to handle this?

private void Initialize()
        {
            //int count = 0;
            //foreach (var pin in Device.Pins.AllPins)
            //{
            //    leds[count] = new Led(Device.CreateDigitalOutputPort(pin));
            //    count++;
            //}

            leds[0] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D00));
            leds[1] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D01));
            leds[2] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D02));
            leds[3] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D03));
            leds[4] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D04));
            leds[5] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D05));
            leds[6] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D06));
            leds[7] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D07));
            leds[8] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D08));
            leds[9] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D09));
            leds[10] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D10));
            leds[11] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D11));
            leds[12] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D12));
            leds[13] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D13));
            leds[14] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D14));
            leds[15] = new Led(Device.CreateDigitalOutputPort(Device.Pins.D15));
        }

Check this out: https://github.com/WildernessLabs/Meadow_Samples/blob/master/Source/MeadowSamples/GpioInterrogation/GpioApp.cs

If they’re a digital pin, it’ll have an IDigitalChannelInfo.