Several task with one button in arduino uno

Hi! i am using arduino uno for several function with one button, i want press the button one moment for power one led in 200ms, too press the button two moment for power the led in 600ms. If I keep the button pressed, the LED should remain on until the button is released. I already tried with external interrupt and a counter timer but I don't have an exact idea of ​​a method. The button sequence does not have to be followed. I can light the led continuously at first if I keep the button pressed, as well as light the led for 200ms if I press once and quickly release the button.

Why are you needing to do this ?


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘posting menu’ to attach the copied sketch.


Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

It’s not too hard, stick with learning, and you’ll get there.

First get your button inputs working, then study millis() to determine the passage of time.
If you’re just tinkering, this should take a couple of days.

Last step, keep track of the button count, and the timing to set the LEDs.
Another two days of tinkering. (not just bumbling).

There are many examples and references for the functions you’re trying to achieve,.
That’s the skill.. finding then adapting them to your requirements.

  • capture a timestamp when the button is pressed as well as when released
  • if released after 200 but before 400ms, recognize case 1
  • if released after 400 but before 600ms, recognize case 2
  • if not released after 600 msec, recognize case 3 and turn off when released

so loop needs code to

  • recognize a button state change and when it is pressed
  • and released
  • a timer that compares the current millis() to some timestampi

there needs to be logic to track which state


.... too difficult to explain

consider

  • which uses a timer to both turn-off an LED as well as recognize when the button has been held down long enough, and

  • monitor button presses and releases to both recognize that a button hasn't been held down long enough, turning on an LED, and long enough, turning off an LED

const byte PinBut = A1;
const byte PinLed = LED_BUILTIN;

byte butLst;

unsigned long msecPeriod;
unsigned long msecLst;
unsigned long msecBut;

byte state;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void loop (void)
{
    unsigned long msec = millis ();
    if (msecPeriod && msec - msecLst >= msecPeriod)  {
        msecPeriod = 0;

        switch (state) {
        case 1:
            Serial.println (" state 1 timeout");
            digitalWrite (PinLed, Off);
            break;
        case 2:
            Serial.println (" state 2 timeout");
            digitalWrite (PinLed, On);
            break;
        }
    }

    byte but = digitalRead (PinBut);
    if (butLst != but)  {
        butLst = but;
        delay (20);     // debounce

        if (LOW == but)  {
            state      = 2;
            msecPeriod = 500;
            msecLst    = msec;
        }
        else {
            if (500 < (msec - msecBut))  {
                Serial.println (" < 1000");
                state = 1;
                msecPeriod = 200;
                msecLst    = msec;
                digitalWrite (PinLed, On);
            }
            else  {
                Serial.println (" off");
                digitalWrite (PinLed, Off);
            }
        }
    }
}

void setup (void)
{
    Serial.begin (9600);

    pinMode (PinBut, INPUT_PULLUP);
    butLst = digitalRead (PinBut);

    pinMode (PinLed, OUTPUT);
    digitalWrite (PinLed, Off);
}

Thanks,i doing using your exemple,your exemple is very usefull

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.