Visual studio button problem

Discussion in 'Programmer's Corner' started by Dritech, Jun 20, 2014.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi all,

    I am using a button will be used to blink an LED. When this button is pressed, I am turning on the LED for 5 seconds, and then turn it off for 5 seconds. The problem is that I want the LED to keep blinking as long as the button is pressed.
    How can I achieve this please? I tried using both BUTTON CLICK and BUTTON DOWN but non of them worked (function only executed once). Is there a method that as long as the button is pressed, the program keeps looping in that function?
    Note, I am programming using Csharp.

    Thanks in advance for your help.
     
  2. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    Is this a hardware project or software only? You really need to post your code before we can help
     
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You could create a timer whose tock event toggles the LED. Enable/disable the timer based on the state of the button.
     
  4. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    It is software combined with hardware. Below is the part of the code:

    Code ( (Unknown Language)):
    1.      
    2.         private void Button_up_MouseDown(object sender, MouseEventArgs e)
    3.         {
    4.             string Send_code = "a";
    5.             serial(use_COM, Send_code);
    6.         }  
    7.  
    Now I want the program to keep sending the character a as long as the button is pressed and not just send it once.
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Why not let the hardware count the 5 seconds and just send go/stop commands when the button state changes?
     
  6. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Thanks for the reply.
    Can I use a while look with the MOUSE UP?

    Code ( (Unknown Language)):
    1.         private void Button_up_MouseDown(object sender, MouseEventArgs e)      
    2.            {
    3.            while(mouseUP)
    4.             {
    5.             string Send_code = "a";            
    6.             serial(use_COM, Send_code);
    7.             }
    8.            }
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    If you do that, you'll lock up your application.
     
  8. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    You can spawn off a thread to blink the LED. Create the thread on the first click event and pass a parameter to tell it to blink or not. On subsequent events, first check to see if the thread has already been created, and if so, just pass a message to blink or not blink. It might sound complicated, but it's very easy.
     
  9. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    I'd go with tshuck's suggestion of letting the hardware keep the LED flashing until told to stop.
     
Loading...