LONG PRESS BUTTON (WITH MILLIS) - Arduino tutorial #15

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 มิ.ย. 2024
  • In this tutorial I'll teach you how to detect whether a button is being short or long pressed using millis(). Course material arduino-tutorials.net/tutoria...
    ⚠️ At 10:20 this video shows an unnecessary check for !buttonStateLongPress. We made this false on line 75 and therefore !buttonStateLongPress is always true. This is fixed in the course material. Thanks to ET's Home Repair for letting me know.
    The Arduino for beginners tutorials series guides you through the world of Arduino. A video is around 10 minutes and shows you step-by-step how to build the circuit and write the code. The website contains the course material, circuit diagram, Arduino code and shopping list of all the components used in the tutorial.
    ⬇️ Arduino code, circuit diagram and part list arduino-tutorials.net/tutoria...
    👉 More tutorials and projects on arduino-tutorials.net
    🇳🇱 Deze tutorial is ook in het Nederlands beschikbaar • 28BYJ-48 STEPPER MOTOR...
    Chapters:
    00:00 Introduction
    00:08 Requirements
    00:46 The circuit
    02:35 Uitleg Arduino code
    11:20 Running on the Arduino
    COMPONENTS
    🇨🇳 Arduino Uno (clone)
    arduino-tutorials.net/r/KnSgtg
    🇳🇱 Arduino Uno (clone)
    arduino-tutorials.net/r/8237Hd
    🇳🇱 Arduino Uno SMD (original)
    arduino-tutorials.net/r/Ud29dh
    🇳🇱 Arduino Uno Chip (original)
    arduino-tutorials.net/r/Jd82ha
    🇨🇳 3x breadboard
    arduino-tutorials.net/r/k8rZRv
    🇳🇱 1x breadboard small
    arduino-tutorials.net/r/238Jds
    🇨🇳 1x breadboard large
    arduino-tutorials.net/r/ZnW9L4
    🇳🇱 1x breadboard large
    arduino-tutorials.net/r/8237Dh
    🇨🇳 Jumper wire male-male
    arduino-tutorials.net/r/pkoGV4
    🇳🇱 Jumper wire male-male
    arduino-tutorials.net/r/8237Jd
    🇨🇳 2000x Resistors
    arduino-tutorials.net/r/FHHA69
    🇳🇱 15x colored tactile switches
    arduino-tutorials.net/r/PU7wZM
    🇳🇱 5x colored tactile switches
    arduino-tutorials.net/r/834jFH
    🇨🇳 50x colored tactile switches
    arduino-tutorials.net/r/TmUMFO
    #arduino #BasOnTech

