FezMini [Newbie]

Hello guys,why flashing the led on, if I let go of the LDR button? The statement 2 is never reached, when iI use staement 1. But why?
Thanks!
Dulcinea


public class Program
    {
        private static OutputPort led;

        public static void Main ( )
        {
            led = new OutputPort ( ( Cpu.Pin )FEZ_Pin.Digital.LED, true );
            var interruptButton = new InterruptPort ( ( Cpu.Pin )FEZ_Pin.Interrupt.LDR, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth );
            interruptButton.OnInterrupt += new NativeEventHandler ( interruptButton_OnInterrupt );

            Thread.Sleep ( Timeout.Infinite );

        }

        static void interruptButton_OnInterrupt ( uint data, uint state, System.DateTime time )
        {
            while ( state == 0 )   //[b] Statement_1[/b]
            {
                led.Write ( !led.Read ( ) );
                Thread.Sleep ( 1000 );
            }
           if (state != 0)      // [b]Statement_2[/b]
           {
              led.Write( false );
           }
        }
    }

First, you should never do a loop in an interrupt handler.

Your program is in a loop.

When your interrupt handler is called, the state of the buttton is stored in the passed state variable.
You are looping on that variable, expecting it to change when the button state changes.

The state variable is not going to change. You need to rethink your logic. Each time the button’s state changes, the interrupt routine is called. On each call, you need to decide what to do with the led, do it, and then exit.

            
            while ( state == 0 )   // Statement_1
            {
                led.Write ( !led.Read ( ) );
                Thread.Sleep ( 1000 );
            }

This is infinite loop.

What took you so long Valen? ;D

Thanks, guys!
My catastrophic logic…

Okay… It is possible - with one interrupt handler - let the led blinking, when the button is pressed, and turn the led off, if the button is released?

Do you mean, turn the LED on when the button is pressed, and off when it is released?

Or blinking when button is pressed, and off when released?

You could do the first. You could do the second, but it would not be good design.

Hello!
I mean, blinking when button is pressed, and off when released.
I don’t want to wait actively, like


while (true)
{
while (Button.Read() == false)
{
LED.Write(true);
Thread.Sleep(100);
LED.Write(false);
Thread.Sleep(100);
}
}

You have to run blinking code in a loop but not in the interrupt handler.Do it in separate thread or inside your main function.That loop should check global variable that will store button state.

Change that global variable accordingly in your button handler.

Thanks for the help!