LED works very slow with button

I use FEZ Spider kit, and I write a programm. there are 4Timers and a button EventHandler in the programm.

The ButtonEventHandler works for a LED. If I press the button, LED turn on. If I press the button again, LED turn off.

The other 4 Timers work for Display task, UDP task, SPI task, and GPIO task.

I find that, the LED works very slow, when I press buttion. sometimes the LED doesn’t work, when I press the button.

If I stop all the Timers, the LED and button work together very well. But I nees the Timers, I want multi tasks.

How can I solve the problem?

How many Timers can I define inthe programm?

the cose as follow:



    public partial class Program
    {

        GT.Timer Timer1 = new GT.Timer(10);//Display
        GT.Timer Timer2 = new GT.Timer(100);//UDP
        GT.Timer Timer3 = new GT.Timer(10);//SPI
        GT.Timer Timer4 = new GT.Timer(100);//GPIO

         initial.........
         ..................................         
         ..................................

        void ProgramStarted()
        {
            ethernet.UseStaticIP("192.168.1.200", "255.255.255.0", "192.168.1.56", new string[] { "8.8.8.8" });

            display.SimpleGraphics.AutoRedraw = false;
           
            Debug.Print("Program Started");


            button.ButtonPressed += new GTM.GHIElectronics.Button.ButtonEventHandler(button_ButtonPressed);


            Timer1.Tick += new GT.Timer.TickEventHandler(Timer2_Tick);//Display
            Timer2.Tick += new GT.Timer.TickEventHandler(Timer3_Tick);//UDP
            Timer3.Tick += new GT.Timer.TickEventHandler(Timer5_Tick);//SPI
            Timer4.Tick += new GT.Timer.TickEventHandler(Timer6_Tick);//GPIO
            Timer1.Start();//Display
            Timer2.Start();//UDP
            Timer3.Start();//SPI
            Timer4.Start();//GPIO

        }

        void button_ButtonPressed(GTM.GHIElectronics.Button sender, GTM.GHIElectronics.Button.ButtonState state)
        {
            if (j == 0)
            {
                led.TurnGreen();
                j = 1;
            }
            else
            {
                led.TurnOff();
                j = 0;
            }
        }

        void Timer1_Tick(GT.Timer timer)//Display
        {
           ...... //Display task             
        }

        void Timer2_Tick(GT.Timer timer)//UDP
        {
           ...... //UDP task             
        }

        void Timer3_Tick(GT.Timer timer)//SPI
        {
           ...... //SPI task             
        }

        void Timer4_Tick(GT.Timer timer)//GPIO
        {
            ......//GPIO task             
        }


}



how long is very slow?

you are getting 220 interrupts a second. I suspect you are doing a lot of processing in the interrupt handlers and/or issuing blocking APIs.

try turning off the timers, and adding them back one by one until you find the timer that is causing the slowdown.

it is not a good idea to do a lot of processing in interrupt handlers. you might want to consider threads.

@ Mike -

hi thank you for your answer!!

I have found the problem!

The display task caused the problem, if I stop the display task, the “ButtonEventHandler” can work well.I show you the display task in Timer 1 again.

How can I solve the problem? I really need the display to show my 16 temperatur values.

How can I define the “threads” in “Gadgeteer”? Can you give me a example?



