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