ความคิดเห็น • 75

  • @A5A5A5A5h
    @A5A5A5A5h 4 ปีที่แล้ว +5

    I'm creating a morse code decoder, this tutorial was very useful even with a straight key or a paddle. Thank you!

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว

      Great to hear I could help! 😃

  • @jaimevaldez3058
    @jaimevaldez3058 4 ปีที่แล้ว +1

    Best long button tutorial I have seen.

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว

      Thanks! 😃

  • @danielsaenz5570
    @danielsaenz5570 ปีที่แล้ว +1

    This video is amazing! Thanks for making it!

    • @BasonTech
      @BasonTech  ปีที่แล้ว

      Thanks, my pleasure! 😃

  • @dextercloser4788
    @dextercloser4788 3 ปีที่แล้ว +1

    Academic explanation, Nice work dude

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว +1

      Thanks! 😃

  • @cchobbyfun1
    @cchobbyfun1 2 ปีที่แล้ว

    Really well done tutorial. Using this code is saving me a lot of time in a project.😀

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      Great! As long as you know how the code works 😉

  • @danielsaenz5570
    @danielsaenz5570 ปีที่แล้ว

    Hello! On line 54 is it really needed to put the ... && !buttonStateLongPress? I did it without the '&& !buttonStateLongPress' and it worked just fine. Because that only matters if you're holding the button, but if you're holding the button then the previous button value CAN'T 'equal LOW', since YOU'RE HOLDING IT.
    hope that makes sense.

  • @ask_fornature4012
    @ask_fornature4012 ปีที่แล้ว

    Thanks a bunch

    • @BasonTech
      @BasonTech  ปีที่แล้ว +1

      My pleasure! 🚀

  • @cksmaya
    @cksmaya 2 ปีที่แล้ว

    THANK YOU

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      My pleasure! 😃

  • @danielsaenz5570
    @danielsaenz5570 ปีที่แล้ว

    Great video! I don't understand why there is the 'buttonStateLongPress' variable. If you could explain that, that would be great! Thanks!! 👍

  • @individuum2106
    @individuum2106 2 ปีที่แล้ว

    Hi, fantastic work. You`ve saved my day! Thanks a lot! But something went wrong in my program code. It will just run the short pressed button and never the long pressed situation. longPress seems always be on false. Any forgotten libraries that have to be integrated (time.h?)? The longPress action in the wrong if statement? Normally it has to be in the serial print "Button long pressed". Or is there maybe a problem with the milli function?

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      Hard to say. Make sure you create the code step by step. I see many people copy/paste the code. Then it is very hard to debug what goes wrong.

  • @prateekbhardwaj6720
    @prateekbhardwaj6720 2 ปีที่แล้ว

    Thanks bas tech with this i programmed my atmel mcu to make a smd rework station like 861dw

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว +1

      Great to hear! 😃

  • @GeorgeJFW
    @GeorgeJFW 4 ปีที่แล้ว +1

    Nice man I am working on Something that could use something like this

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว +1

      Great to hear this can help you with your project! 😃

  • @fleckditchburn2494
    @fleckditchburn2494 2 ปีที่แล้ว

    Hello I have watched your video and it loots really good. I am a 100% disabled (no movement whatsoever) the software looks adaptable to use my tongue to push a switch. With various hardware I have developed this will in turn click the computer mouse.

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      That is so awesome, great work! 😃

  • @petebevan
    @petebevan 4 ปีที่แล้ว +1

    Hi, Thanks for the informative and useful tutorial. I am new to coding and arduino. Is it easy to add another button to this set up without having two lots of code for each button?

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว

      Hi Pete, that kind of depends on what you want achieve. You still need to determine if the button was long pressed for each button.

    • @ETs-Everend
      @ETs-Everend 4 ปีที่แล้ว

      You could do this by passing variables back and forth to the function. Pass the button address to the function so it checks the right one for long/short press. Return the press state as an integer, 0=no complete press, 1=short press, 2=long press.

  • @robr3662
    @robr3662 4 ปีที่แล้ว

    Hi!
    With this code I was finally able to use my ESP32 touch sensor as long/short press button, however there is one issue in all of that:
    Using the "long press" will always trigger a short press as well, which shall not be the point of this I guess.
    How to improve the code to ignore the short press if a long press was initiated?
    Thanks!
    BR
    RoB

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว +1

      Hi Rob, then this is code isn't suitable. What needs to be done is measure the time the button has been pressed. So when it goes from HIGH to LOW you start measuring, and when the button is released, so from LOW to HIGH then you stop timing. You can then determine if it was a short or longpress based on the elapsed time. Hope this makes sense 😃

  • @ipadize
    @ipadize ปีที่แล้ว

    whats the advantage of using a external pull down resistor vs using the internal pull up of the arduino?

    • @BasonTech
      @BasonTech  ปีที่แล้ว +2

      As far as I am aware none. In this tutorial I wanted to show how to use a pull-down resistor with an external one 😃

  • @fringeanomaly9284
    @fringeanomaly9284 11 หลายเดือนก่อน

    could you please explain a condition where 1 button was pressed and released and another button was pressed ..and if all this is within a certain amount of time then a certain code must execute
    plz help

    • @BasonTech
      @BasonTech  11 หลายเดือนก่อน

      What have you tried so far, and where do you get stuck?

  • @Seoulhawk01
    @Seoulhawk01 2 ปีที่แล้ว

    Would this help with false positives? I have seven buttons at the end of a four meter cable, two of the buttons keep popping on and off. I was thinking that a requiring long press would stop this from happening.

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      Hi Douglas, it looks like your aren't using pull-up or pull-down resistors in your circuit. This can make the state of the switch "floating" and gives false positives.

    • @Seoulhawk01
      @Seoulhawk01 2 ปีที่แล้ว

      @@BasonTech Hello, thanks for the comment. I am using resistors. Seems like I get a bit of static electricity that is causing the problem. That is why I was looking for something of a long hold or using the analog input. Today was a rainy day and I had no problems at all.

  • @shrikantnikam2426
    @shrikantnikam2426 4 ปีที่แล้ว

    Is it possible to make millis function in separate tab and use millis single line command like delay? For make code more simpler

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว

      Hi Shrikant, this should be possible. You then include your functions via the separated file.

    • @shrikantnikam2426
      @shrikantnikam2426 4 ปีที่แล้ว

      @@BasonTech but how?

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว

      I don't have an off the shelf example for you. But if you search for "arduino split code multiple files" there should be many examples.

  • @micodatugan7724
    @micodatugan7724 4 ปีที่แล้ว +1

    Did you have any code of arduino sir. There are 2 buttons that 1 button is the led would run and the 2nd button is for faster and That if i press the 2nd button it will increase the speed and then if i press again it will increase and so fort then if it reach the limit it goes back to the designate value i means it will go back to the normal speed.
    I hope you can help me

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว +1

      Hi Mico, sorry I don't have a on the shelf solution for your idea. However, take some time to split your idea into smaller challenges. Later you can merge them together. The trick is to start small, and gradually build bigger projects 😃

    • @micodatugan7724
      @micodatugan7724 4 ปีที่แล้ว +1

      Thank you sir. I've been struggling for the condition for the 2nd button 😭. But anyways thank you for the idea.

  • @luissolla7990
    @luissolla7990 3 ปีที่แล้ว

    Thanks! I'am doing a project and i need this code for two buttons, is this possible??

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว +1

      Sure! You can use this code for as much buttons as you'd like 😃

    • @luissolla7990
      @luissolla7990 3 ปีที่แล้ว

      @@BasonTech thank you man! you saved my ass!

  • @jahrelubaldo9322
    @jahrelubaldo9322 3 ปีที่แล้ว

    I am having hard time on working with RGB LED. When coin cell is placed, LED is normally OFF. After pressing for about 5 seconds, the LED will be i BLINK mode. Pressing again shortly it will be in FADE mode. And again by pressing shortly, it will be in SOLID ON mode. And pressing it again for about 5 seconds it will be in OFF mode.

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว

      I see, but what is the thing I can help you with? 😃

  • @shahzaibshamim6524
    @shahzaibshamim6524 3 ปีที่แล้ว

    I want to change this code for 8051 MCU in interrupt routine, falling edge type interrupt.

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว

      Hi, unfortunately I don't have any experience with the 8051 MCU

  • @remcobooy9837
    @remcobooy9837 2 ปีที่แล้ว

    The intervalButton=50msec is that a debounce time for the button??

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      Hi Remco, as shown in the code comment it is the time between two readings of the button state. This is not de debounce time.

    • @remcobooy9837
      @remcobooy9837 2 ปีที่แล้ว

      @@BasonTech Ik snap hem nu..Er zijn meerdere wegen naar Rome...je kunt met kortere tijd door een if() laten vallen als de millis korter is dan je voorbeeld van 3000 maar je hebt een duidelijke grens aangegeven dat dat een short press is geweest...als ie korter dan 50mSec is...wat veiliger om een onderscheid te maken tussen een short of een long press...alsk ut zo goed heb?
      Code werkt verder super..precies wat ik zocht...alleen de switch is, bij mij, active LOW maar is een kwestie van de sketch aanpassen...Dank oe iig..

    • @BasonTechNL
      @BasonTechNL 2 ปีที่แล้ว

      @@remcobooy9837 Deze code is zeer specifiek voor een long press oplossing. Voor alleen debounce kun je de code significant versimpelen. Eigenlijk hoef je alleen maar de timestamp op te slaan als knop wordt losgelaten. Om een volgende te registreren met een x aantal millis verstreken zijn sinds het moment van loslaten.

  • @rushigaikwad7683
    @rushigaikwad7683 2 ปีที่แล้ว

    Hello sir can u help me with this problem.....i want to operate 2 relay having 50ms time gap in it when i pushed a button
    i used delay function but it doesnt work can u write code with millis
    i want like this (S=switch, R=relay)
    S1 = R1(50MS)R2
    S2 = R1(50MS)R4
    S3 = R1(50MS)R3
    S4 = R2(50MS)R4
    S5 = R2(50MS)R3

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      What have you tried so far?

  • @mukundbhatia
    @mukundbhatia ปีที่แล้ว

    There's an issue in ur code for every first run if the button in pressed for a long duration in the first try ur code will not recognise it as long press instead it goes to void loop and then again performs the function to check long press

    • @BasonTech
      @BasonTech  ปีที่แล้ว

      I did not have that problem when running the code. Are you sure it is exactly the same?

  • @Seoulhawk01
    @Seoulhawk01 2 ปีที่แล้ว

    How do I do more than one button? I need to have seven buttons each with a long press.

    • @BasonTech
      @BasonTech  2 ปีที่แล้ว

      You can use a pin for each switch.

    • @Seoulhawk01
      @Seoulhawk01 2 ปีที่แล้ว

      @@BasonTech Yes I understand, but I am a novice programmer. I am going to try to spend work time this weekend trying to make two relays trigger. So button 1 = state 1 relay one on and relay two off, then button 2 = state 2 relay one off and relay two on. If I can do that, I can start adding more buttons and more relays. :)

  • @talapalasneha9367
    @talapalasneha9367 4 ปีที่แล้ว +1

    From where did you buy the button?

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว +1

      Hi sneha, every tutorial contains a part list in the description of the video 😃 So on arduino-tutorials.net/r/TmUMFO

  • @sameerk12982
    @sameerk12982 4 ปีที่แล้ว

    Hello from India....Very well explained and easy to understand tutorial...Thank you very much....appreciated.....subscribed....Looking forward for more.

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว

      Awesome, thank you! 😃

  • @heruwibowo4442
    @heruwibowo4442 3 ปีที่แล้ว

    Sir, sorry I used google translate.
    Sir, I want to use this for fans, what if after "Button long pressed" the fan will stay HIGH during minButtonLongPressDuration, after which the fan goes LOW.
    Thank you.

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว +1

      With this tutorial you should be able to make that 😃

    • @heruwibowo4442
      @heruwibowo4442 3 ปีที่แล้ว

      @@BasonTech
      Sorry sir, I'm still a beginner.
      I tried placing the script like below, but after the fan release it is always HIGH.
      static const int buttonPin = A2; // switch pin
      static const int Fan = A3; // Fan
      int buttonStatePrevious = LOW;
      unsigned long minButtonLongPressDuration = 3000;
      unsigned long buttonLongPressMillis;
      bool buttonStateLongPress = false;
      const int intervalButton = 50;
      unsigned long previousButtonMillis;
      unsigned long buttonPressDuration;
      unsigned long currentMillis;
      void setup() {
      Serial.begin(9600);
      pinMode(buttonPin, INPUT);
      pinMode(Fan, OUTPUT);
      }
      void readButtonState() {
      int buttonState = digitalRead(buttonPin);
      if(currentMillis - previousButtonMillis > intervalButton) {
      if (buttonState == HIGH && buttonStatePrevious == LOW && !buttonStateLongPress) {
      buttonLongPressMillis = currentMillis;
      buttonStatePrevious = HIGH;
      Serial.println("Button pressed: Fan ON");
      digitalWrite(Fan,HIGH);
      }
      buttonPressDuration = currentMillis - buttonLongPressMillis;
      if (buttonState == HIGH && !buttonStateLongPress && buttonPressDuration >= minButtonLongPressDuration) {
      buttonStateLongPress = true;
      Serial.println("Button long pressed:Fan ON");
      digitalWrite(Fan,HIGH);
      }

      if (buttonState == LOW && buttonStatePrevious == HIGH) {
      buttonStatePrevious = LOW;
      buttonStateLongPress = false;
      Serial.println("Button released");
      digitalWrite(Fan,HIGH);
      if (buttonPressDuration < minButtonLongPressDuration) {
      Serial.println("Button pressed shortly:Fan OFF");
      digitalWrite(Fan,LOW);
      }
      }
      previousButtonMillis = currentMillis;
      }
      }
      void loop() {
      currentMillis = millis(); // store the current time
      readButtonState(); // read the button state

      }
      Thank You

    • @BasonTech
      @BasonTech  3 ปีที่แล้ว +1

      Looks like my code. Have you tried to download my code to be sure?

    • @heruwibowo4442
      @heruwibowo4442 3 ปีที่แล้ว

      @@BasonTech
      The code I wrote above is from you, I tried to modify it so that if the Button release after a long press is detected the fan will still turn on first according to minButtonLongPressDuration then off

  • @ThuanDuong-pv3xu
    @ThuanDuong-pv3xu 4 ปีที่แล้ว

    Why didn't you use ( while (button time ) && condition ()

  • @ETs-Everend
    @ETs-Everend 4 ปีที่แล้ว

    at timestamp 10:19 - if(!buttonStateLongPress && ... since you just set this two lines above, won't it always be false? thanks for the video, this is helpful.

    • @BasonTech
      @BasonTech  4 ปีที่แล้ว

      Hi, nice find! Indeed on line 75 I make it false, so !buttonStateLongPress is always TRUE. Therefore you can remove the "!buttonStateLongPress &&" part from the if. There is no use to have this part checked. I've updated the code in the course material. Thanks for letting me know! 😃

    • @ETs-Everend
      @ETs-Everend 4 ปีที่แล้ว +1

      @@BasonTech thanks again for the video and code. I used it on a project, teaching my 10 year old son. We connected for buttons to an RGB LED, one for each color. Long press a fourth button puts it into 'record' mode, short press ends record mode and initiates playback of the sequence and duration of button pushes. Your use of the millis() function gave me the idea to measure duration of press and pause between presses. Thanks!