How to control 4 leds with a button

Hi, i am new to arduino and i want to use it for my hobby. I try to use 1 push button to control 4 leds. If i shortpress 1 time and longpress, it will execute led #1. If i shortpress 1 time and longpress again, it will turn off led #1. If i short press 2 times and long press, it will execute led #2. If i short press 2 times and long press again, it will turn off led #2 and so on. If i long press will turn off all leds. I will use ESP32 for this project or maybe anyone knows there is a better microcontroller to run this. Can anyone help me for this project? Thank you.

Welcome to the forum

The ESP32 is suitable for this project. What code have you written so far ?

Have you considered using a library to make detecting the short and long button presses ?

Your description of the project is not very clear. For instance, can 2 or more LEDs be on at once ?

1 Like

ESP32 is OK, but for the functions you listed here, any Arduino can be used, from a UNO R3 (I think even an ATtiny).
Anyway, we don't/can't create code for third parties, you should start by yourself with "something" (and you probably need to study at least the basics of Arduino and the C language). If you don't have any experience with Arduino and/or C language, your first goal is a bit too hard, you better proceed in simpler steps.

So, you first need to know how to connect a LED to an Arduino digital output (use a 220 Ohm resistor in series) ex. pin 2, and a button (one button pin to GND the other to the digital input) ex. pin 3, and set it to INPUT_PULLUP.
Then start writing a simple code to control the LED and make it on and off with each button press. Remember physical buttons have "bounces" so when you detect a state change (from HIGH to LOW when start pressing it, or LOW to HIGH when releasing), add a small delay (e.g. "delay(80)") to bypass bounces. This is called "software debounce" (you can add some hardware for a "hardware debouce" but I don't want to add you more concepts).
We can help you if you have problems, but we need your code here.

Once you have this code working, you can start checking how to detect short and long presses (using "millis()" function), and so on. We'll help you there too. But you need something to start.

1 Like

https://docs.arduino.cc/built-in-examples/digital/Button/

https://www.arduino.cc/en/Tutorial/Blink

Hello famra

Welcome to the best Arduino forum in the world.

What is the task of the program in real life?

As already mentioned, the task description is incomplete.

Try the following project (Fig-1) first using Arduino UNOR3 to develop the concept.


Figure-1:

1. Define the duration of shortpress (say: 5000 ms) and longpress (say: 10000 ms) in ms.

2. Press the Button and then turn on L (built-in LED of UNO).

When shortpress/longpress duration will be expired, L will turn on. The duration will be displayed on Serial Monitor.

3. Make a shortpress of Button and then turn on L.

Example Sketch: (tested)

unsigned long presentTime = millis();
#define shortPress 5000   //test purpose

void setup()
{
  Serial.begin(9600);
  pinMode(12, INPUT_PULLUP);
  pinMode(13, OUTPUT);
}

void loop()
{
  if (digitalRead(12) == LOW)
  {
    while (millis() - presentTime < shortPress) //waiting until shortPress is expired 
    {
      Serial.println(millis() - presentTime);
    }
    digitalWrite(13, HIGH);
    while (digitalRead(12) != HIGH) //waiting to see Button is opened
    {
      Serial.println("Release the Button.");
      delay(300);
    }
    digitalWrite(13, LOW);
  }
}

All this sounds like class assignment to me. It is not something you would try and dream up as a hobbyist. When is the assignment due?