public partial class Program
    {
 
        GT.Timer Timer1 = new GT.Timer(10);//Display
        GT.Timer Timer2 = new GT.Timer(100);//UDP
        GT.Timer Timer3 = new GT.Timer(10);//SPI
        GT.Timer Timer4 = new GT.Timer(100);//GPIO
 
         initial.........
         ..................................         
         ..................................
 
        void ProgramStarted()
        {
            ethernet.UseStaticIP("192.168.1.200", "255.255.255.0", "192.168.1.56", new string[] { "8.8.8.8" });
 
             static Font DispFont = Resources.GetFont(Resources.FontResources.NinaB);            
             display.SimpleGraphics.AutoRedraw = false;
 
            Debug.Print("Program Started");
 
 
            button.ButtonPressed += new GTM.GHIElectronics.Button.ButtonEventHandler(button_ButtonPressed);
 
 
            Timer1.Tick += new GT.Timer.TickEventHandler(Timer2_Tick);//Display
            Timer2.Tick += new GT.Timer.TickEventHandler(Timer3_Tick);//UDP
            Timer3.Tick += new GT.Timer.TickEventHandler(Timer5_Tick);//SPI
            Timer4.Tick += new GT.Timer.TickEventHandler(Timer6_Tick);//GPIO
            Timer1.Start();//Display
            Timer2.Start();//UDP
            Timer3.Start();//SPI
            Timer4.Start();//GPIO
 
        }
 
        void button_ButtonPressed(GTM.GHIElectronics.Button sender, GTM.GHIElectronics.Button.ButtonState state)
        {
            if (j == 0)
            {
                led.TurnGreen();
                j = 1;
            }
            else
            {
                led.TurnOff();
                j = 0;
            }
        }
 
        void Timer1_Tick(GT.Timer timer)//Display
        {
            display.SimpleGraphics.ClearNoRedraw();

            display.SimpleGraphics.DisplayText("T1:  "  + Temperatur[0],  DispFont, GT.Color.Blue, 0,    0);
            display.SimpleGraphics.DisplayText("T2:  "  + Temperatur[1],  DispFont, GT.Color.Blue, 80,   0);
            display.SimpleGraphics.DisplayText("T3:  "  + Temperatur[2],  DispFont, GT.Color.Blue, 160,  0);
            display.SimpleGraphics.DisplayText("T4:  "  + Temperatur[3],  DispFont, GT.Color.Blue, 240,  0);
            display.SimpleGraphics.DisplayText("T5:  "  + Temperatur[4],  DispFont, GT.Color.Blue, 0,   20);
            display.SimpleGraphics.DisplayText("T6:  "  + Temperatur[5],  DispFont, GT.Color.Blue, 80,  20);
            display.SimpleGraphics.DisplayText("T7:  "  + Temperatur[6],  DispFont, GT.Color.Blue, 160, 20);
            display.SimpleGraphics.DisplayText("T8:  "  + Temperatur[7],  DispFont, GT.Color.Blue, 240, 20);
            display.SimpleGraphics.DisplayText("T9:  "  + Temperatur[8],  DispFont, GT.Color.Blue, 0,   40);
            display.SimpleGraphics.DisplayText("T10: "  + Temperatur[9],  DispFont, GT.Color.Blue, 80,  40);
            display.SimpleGraphics.DisplayText("T11: "  + Temperatur[10], DispFont, GT.Color.Blue, 160, 40);
            display.SimpleGraphics.DisplayText("T12: "  + Temperatur[11], DispFont, GT.Color.Blue, 240, 40);
            display.SimpleGraphics.DisplayText("T13: "  + Temperatur[12], DispFont, GT.Color.Blue, 0,   60);
            display.SimpleGraphics.DisplayText("T14: "  + Temperatur[13], DispFont, GT.Color.Blue, 80,  60);
            display.SimpleGraphics.DisplayText("T15: "  + Temperatur[14], DispFont, GT.Color.Blue, 160, 60);
            display.SimpleGraphics.DisplayText("T16: "  + Temperatur[15], DispFont, GT.Color.Blue, 240, 60);

            display.SimpleGraphics.Redraw();           
        }
 
        void Timer2_Tick(GT.Timer timer)//UDP
        {
           ...... //UDP task             
        }
 
        void Timer3_Tick(GT.Timer timer)//SPI
        {
           ...... //SPI task             
        }
 
        void Timer4_Tick(GT.Timer timer)//GPIO
        {
            ......//GPIO task             
        }
 
 
}

You are updating the display 100 times a second. That seems much too fast. :slight_smile:

One or two times a second should be more than enough?

As far as threading, Google “C# threading tutorial”.

@ Mike -

it works well now, when I use 1000ms for Timer1. Thanks a lot!!!

Can you also tell me, how can I define the thread for multitask?

I really want to know it, can you give me some suggestion or tip?

If it is possible, can you give a example on basis of my programm, use the “thread” instead of “timer” ?

I remember only somthings like that as follow:



Thread[] ta = new Thread[100]; 

   lock (lockvar)
   {
           ..........
    }  


Threads are not a simple subject. Read some tutorials online.