Repeatidly checking of IF cases n stuff

Hello Folks

I’m pretty new in C# and Gadgeteer… but I make my way bit by bit.
Actually I have a small issue with c# itself… it’s probably cuz of some missing knowledge about it^^

I’m not sure if that is the correct section for this… althought is more a basic C# issue, I post it here… since you probably better understand what I wanna do^^

I just know it from Java… is the Main Methode is through tha program will stop. in C# is that not the case… althought there is no actual code to process through… it doesn’t stop and it reacts to events like the button.

So I thought it always repeats the main methode in C#… but that’s also not the case, cuz then it would print me all the time “Program Sstarted” from the Debug.Print.

In Java would there be a stupid but effective solution… you wrap all into a DO{}While(True) loop and let the program run forever.
C# seems not very pleased with that… depens on where i place that infinity loop, i get memory overflow. but sometimes it works.

I tried also to cancel that loop with a simple boolean that can end the loop by pressing a button… the thing is, if the button event is inside that loop… it won’t let me call a methode outside that would set the loop-boolean on false to end the loop.

same issue i got with IF cases… even thought the IF condition is true, the program won’t go in since it’s not repeating the main methode all the time.

I made some tests with the Joystick… i wanna have the joystick always active… and not checking the IF condition when I press a button or something. it did wotk if i wrapped it inside a DO-While-True loop… but ah well… you know…

How you ppl solve that issue? >.<

This I’d like to have inside the main methode… but it only goey onetime through if i don’t wrap a loop around it >.<

JoystickPosX = joystick.GetJoystickPosition().X;
                Debug.Print("X: " + JoystickPosX);

                JoystickPosY = joystick.GetJoystickPosition().Y;
                Debug.Print("Y: " + JoystickPosY);
                if ((JoystickPosY < 0.25) && (Flip1 == 0))
                {
                    Debug.Print("LED nach R");
                    Debug.Print("Flip1:" + Flip1);
                    Flip1 = 1;
                    Led++;
                    led7r.TurnLightOn(Led, true);
                }
                if ((JoystickPosY > 0.25) && (JoystickPosY < 0.75))
                {
                    Flip1 = 0;
                }

                if ((JoystickPosY > 0.75) && (Flip2 == 0))
                {
                    Debug.Print("LED nach L");
                    Debug.Print("Flip2:" + Flip2);
                    Flip2 = 1;
                    Led = Led - 1;
                    led7r.TurnLightOn(Led, true);
                }
                if ((JoystickPosY > 0.25) && (JoystickPosY < 0.75))
                {
                    Flip2 = 0;
                }  

What hardware are you using?
Try using a timer, that way you can constantly check the value and not block the application

Something like this… check the joystick position every 100 ms


void ProgramStarted()
        {
           
           GT.Timer timer = new GT.Timer(100);
            timer.Tick +=new GT.Timer.TickEventHandler(timer_Tick);

            timer.Start();
         
        }

 void timer_Tick(GT.Timer timer)
        {
            double xcoord = (joystick.GetJoystickPosition().X -0.5) * 180;
            double ycoord = (joystick.GetJoystickPosition().Y - 0.5) *180;

            Debug.Print("X = " + xcoord.ToString() + "Y = " + ycoord.ToString());

        }


EDIT : added the bit i deleted by accident

1 Like

I use a FEZ Hydra / GHI Joystick / Button / led7c … and next to implement is the 16 Relay module^^

hmm… a timer… that looks like the thing is was looking for. >.>
I’ll have a try on that.

Thank you very much :smiley:

greez evi

did I miss something when it says “timer is unknown in the actual context” ?

 void ProgramStarted()
        {
            GT.Timer timer = new GT.Timer(100);
            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);

            timer.Start();
        }

That will teach me to clean up the code in my post :). I got a bit over confident with the delete key. Sorry about that evul.

Arrr, it worked. you guys are great :smiley:

Then I take in the 16 Relay Module… didn’t find any sourcecode on that one… but i guess it’s similar to the 4 relay module.

@ HughB: don’t worry about that^^ it did help anyway :smiley: