Main Site Documentation

Arrays? Defining Components


So I know this is more of a programming question, not a specific FEZ question, when defining components, is there a short way to define an array of new components, but also map/define the pin set.

The following is a hack of the LED driver which I set up for an old parallel port 8 relay board I had laying around. I got the hardware and program to turn off and on the relays, but when programming many new components I was just wondering if there is a shorter, less program consuming way to define new components.

public static void Main()
            FEZ_Components.relay Relay1 = new FEZ_Components.relay(FEZ_Pin.Digital.Di5);//cable pin2
            FEZ_Components.relay Relay2 = new FEZ_Components.relay(FEZ_Pin.Digital.Di6);//cable pin3
            FEZ_Components.relay Relay3 = new FEZ_Components.relay(FEZ_Pin.Digital.Di7);//cable pin4
            FEZ_Components.relay Relay4 = new FEZ_Components.relay(FEZ_Pin.Digital.Di8);//cable pin5
            FEZ_Components.relay Relay5 = new FEZ_Components.relay(FEZ_Pin.Digital.Di9);//cable pin6
            FEZ_Components.relay Relay6 = new FEZ_Components.relay(FEZ_Pin.Digital.Di10);//cable pin7
            FEZ_Components.relay Relay7 = new FEZ_Components.relay(FEZ_Pin.Digital.Di11);//cable pin8
            FEZ_Components.relay Relay8 = new FEZ_Components.relay(FEZ_Pin.Digital.Di12);//cable pin9

If you see the layout and the pin configuration, they are (in this example) all incrementally in order.

I am not sure if an array is even correct line this application, but I am just thinking of different methods/processes thru this programming stuff.

Mike in MN


I’d do it the way you have. That way you can move the connection to each one as needed.

If you check out most other drivers you create an insance of, their constructor takes all the pins it needs, it doesn’t “assume” any. In your case you happen to have a bank of connections to consecutive pins, but what if I said I want you to add a PWM LED control and an SPI device to the project? Then you most likely will need to separate the 8 relay outputs to different pins


you can do this instead:

FEZ_Components.relay[] Relay= new FEZ_Components.relay[8];

Relay[0] = new FEZ_Components.relay(FEZ_Pin.Digital.Di5);

// etc...


