Press a button

Post your cool example code here.
User avatar
Rintin
Posts: 16
Joined: Sat Jan 23, 2016 11:20 am

Press a button

Postby Rintin » Thu Dec 15, 2016 1:14 am

Hi,

I needed to detect if a button is pressed once, twice or long.

For this I wrote the attached code.

The used pin is configured with the internal pullup resistor.
You only need to connect the button to the pin and ground.

Any suggestions on how to improve the code?
Attachments
button_test.zip
(1.57 KiB) Downloaded 45 times
#legalizeawoo

User avatar
mrburnette
Posts: 1774
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Press a button

Postby mrburnette » Thu Dec 15, 2016 4:01 am

Any suggestions on how to improve the code?


Not trying to be contrary, but does your code not work as you need it to work? That is, do you need assistance with the code: does it not function in some manner? Or, are you just looking for some critical comments or idle conversation?

Unless your code example is too long to post in code tags (that menu button titled 'Code'), then pasting between the tags is the preferable way to show code since downloading a Zip, uncompressing, reviewing in an editor .... Well, it is a waste of time.

Ray

User avatar
RogerClark
Posts: 5470
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Press a button

Postby RogerClark » Thu Dec 15, 2016 10:06 am

@rintin

This section is not intended for programming questions, its intended for people to post useful STM32 specific code example snippets

Is this even a STM32 specific problem.

If its a general Arduino programming question you would be better off posting to https://forum.arduino.cc/index.php?board=4.0

I've a mind to either move this thread entirely to perhaps Off Topic, or perhaps General, as its not really relevant on this forum.

User avatar
ahull
Posts: 1422
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Press a button

Postby ahull » Fri Dec 16, 2016 10:23 am

Other "Press a button" libraries are available... -> http://playground.arduino.cc/Code/Button

I took a quick look at the code in the three files in the zip file Rintin posted above. Assuming it works as intended, then it looks fine. What I would suggest is that the OP''s question was somewhat vague.

"Any suggestions on how to improve the code?"

Yes, document it fully, stick it on GitHub add some wiki details on how to use it, and perhaps a few working examples with pictures (we all like pictures of blinklights :D ). If others like it and find it useful, then they will ask you for improvements and suggest enhancements.
- Andy Hull -

zmemw16
Posts: 1067
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: Press a button

Postby zmemw16 » Fri Dec 16, 2016 9:12 pm

oh how many wheels can i get on my wagon ?
bit like programming styles, editors, ide's, languages ... ... ...
stephen

stevestrong
Posts: 884
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Press a button

Postby stevestrong » Sat Dec 17, 2016 11:03 am

Come on, guys, be gentle with Rintin, I think he did a good job, his code includes more than the Alexander's (playground) version.
Btw, this is the right place to post code snippets, what he actually did correctly in my view.

What I would propose:
- a github place for this (as Andy also suggested) - @Rintin, can you do this?
- add "double-click" functionality.
Last edited by stevestrong on Sat Dec 17, 2016 12:28 pm, edited 2 times in total.

User avatar
ahull
Posts: 1422
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Press a button

Postby ahull » Sat Dec 17, 2016 11:36 am

stevestrong wrote:Come on, guys, be gentle with Rintin, I think he did a good job,
...
- add "double-click" functionality.


As I said, the code looks fine, I couldn't think of any additions, but double click is a good suggestion.
If I came over as a trifle abrupt, that wasn't my intention, one of the down sides of text only communication. Besides there's a good reason i seem like a grumpy old fart... I AM a grumpy old fart :P Merry Christmas and Bah humbug to the lot of you... ;)
- Andy Hull -

zmemw16
Posts: 1067
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: Press a button

Postby zmemw16 » Sat Dec 17, 2016 11:45 am

which side of 7/1954, so i can agree or say behave young man :D

stephen

User avatar
ahull
Posts: 1422
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Press a button

Postby ahull » Sat Dec 17, 2016 12:24 pm

zmemw16 wrote:which side of 7/1954, so i can agree or say behave young man :D

stephen

2/1964 I'll behave... and I'm getting off your lawn... :D
- Andy Hull -

zmemw16
Posts: 1067
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: Press a button

Postby zmemw16 » Sat Dec 17, 2016 12:46 pm

ain't got a lawn :D
front is 30ft sq with side path, natural planting and a central square of paving, rear is 70ft x 30ft of non-descript concrete.
60ft of 15ft high of hedge ( a yearly short top and one side trim exercise ) and a WIP fence from other side's neighbour.

stephen


Return to “Code snipplets”

Who is online

Users browsing this forum: No registered users and 1 guest