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)
      {
           .....

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.

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!