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

Поделиться
HTML-код
  • Опубликовано: 10 сен 2024

Комментарии • 73

  • @A5A5A5A5h
    @A5A5A5A5h 4 года назад +6

    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.

  • @dextercloser4788
    @dextercloser4788 3 года назад +1

    Academic explanation, Nice work dude

  • @danielsaenz5570
    @danielsaenz5570 Год назад +1

    This video is amazing! Thanks for making it!

    • @BasonTech
      @BasonTech  Год назад

      Thanks, my pleasure! 😃

  • @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! 😃

  • @fringeanomaly9284
    @fringeanomaly9284 Год назад

    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  Год назад

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

  • @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

  • @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 😉

  • @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! 😃

  • @mukundbhatia
    @mukundbhatia 2 года назад

    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  2 года назад

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

  • @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.

  • @danielsaenz5570
    @danielsaenz5570 Год назад

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

  • @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.

  • @jahrelubaldo9322
    @jahrelubaldo9322 4 года назад

    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  4 года назад

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

  • @ipadize
    @ipadize 2 года назад

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

    • @BasonTech
      @BasonTech  2 года назад +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 😃

  • @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. :)

  • @cksmaya
    @cksmaya 2 года назад

    THANK YOU

  • @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! 😃

  • @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! 😃

  • @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 😃

  • @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

  • @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.

  • @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

  • @remcobooy9837
    @remcobooy9837 3 года назад

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

    • @BasonTech
      @BasonTech  3 года назад

      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 3 года назад

      @@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 3 года назад

      @@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.

  • @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

  • @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.

  • @luissolla7990
    @luissolla7990 4 года назад

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

    • @BasonTech
      @BasonTech  4 года назад +1

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

    • @luissolla7990
      @luissolla7990 4 года назад

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

  • @ThuanDuong-pv3xu
    @ThuanDuong-pv3xu 4 года назад

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

  • @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.

  • @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!