I'm creating a morse code decoder, this tutorial was very useful even with a straight key or a paddle. Thank you!
@BasonTech
4 жыл бұрын
Great to hear I could help! 😃
@jaimevaldez3058
5 жыл бұрын
Best long button tutorial I have seen.
@BasonTech
4 жыл бұрын
Thanks! 😃
@dextercloser4788
3 жыл бұрын
Academic explanation, Nice work dude
@BasonTech
3 жыл бұрын
Thanks! 😃
@danielsaenz5570
Жыл бұрын
This video is amazing! Thanks for making it!
@BasonTech
Жыл бұрын
Thanks, my pleasure! 😃
@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.
@danielsaenz5570
Жыл бұрын
Great video! I don't understand why there is the 'buttonStateLongPress' variable. If you could explain that, that would be great! Thanks!! 👍
@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
2 жыл бұрын
That is so awesome, great work! 😃
@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
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.
@cchobbyfun1
2 жыл бұрын
Really well done tutorial. Using this code is saving me a lot of time in a project.😀
@BasonTech
2 жыл бұрын
Great! As long as you know how the code works 😉
@ipadize
2 жыл бұрын
whats the advantage of using a external pull down resistor vs using the internal pull up of the arduino?
@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
Жыл бұрын
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
Жыл бұрын
What have you tried so far, and where do you get stuck?
@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
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
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.
@petebevan
4 жыл бұрын
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
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
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.
@micodatugan7724
4 жыл бұрын
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
4 жыл бұрын
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
4 жыл бұрын
Thank you sir. I've been struggling for the condition for the 2nd button 😭. But anyways thank you for the idea.
@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
2 жыл бұрын
I did not have that problem when running the code. Are you sure it is exactly the same?
@prateekbhardwaj6720
2 жыл бұрын
Thanks bas tech with this i programmed my atmel mcu to make a smd rework station like 861dw
@BasonTech
2 жыл бұрын
Great to hear! 😃
@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
4 жыл бұрын
I see, but what is the thing I can help you with? 😃
@cksmaya
3 жыл бұрын
THANK YOU
@BasonTech
3 жыл бұрын
My pleasure! 😃
@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
3 жыл бұрын
With this tutorial you should be able to make that 😃
@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
3 жыл бұрын
Looks like my code. Have you tried to download my code to be sure?
@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
@sameerk12982
4 жыл бұрын
Hello from India....Very well explained and easy to understand tutorial...Thank you very much....appreciated.....subscribed....Looking forward for more.
@BasonTech
4 жыл бұрын
Awesome, thank you! 😃
@ask_fornature4012
Жыл бұрын
Thanks a bunch
@BasonTech
Жыл бұрын
My pleasure! 🚀
@remcobooy9837
3 жыл бұрын
The intervalButton=50msec is that a debounce time for the button??
@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
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
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.
@luissolla7990
4 жыл бұрын
Thanks! I'am doing a project and i need this code for two buttons, is this possible??
@BasonTech
4 жыл бұрын
Sure! You can use this code for as much buttons as you'd like 😃
@luissolla7990
4 жыл бұрын
@@BasonTech thank you man! you saved my ass!
@talapalasneha9367
4 жыл бұрын
From where did you buy the button?
@BasonTech
4 жыл бұрын
Hi sneha, every tutorial contains a part list in the description of the video 😃 So on arduino-tutorials.net/r/TmUMFO
@Seoulhawk01
2 жыл бұрын
How do I do more than one button? I need to have seven buttons each with a long press.
@BasonTech
2 жыл бұрын
You can use a pin for each switch.
@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. :)
@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
4 жыл бұрын
Hi Shrikant, this should be possible. You then include your functions via the separated file.
@shrikantnikam2426
4 жыл бұрын
@@BasonTech but how?
@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.
@GeorgeJFW
5 жыл бұрын
Nice man I am working on Something that could use something like this
@BasonTech
5 жыл бұрын
Great to hear this can help you with your project! 😃
@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
4 жыл бұрын
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 😃
@ThuanDuong-pv3xu
4 жыл бұрын
Why didn't you use ( while (button time ) && condition ()
@BasonTech
4 жыл бұрын
Instead of?
@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
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
4 жыл бұрын
@@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!
@shahzaibshamim6524
4 жыл бұрын
I want to change this code for 8051 MCU in interrupt routine, falling edge type interrupt.
@BasonTech
4 жыл бұрын
Hi, unfortunately I don't have any experience with the 8051 MCU
Пікірлер: 73