How to Make One Button Have the Functionality of Two or More with Arduino
Vložit
- čas přidán 3. 08. 2024
- 🤩 FREE Arduino Crash Course 👇👇
bit.ly/get_Arduino_skills
**If you like this, I think you'll like the premium Arduino training we offer. Check it out here**
bit.ly/3nSBPUs
We designed this circuit board for beginners!
Kit-On-A-Shield: amzn.to/3lfWClU
SHOP OUR FAVORITE STUFF! (affiliate links)
---------------------------------------------------
Get your Free Trial of Altium PCB design Software
www.altium.com/yt/programming...
We use Rev Captions for our subtitles
bit.ly/39trLeB
Arduino UNO R3:
Amazon: amzn.to/37eP4ra
Newegg: bit.ly/3fahas8
Budget Arduino Kits:
Amazon:amzn.to/3C0VqsH
Newegg:bit.ly/3j4tISX
Multimeter Options:
Amazon: amzn.to/3rRo3E0
Newegg: bit.ly/3rJoekA
Helping Hands:
Amazon: amzn.to/3C8IYXZ
Newegg: bit.ly/3fb03X1
Soldering Stations:
Amazon: amzn.to/2VawmP4
Newegg: bit.ly/3BZ6oio
AFFILIATES & REFERRALS
---------------------------------------------------
►Audible Plus Free trial: amzn.to/3j5IGrV
►Join Honey- Save Money bit.ly/3xmj7rH
►Download Glasswire for Free:bit.ly/3iv1fql
FOLLOW US ELSEWHERE
---------------------------------------------------
Facebook: / programmingelectronics...
Twitter: / progelecacademy
Website: www.programmingelectronics.com/
Follow This Link to More Resources:
programmingelectronics.com/ma...
Further Reading:
Do you have an application where you want multiple buttons for different user inputs? Maybe you have a timer and you want one button for minutes and another for hours.
But there is a problem - you only have room for one button!
In this tutorial, we are going to use Arduino to explore how to make one button have the functionality of two or more.
****If you like this, I think you'll like the premium Arduino training we offer. You can check it out here**** bit.ly/3lHyzcB
Your videos are some of the best on CZcams. The way you present the information, the detail you go through to explain every part of the code is just EXCELLENT. I hope to learn from your style when I make videos. Won't ever be as good as yours, but I can hope!!! Nice work!!!
This video may seem to be old 8 years back but helpful until tomorrow. Thanks for the video.
Glad to hear it has aged well!
This helped me out a lot with my menu scrolling script, so I no longer get the skipping due to longer key presses. Any press between 10 and 3000ms now count as one step in the navigation.
Thank you very much, this is a great video!
int pinswich = 2;
int ledpin = 13;
int state = LOW;
int a = LOW;
void setup(){
Serial.begin(9600);
Serial.setTimeout(10);
pinMode(ledpin, OUTPUT);
pinMode(pinswich, INPUT_PULLUP);
digitalWrite(ledpin, LOW);
}
void loop(){
while (digitalRead(pinswich) == HIGH)
{
a = HIGH;
delay(10);
}
if ((a == HIGH) && (state == HIGH))
{
digitalWrite(ledpin, HIGH);
Serial.println("on");
state = LOW;
a = LOW;
}
else if ((a == HIGH) && (state == LOW))
{
digitalWrite(ledpin, LOW);
Serial.println("of");
state = HIGH;
a = LOW;
}
}
Thank you so much for this amazing information! I just successfully finished my first complex control structured algorithm and project. Thanks to your videos. I've been watching your videos every day for about a month now. And I can now program a device to use several buttons that does many different functions based on many inputs. My next goal is to add an LCD screen to the project. And with your videos, I'm sure I will accomplish that goal in no time. Thank you so much. 🙏
Very cool! Progress feels so good!
Thank you! The level of detail and explanation in your vid is phenomenal.
Glad you found it helpful!
You're a lifesaver great video as always
Good video and good explanation. It will be great to add a delay time to set the led on for certain time. Than it can be used for garage door opening
I looked at the thumbnail and went "man, I can burn an arduino with 1 button?"
I am using this code to change numbers on a seven segment display and it didn't seem to be working too well until I changed the while statement from "while (digitalRead(buttonPin) == LOW)" to "while (digitalRead(buttonPin) == HIGH)". After that it worked perfectly. Thank you so much for your help!
That's what I was looking for my project. I just had to remove the delay so I could use it! Awesome.
+Caio Poit Great, glad it could help.
Thank you very much for this tutorial it was very clear and so it was very useful for me and the functionality of button . Note : I get the tutorial by EMail
Merci beaucoup pour ce tutoriel c'était très clair et donc c'était très utile pour moi et la fonctionnalité du bouton.
really appreciate this, I managed to apply this logic to my application using a sensor switch by just changing the while loop signal to HIGH. Really appreciate this. Thanks so much :)
Glad it helped! Thanks for watching!
Very informative and interesting. I loved it very much 👍.
I was wondering why mine wouldn't work, but then I remembered the Pomegranate. I went out and bought one, put it on the desk next to me and my Arduino and it still didn't work? Turns out the pomegranate wasn't ripe so I had to go out and get another one. I got a ripe one, put it on the desk next to me and surely it worked! Somewhat annoying that the pomegranate must be ripe but otherwise great tutorial!
It's those details that are always tripping me up...thanks for pointing that out!
int pinswich = 2;
int ledpin = 13;
int state = LOW;
int a = LOW;
void setup(){
Serial.begin(9600);
Serial.setTimeout(10);
pinMode(ledpin, OUTPUT);
pinMode(pinswich, INPUT_PULLUP);
digitalWrite(ledpin, LOW);
}
void loop(){
while (digitalRead(pinswich) == HIGH)
{
a = HIGH;
delay(10);
}
if ((a == HIGH) && (state == HIGH))
{
digitalWrite(ledpin, HIGH);
Serial.println("on");
state = LOW;
a = LOW;
}
else if ((a == HIGH) && (state == LOW))
{
digitalWrite(ledpin, LOW);
Serial.println("of");
state = HIGH;
a = LOW;
}
}
Exactly what I need right now
Glad it hit helped!
hi P.E.A. (Programming Electronics Academy) hope the short is good for you :p
Thank you for this amazing videos and tutorials!!
Hope i can see more of them in future
+Sasko Karabojoski Thanks for watching!
Great video!!!
Thnx man
nice, clear, well explained, thanks a lot!
Profesional explanation....mill gracias....me estás ayudando mucho.
Thanks so much!
Excellent, thanks!
Thanks for watching!
This is soooooo helpful. Thank you. I was going to add two buttons to my project, but now I'm smarter and only need one!
By the way, what program are you using to design your breadboard diagrams. I'm taking actual pictures to remind me and figure things out. It's a messy process and would love to use a program to do that for me. Thanks.
He's using a software called fritzing.
you can also mutiply antrance with logic gates, there are some devices ready for it. I think sn74hc595n. multiply 3 to 8
AWESOME,i little doubt, doesnt the current in the circuit run from positive to negative , so im not sure why the resistor is connected the other way
Nice video - THX !
I would prefere NOT to use delay() and instead check the 'delay-time' against the current time in millis() - right ?
I was able to extend the count for the button presses and add a short one to reset/turn off the LED's. I also removed the Serial command so I could push to one of my attiny85's I seem to like. I'm thinking of creating a circuit to include to relays instead of the LED's so I can use one button on a motorcycle handlebar to control two external light circuits (driving and fog lights perhaps). Medium press to turn on one circuit, longer press for second. Somewhat quick press turns off both. Thanks again.
Awesome! Glad it could be of use.
perfect video . thank u
Thanks so much for watching!
Do not use delay (if you use more buttons it will not work right?) i use: in the loop unsigned long currentMillis=millis(); if((unsigned long)(currentMillis - btn1previousMillis) >= btn1interval){//handle led and pressed boolean (overflow safe) }
I have 2 questions;
1. How do you write a program to create a double click function on the button to have the motor turn a certain amount of degrees, hold it as long as you hold down the button, than rewind back the exact way when you release it?
2. Could and how do you program/solder that circuit into smaller arduino models?
Im wanting to use a toggle switch to activate a servo. I want to do this to make a door lock but I cant seem to find a tutorial for this. Is it the same like this or is it a whole different code?
Wait so what was the pomegranate for?
@Programming Electronics Academy, so I painstakingly copied then modified the "opensource" code from your video, then tried to modify an off sequence by writing an option 3 big suprise got compile errors, I'll try to work that out with hours of other utube videos, but I cant seem to find out when putting pin 2 in a pull up state, does it remain in that state even after uploading a new sketch that doesnt use pin 2? or after uploading one that doesn't use pin 2? can a noob just use an external resistor? (i think I might be able to answer that my self after watching one more time) and does float refer to a pin being in neither a pull up or pull down state? any how thanks for the vid.
very interesting
Really cool :) Thanks :D
+Nikola Nikolov Thanks for watching - I hope you like the other videos too.
:)
Great trick.
Thanks Chanda!
Nice...
great thanks.
Thanks a ton for watching!
nice, I think with another time setting you can also turn the lights off.
snaprollinpitts Absolutely, that is actually part of the challenge at the end.
So could the while loop adding +100 also be thought as a form of "debounce" to the switch press?
Rick Duncan Yes, it definitely could. Great point.
pressLength_milliseconds should be an int, not a float. We aren't measuring or keeping track of fractions of a millisecond.
Sad But Mad Lad Very true. That was a remnant of me messing around, and actually wanting a long, but instead using float by mistake. Great point.
I have a query, I tried to print the time of pressing using the same logic, but with if statement, but it shows only 100 after every 100milliseconds, can u plz explain?
I wonder if this code won't block my other code to fill my water reservoir and my other code to control lights.
Great video!!!
However I need some clarification.
You have option 1 set at =>100 to activate the red LED.
You then have option 2 set at =>2000 to activate the green LED.
So when you press the button the first time for 2500ms, why does the red AND green LED not turn on. because the statement => 100 should turn on the red LED, since 2500ms is > 100.
where is the part of your code that specifies the top limit for an option.
SO if I have 3 options, can I specify something that states
option1 =>100 but no higher than 1999ms
option2 =>2000 but no higher than 2999ms
option3 =>3000 but no higher than 3999ms
and so on and so on..
the 3 options above, without defining a "no higher" option, if I press the button for 4000ms, then why would it not turn on all 3 options since 4000ms is => all of my options.
Great question Mike. Since we are using an IF/ELSE statement in this sketch, only one of the if statements gets executed.
If we had to separate if statement, one after, then the issue you mention would be a problem.
Hope this helps some.
Ja zrobiłem o wiele prostrzy sposób, w moim kodzie też możesz dodać jak długo przycisnołeś guzik i wybrać opcje względem czasu kliknięcia.
int pinswich = 2;
int ledpin = 13;
int state = LOW;
int a = LOW;
void setup(){
Serial.begin(9600);
Serial.setTimeout(10);
pinMode(ledpin, OUTPUT);
pinMode(pinswich, INPUT_PULLUP);
digitalWrite(ledpin, LOW);
}
void loop(){
while (digitalRead(pinswich) == HIGH)
{
a = HIGH;
delay(10);
}
if ((a == HIGH) && (state == HIGH))
{
digitalWrite(ledpin, HIGH);
Serial.println("on");
state = LOW;
a = LOW;
}
else if ((a == HIGH) && (state == LOW))
{
digitalWrite(ledpin, LOW);
Serial.println("of");
state = HIGH;
a = LOW;
}
}
how do you turn off the two lights?
Excellent..!!
But Can we operate two different LED's using single button?? I mean when i'll press the button, 1st LED should glow and it should be turn off after few second... and when i'll press this button for 2nd time, 2nd LED should glow and it should be turn off like 1st . so on... what to do??
+Anand Dalal Yes, that is very doable. You might use a variable that toggles back and forth each button press, and two separate if statements with conditions that blink the LED accordingly. Hope this helps some.
+Programming Electronics Academy hi, Could you please give an example?
int pinswich = 2;
int ledpin = 13;
int state = LOW;
int a = LOW;
void setup(){
Serial.begin(9600);
Serial.setTimeout(10);
pinMode(ledpin, OUTPUT);
pinMode(pinswich, INPUT_PULLUP);
digitalWrite(ledpin, LOW);
}
void loop(){
while (digitalRead(pinswich) == HIGH)
{
a = HIGH;
delay(10);
}
if ((a == HIGH) && (state == HIGH))
{
digitalWrite(ledpin, HIGH);
Serial.println("on");
state = LOW;
a = LOW;
}
else if ((a == HIGH) && (state == LOW))
{
digitalWrite(ledpin, LOW);
Serial.println("of");
state = HIGH;
a = LOW;
}
}
This is great, but how do I turn off the LEDs one at a time, or all at once when I'm done with them?
You could, have them turn off in the if else statement. So option 2 turns off option 1, option 1 turns off 2. Then a press of 4000+ could turn off both
Hi, I think this is a really great video and I did everything told here, however, my board was still having some compiling issues and then the board itself executing the code, even if I uploaded the code multiple times. Any Tips?
int pinswich = 2;
int ledpin = 13;
int state = LOW;
int a = LOW;
void setup(){
Serial.begin(9600);
Serial.setTimeout(10);
pinMode(ledpin, OUTPUT);
pinMode(pinswich, INPUT_PULLUP);
digitalWrite(ledpin, LOW);
}
void loop(){
while (digitalRead(pinswich) == HIGH)
{
a = HIGH;
delay(10);
}
if ((a == HIGH) && (state == HIGH))
{
digitalWrite(ledpin, HIGH);
Serial.println("on");
state = LOW;
a = LOW;
}
else if ((a == HIGH) && (state == LOW))
{
digitalWrite(ledpin, LOW);
Serial.println("of");
state = HIGH;
a = LOW;
}
}
Can you do same thing with out arduino?
Hi... How can i do if two buttons needed?
hello can you help me to write a code do the following
we have one switch and four leds
the first click make 1 led work
the second click make 2 leds work
the third click make 3 leds work
the fourth click make the 4 leds work
What if I want to change the "program" with one button by one push. Not a timed press.
I want to make one button change between a steady light, a blink example and a random blink example.
int pinswich = 2;
int ledpin = 13;
int state = LOW;
int a = LOW;
void setup(){
Serial.begin(9600);
Serial.setTimeout(10);
pinMode(ledpin, OUTPUT);
pinMode(pinswich, INPUT_PULLUP);
digitalWrite(ledpin, LOW);
}
void loop(){
while (digitalRead(pinswich) == HIGH)
{
a = HIGH;
delay(10);
}
if ((a == HIGH) && (state == HIGH))
{
digitalWrite(ledpin, HIGH);
Serial.println("on");
state = LOW;
a = LOW;
}
else if ((a == HIGH) && (state == LOW))
{
digitalWrite(ledpin, LOW);
Serial.println("of");
state = HIGH;
a = LOW;
}
}
Try use variable like memory. function1 function2 and so one, and push button in seperate function. This way procesor will jump at whole code, and will du also led blinking. Remember to not use delay for blinking, but counting variable, and when will reach 200 then blinking will change.
You dont need use milis() if You doint care about timing. Simly use " count = count +1 "
Sir Please help me So how to blink and fade a led with one button with this way, when press first time button led will blinking and when press some second same led stop and fade function will start. So I need one button and one led but function are two one is blind and one is fade. Please help me sir.
int pinswich = 2;
int ledpin = 13;
int state = LOW;
int a = LOW;
void setup(){
Serial.begin(9600);
Serial.setTimeout(10);
pinMode(ledpin, OUTPUT);
pinMode(pinswich, INPUT_PULLUP);
digitalWrite(ledpin, LOW);
}
void loop(){
while (digitalRead(pinswich) == HIGH)
{
a = HIGH;
delay(10);
}
if ((a == HIGH) && (state == HIGH))
{
digitalWrite(ledpin, HIGH);
Serial.println("on");
state = LOW;
a = LOW;
}
else if ((a == HIGH) && (state == LOW))
{
digitalWrite(ledpin, LOW);
Serial.println("of");
state = HIGH;
a = LOW;
}
}
what programs are you using to digitally create the circutboard
fritzing
can you make morse code with only one button?
Presslengthparagraph = Presslengthparagraph + 100;
SAME AS
Presslengthparagraph += 100;
yes but doesnt while slows your program down ?
im a n00b so its a stupid question probably but one thing i dont understand is why is press_Length_milliSeconds a float? when its not a float
Not stupid at all! In fact, you are correct in being suspicious as to why that is a float. It should really be a "long" data type, and I think that must have been an overlooked error - my apologies on that! The reason we want it a long is because milliseconds is a value that can grow very rapidly, and we want space to store a very large number.
Thank you for an answer. I really thought that there’s some special reason I’m not aware of lol
Hi sir
How can stop Both LED by same button
and Add one more button???
This is what I did. I use 'option3' to write both pins to low. I did a lot of messing around...but if you copy/paste and set the Arduino up as shown in the video I'm 99% what I have here will work for you too. This has a quick press turning on pin 12 and turning pin 13 off, a longer press turning on pin 13 and turning 12 off, and a longer-er press turning off both 12 and 13.
float presslength = 0;
int option1 = 100;
int option2 = 1000;
int option3 = 2000;
int buttonpin = 2;
int led1 = 12;
int led2 = 13;void setup() {
pinMode(buttonpin, INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.begin(9600);
}void loop() {
while(digitalRead(buttonpin) == LOW) {
delay(50);
presslength = presslength + 50;
Serial.print(presslength);
}
if(presslength >= option3) {
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
}
else if(presslength >= option2) {
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);
}
else if(presslength >= option1) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
}
presslength = 0;
}
@@pengwin9 with your help I'm driving the three speeds of my home ventilation with one button.
Thanks man
here's the problem.... while your program "delays".... no other code can run.... so if you were sampling something every 50ms this will totally screw things up..... delays are to be avoided at all costs
Oh boy... Never ever use loops to ask for buttons. Either use interrupts, or the more common way is to poll the button by a timer...
It will be nice if you make a video to see. Else talk is cheap
Pretty ugly. Delays are evil.
lol, why do you need a pomegranate?
Pomp pomp was for laughs prove me wrong in comments
CANNOT HEAR YOU!!!!!!!!!!!!!!!
TURN THE VOLUME UP
Huh? What?! Speak up! The volume of your voice is sooooo much lower than the intro music or other vids. Seriously though, its so low my phone cant play it loud enough, and I'm in a room with no other noise. Not a problem with pretty much every other video out there.