One button for many functions

Happy New Year!

I have a quenstion.

I have this button
https://www.ghielectronics.com/catalog/product/274
and I am thinking if I can use this button for many function at the same time.

One idea I have is to have a counter and depending the clicks to run other function.
Is this possible?
Have you some other idea?

Thanks.

see this thread: https://www.ghielectronics.com/community/forum/topic?id=12509

thanks!
I will see it.

@ gharamis - should be no problem. Start a “fire once” timer in the first “putton pressed” event, Count the “button pressed” Events in the timer period, do different things depending on the Count.

@ RoSchmi

thanks.
is it easy for you to give me any example?

many thanks!

@ gharamis - A minimal state machine example:



int buttonState = 0;
int state_max = 3;
int state_min = 0;

button_press_event_handler()
{
    buttonState++;
    
    if(buttonState > state_max)
        buttonState = state_min;

    switch(buttonState)
    {
        case 0:
            cleanup();
            break;
        case 1:
            makeNoise();
            break;
        case 2:
            doSometingElse();
            break;
        case 3:
            doOneOtherState();
            break;
    }
}

I understand.

thanks! :slight_smile:

@ James -
Hi, isnt it that your program only toggles through the different functions? I dont think that this is what gharamis wants. I think he wants it possible to reach e.g. function 3 without doing function 1 or 2.
I have no FEZ Device here, so I made a Windows Forms example.
If you click 2 times or 3 times in a second you get function 2 or 3. If you click 1 x you get function 1. If the functions last longer or could block the functions should be in new threads. I’m sure there are better solutions for this than mine.


namespace DiffButtonFunctions
{
    public partial class Form1 : Form
    {
        System.Windows.Forms.Timer myTimer = new Timer();
        bool GateIsOpen = true;
        int ClickCounter = 0;
        int MaxClick = 3;
        int timerInterval = 1000;

        public Form1()
        {
            InitializeComponent();
            myTimer.Interval = timerInterval;
            myTimer.Tick += myTimer_Tick;
            myTimer.Stop();
        }

        void myTimer_Tick(object sender, EventArgs e)
        {
            GateIsOpen = false;
            myTimer.Stop();
            switch (ClickCounter)
            {
                case 1:
                    textBox1.Text = "Button Funct. 1";
                    Application.DoEvents();
                    break;
                case 2:
                    textBox1.Text = "Button Funct. 2";
                    Application.DoEvents();
                    break;
                case 3:
                    textBox1.Text = "Button Funct. 3";
                    Application.DoEvents();
                    break;
            }
            ClickCounter = 0;
            GateIsOpen = true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (GateIsOpen)
            {
                if (ClickCounter == 0)
                {
                    myTimer.Interval = timerInterval;
                    myTimer.Start();
                }
                ClickCounter++;
                if (ClickCounter > MaxClick)
                {
                    ClickCounter = MaxClick;
                }
            }
        }
    }
}

Another option could be to use the ButtonPressed & ButtonReleased events to time how long the button is held. So, holding the button for 1 sec does one thing, 2 secs does another, etc…

Unless there is some visual indication of the current state selected by a button, using a single button would make for a confusing user interface. Also, if you started pressing a button, and decided you did not want to execute, how would you cancel?

@ Mike - Pressing a button and then wanting to cancel is a problem regardless of single or multiple clicks. It’s not like a mouse where you can click on the button and then release off of the button.

I have had to use a single device for multiple operations often in my work, when the customer provides the hardware upfront and doesn’t want to pay for modifications. We always use the length of time of the button press, key switch, what have you.

Maybe it is better to speak more specific.

so, my code is for an alarm clock.
I have 2 buttons.
with these 2 button I want to control a menu with 3 options.
for example:
-set up time (button 1)
-set up alarm (button 2)
-on/off alarm (button 1 & 2 together)

when I will be inside into menu with button 1 will change hour and minute and with button 2 for confirmation etc.

You need to do 2 things:
1st: always remember in which menu you are

2nd to enter setup time/alarm and on/off alarm you need to handle butten press and release accordingly:

b1 down -> b1 up = setup time
b2 down -> b2 up = setup alarm
b1 down -> b2 down -> b1 or b2 up -> on/off alarm -> other button up ignored
b2 down -> b1 down -> b1 or b2 up -> on/off alarm -> other button up ignored

when inside a menu the b1 will change hour/minute and b2 continues to next or exits the sub menu.

in fact this is a simple state machine (google it)

yes!
I want something like this. :slight_smile:

thanks!