Let's Make Robots!

The Tale of One Pleased Robot Owner


I can't tell you how much I am enjoying this FEZ Mini Robot!

My coding in C# is rapidly improving and now I can do just about anything I want, it compiles (not even many warnings!) and runs great on the robot. The Microsoft emulator no longer puts out those error messages (as long as the hardware is attached to the system.)

When I started out I thought maybe I couldn't learn C# but I am happy to be wrong about that. I still have a LOT to learn in C#, but my desire to learn it is VERY strong.

It's true, FEZ is Fricking Easy! :-)

I had to get the mini board with the robot, but now I want a bigger FEZ board in a bigger robot.

UPDATE: This is better than I could have ever expected! This thread has now become a discussion from beginner (me) to seasoned programmer (Geir) about programming in C#. Others are becoming involved now too. That is sooooo cool!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Beware of the new operator. You should not create any FEZ objects (FEZ Component or FEZ IO pins) inside of functions, except the main function. Each time the function will be called, a new FEZ object will be created, that will cause a runtime error. 

The main function is a special function, because it will only be called once at the program startup. So it is ok to create FEZ objects here (But not inside the while loop!)

If you want to access the FEZ objects from other functions, declare them static outside of the main function or give the function a reference to the FEZ object as function parameter.

Here is a code snippet with all of your FEZ objects (except anaIn) declared outside the main function. All functions inside the program class will have access to the FEZ objects. The AnalogIn FEZ object anaIn is only used inside the main function. Remove all other object creations from your functions.


  public class Program
    static FEZ_Components.ServoMotor myServo = new FEZ_Components.ServoMotor(FEZ_Pin.Digital.Di8);
    // set up An4 and An5 LED outputs
    static FEZ_Components.LED myGreen = new FEZ_Components.LED(FEZ_Pin.Digital.An4);
    static FEZ_Components.LED myRed = new FEZ_Components.LED(FEZ_Pin.Digital.An5);
    // a little object oriented goodness
    static FEZ_Components.DistanceDetector myRanger = new // new object
    FEZ_Components.DistanceDetector(FEZ_Pin.AnalogIn.An1, // one of the available analog inputs (An1)
    FEZ_Components.DistanceDetector.SharpSensorType.GP2Y0A21YK); // number may be wrong???

    static void Main()
    {      // for random numbers
      var rnd = new Random(); // object oriented variable creation
      sbyte m1 = 50;
      sbyte m2 = 60;
      sbyte n1 = -70;
      sbyte n2 = -70;
      int dist = 300; // don't worry about it if this or beyond
      int caut = 200; // caution if less than this
      int turn = 13; // must turn at this distance
      byte y = 0;
      byte ramp = 10;
      double value = 0;

      int anaSensorReading = 100; // this variable will hold the switch analog value

      FEZMini_Robot.MoveRamp(m1, m2, ramp);

      // config analog
      AnalogIn anaIn = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An2); // An2 is analog input
      anaIn.SetLinearScale(0, 100); // scale the analog input (wow!)

      while (true)

As of my last compile this morning, my entire program is almost all broken up as working functions and it works great!!!!

I do have central "core" code but almost all it does is call the functions. Nice!

This is what I wanted all along, a program that really looks like C# and runs!

I have a LOT more to learn, but Geir and you have gotten me off to an awesome start.

Now I just want my Panda II board for more intense experiments (and maybe a next-stage "super" robot! :-)

Any way, thanks again!

We have another C# guru! You can never have too many of them! Thank you for the very sage advice, it should help with some of problems I am having (the compiler gives good warnings, I just don't know enough about C# to understand the "lingo" yet.)

Now I have a better chance of getting my good working code into proper looking code. It is all appreciated.

Off topic, slightly, I have decided to get a FEZ Panda II for more experimenting. It looks like an awesome board!!!

A big thank you to both you and Geir for all the C# help you have given me!

(I was also hoping birdmun would get involved here too, I just am not sure he does any C#, (If I remember right I believe he is into C and C++.) 

Great to see it's working now. Soon you will become a C# guru, too.

The example you gave me ran fine and debugged without a problem.

I can send any program to the FEZ without a problem. It is only after starting to debug a bad program and it runs for a while does the debugger crash. It always shows the error as being on of the provided source files (liks led.c) and NOT in my program (the debugger switches its display to the failing program and code after it crashes).

I know all the programs like led.c are working becuase I use them in my working program.

Confusing? Yes it is really bad for me a C# neophyte. (I am trying to learn scope of variables right now.)

(My program doesn't look THAT bad and at least it all runs great on my robot...)

Back to studying,,,

I don’t know if this is an issue, but you reassign some analoge pins in this function

static void mood(double value, int dist, int caut, int turn)
            FEZ_Components.LED myGreen = new FEZ_Components.LED(FEZ_Pin.Digital.An4);
            FEZ_Components.LED myRed = new FEZ_Components.LED(FEZ_Pin.Digital.An5);

So you might want to move the myGreen & myRed up as a global object like this

namespace tryFunc1
    public class Program
       static FEZ_Components.LED myGreen = new FEZ_Components.LED(FEZ_Pin.Digital.An4);
       staticFEZ_Components.LED myRed = new FEZ_Components.LED(FEZ_Pin.Digital.An5);

        static void Main()
            FEZ_Components.ServoMotor myServo = new FEZ_Components.ServoMotor(FEZ_Pin.Digital.Di8);

This is something off our beaten path. I updated the discussion topic intro expressing my pleasure as to which way this conversation has gone. You didn't want me to call you a guru (in my book you are) so I refered to you as a "seasoned programmer" and thought you wouldn't mind that.

That helps show me more of function structure. (I also have been redefineing pins and I know the warning not to assign 2 functions to the same pin at one time. Wonder if that could blow a pin?)

I want to ask:


using System;

using Microsoft.SPOT;

namespace justJunk1


    public class Program


        int a = 0;

        static void Main()





       void fun()





        void fun2()







In this small example program, I know by defining "a" publicly all the little routines can make use of it. The question/problem is this variable is having "strange" problems in main. I thought it would just as easily be available there.
It seems easy to create the functions, its just passing the variables that causes me severe problems. (Primarily variable type and scope.) 
Thanks again.


I think that if you changed the

 int a = 0;


static  int a = 0;

it would compile just fine... but I maybe wrong.

As of my most recent compile, MY ENTIRE PROGRAM IS ALMOST ALL FUNCTIONS (methods?) !!!!

Even the "harder" functions are working as they should ( avg(), mood() etc)

(Man, have I ever learned a LOT of C#!!!!)  Now I want that Panda II board!!!! :-)

A BIG thanks goes to you for getting me to this point!

Your (want to write more C#) friend, Will.