edit: More coffee for me please :-[


No …

new FEZ_Components.relay[8]

is correct. He needs to allocate an array of 8 relays (0-7) and that is the correct way to do it.

When using ‘new’ you are specifying the count of items, not the array index of the last item.



Foekie, you are a Hero member, you shouldn’t make such mistake :frowning:


@ Gus Are the pins enumerated

Can he do this?

FEZ_Components.relay[] Relay= new FEZ_Components.relay[8];

for(int x = 5 ; x < 13 ; x++)
Relay[ x - 5 ] = new FEZ_Components.relay(FEZ_Pin.Digital.x);

I havent tried it with Fez components…

Cheers Ian


Yes I agree it’s probably academic to define components this way, but interesting that if you can dream it, it may be able to happen.

Thanks for the input, I might play a bit with it, just because… :smiley: :smiley: :smiley:

Mike in MN


For Arduino and BS2 compatibility reasons, the pins named Dix or Anx are not mapped to same pin number. I mean Di1 is not IO1.

Now, all pins that are named IOx have the correct numbering.

OutputPort x = new OutputPort(5, …);
is same as
OutputPort x = new OutputPort(IO5, …);
but it is not same as
OutputPort x = new OutputPort(Di5, …);


But that wouldn’t help since your array of relay objects is just an array of the references to the objects and not the objects themselves.

You will still have to do some work to initialize the objects and place their references in an array but the good news is that you will have them grouped in one array…if this is what you need.


Thanks Gus… I guess I don’t need anything, moreso looking at all of the IO’s on the Panda and the expansion board component, thinking if a person used all those up it would be a lot of defining objects/components of code.

For the most part just wondering if there was a shorter way, or different way or better way. (remember a programming newbie here).

If that’s the only/best way, then that’s the way I will do it.

Thanks again,

Mike in MN


Probably good idea to keep those in array for easy enumeration of array later. .Net provides some cool things that many people don’t see at first such as initializers, implicit typing, etc that can take some the grunt work out.

public static void InitComponents()
        * Using collection initializers and implicitly typed local var
        * you can reduce some of the grunt work.
    var lightSensors = new FEZ_Components.LightSensor[]
        new FEZ_Components.LightSensor(FEZ_Pin.AnalogIn.An6),
        new FEZ_Components.LightSensor(FEZ_Pin.AnalogIn.An7)
        // ... others

    // Later in program you can enumerate (i.e. walk over the array) or pluck out by index.
    // Note: foreach has a bit more overhead then "for",
    // foreach first grabs an Enumerator for the array and uses that.
    // It may also copy array first.
    foreach (var sensor in lightSensors)

    // A for loop is more resource efficient.
    for (int i = 0; i < lightSensors.Length; i++)
        var sensor = lightSensors[i];

    // Can also just grab a sensor object using an index.
    int level1 = lightSensors[0].GetLevel();


William…have absolutely no idea what you tried saying there, but keep throwing it out, because each time someone suggests something I try reading up on it.

Still don’t grasp the concept of enumeration yet, but it’s brought up alot… :-[

Side note: I have 3 C# books I have been through over and over, they are huge and technical, trying to pick out the couple needles to make something work is hard when they aren’t written for the robotic side of the world, they are skewed for internet and database office stuff.


Mike in MN


The post has been edited. Again, sorry, you were totally right ;D

I should read twice before posting when tired, I totally misunderstood you ???

Don’t shoot me :smiley:


Enumeration is really defining several variable or types with a common reference

Ie. just imagine you have an output port numbered 0 - 7 and.

A1 is on pin 1
A2 is on pin 4
A3 is on pin 0
A4 is on pin 7

It doesn’t flow very well. So we enumerate so we don’t have to remember where they are ie…

Enum {
AnalogInA1 =1 ;
AnalogInA2 = 4;
AnalogInA3 = 0;
AnalogInA4 = 7;

Sometimes we enumerate so our code is readable ie… Cpu.Pin.D1 is easier to read than Cpu.Pin.1

I hope this helps


[quote]Still don’t grasp the concept of enumeration yet, but it’s brought up alot…

It´s not hard. Let´s look at it step by step.
First, he creates a array of components (in this case light sensors)

var lightSensors = new FEZ_Components.LightSensor[]
        new FEZ_Components.LightSensor(FEZ_Pin.AnalogIn.An6),
        new FEZ_Components.LightSensor(FEZ_Pin.AnalogIn.An7)
        // ... others

As you can see he uses this statement for defining it as an array:

var lightSensors = new FEZ_Components.LightSensor[]

Now we have an array of components.

Next, we can add components to this array by using “new” :

new FEZ_Components.LightSensor(FEZ_Pin.AnalogIn.An6),
new FEZ_Components.LightSensor(FEZ_Pin.AnalogIn.An7)

Now we have added 2 components to the component array.

Enumeration of an array is basically an exact listing of all of its elements.

What he does is this:

foreach (var sensor in lightSensors)

If we strip it down to English we have this:

[quote]For each sensor in the array of lightsensors


print their value to the debug window.[/quote]

so, print the value of each sensor in the array to the debug window.

In windows form you will see this with disposing controls on a form.
I hope it’s a little bit more clear to you now, if not, please let me know.

If I made a mistake let me know, I do my best :slight_smile:


This is from Jens Kuhners book Expert .NET Micro Framework page 74


Thanks for the info and explanations guys!

It’s much nicer having an actually board now to try to apply some of this stuff. I think I am grasping it.

Last nite download the “FezHexAPod” program, been looking through that, and it has array in there also.

I will keep pluggin thru with my LEDS and relays. Hopefully I can put something practical together.

In time it will all come together.

Thanks again, it’s appreciated.

Mike in MN


The hexapod has a servo array. Where servo is a component too in fact. It does not differ much, however the project uses a little more complicated declarations.


Yes, it’s a bit over my head, I do know that.

I went through the Projects and downloaded many of them, primarily just to read through the code.

Trying to understand the layout, syntax, probably moreso just get an overall feel how programs flow with the layouts.

I will say some are a bit over my understanding with multiple projects or files in the solutions. But I guess it kinda helps how some of you guys call/use different things and put them together.

Mike in MN