L
L
Leksaqq2019-09-10 12:58:20
Arduino
Leksaqq, 2019-09-10 12:58:20

How to make a triple click for a button in arduino?

It is necessary that when the button is pressed once, the red LED lights up, when double (for example, for a period of 0.5 s) - green, and when triple (for the same period) - blue. After 2 seconds, the RGB LED should turn off.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
max4107575, 2020-03-13
@max4107575

A simple code would look something like this:

int ledPin1 = 1;
int ledPin2 = 2; 
int ledPin3 = 3;   // Светодиоды
int inPin = 4;     // кнопка


void setup()
{
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    pinMode(inPin, INPUT);
}
 
bool flag = false;
int counter = 0;
uint32_t timer = 0

void loop()
{
    if (digitalRead(inPin) && !flag && millis()-timer < 500 && counter < 3)
    {
        flag = true;
        counter++;
        timer = millis();
    }
    if (!digitalRead(inPin) && flag)
    {
        flag = false;
    }
    if (!flag && millis()-timer > 2000)
    {
        counter=0;
    }
    switch (counter) 
    {
        case 1:
            digitalWrite(ledPin1, true);
            digitalWrite(ledPin2, false);
            digitalWrite(ledPin3, false);
            break;
        case 2:
            digitalWrite(ledPin1, false);
            digitalWrite(ledPin2, true);
            digitalWrite(ledPin3, false);
            break;
        case 3:
            digitalWrite(ledPin1, false);
            digitalWrite(ledPin2, false);
            digitalWrite(ledPin3, true);
            break;
        default:
            digitalWrite(ledPin1, false);
            digitalWrite(ledPin2, false);
            digitalWrite(ledPin3, false);
    }
}

You can improve it, take into account the bounce of contacts, you can use the GyverButton library

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question