Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR/ARM] Requesting help in writing code to enable single-, double-click & hold.

Not open for further replies.


Advanced Member level 2
Dec 6, 2013
Reaction score
Trophy points
Activity points

I have posted on the arduino forum about this subject since I am using arduino but so far I have no replies and I have not solved it my self jet, I bought a couple of encoders from sparkfun:
They have RGB diodes and a switch besides the encoder and I am planing to use the encoder for my lab power supply to use the RGB diodes to signal things like reaching the current limit(flashes red maybe) while steadily shinning green while all is well and the output is active etc etc

While using the switch/button to change the steps in voltage/current each step of the encoder represents(double click) or/also using the button to select option on the menu while navigating it through turning the encoder right/left(single click).
Prototype with hardware de-bouncing which is not enough for the switch:
**broken link removed**

Or something similar, that is not happening until I figure out how to do those things but I lack the programming skills as will show below. I have spent many hours on this and trying to write a class for the encoder but it has failed.

I don't want to be polling the switch and I use a external interrupt detecting rising edges( the button is internally connected to the supply voltage), a double click needs to be within 600mS within the first click and to make a click and hold the hold time needs to exceed 1200mS.
I am not experienced with programming and it is not a subject very suitable for me but I really try to develop in it.
My interrupt ISR is called switchAction() and the latest attempt looks like this:
void switchAction()
	unsigned long T = micros;      // Will not update while in ISR.
	static unsigned long oldT = 0; // Will store T for next interrupt.
	static uint8_t clickCount;     // A try to track if its first or second click.
	unsigned long repeatInterval = oldT - T; // Used to check if a click is within 600ms/60000µS.
	if(clickCount > 0 | repeatInterval < 60000)
		clickCount = 0;
	else if(clickCount != 1)
		if (T - oldT < 10000) // De-bounce
			if(hold time is les than 1200mS) // This should determine if the switch is hold or not.
				clickCount = 1;
			else if(hold time is more than 1200mS) // Should determine if switch is hold or not.
	oldT = T; // Store time for next time through ISR.

This might be a very ugly way of doing it I don't know but I could really need some assistance.


Re: [AVR/ARM] Requesting help in writing code to enable single-, double-click & hold.

I am using a arduino Due which means that thanks to the SAM3X8E every digital pin can be used as a external interrupt.
I just thought about writing a program that loops through a series of If statements checking if different flags(bool variables) is true or false and based on that execute functions.
We'll see how that goes.

Re: [AVR/ARM] Requesting help in writing code to enable single-, double-click & hold.

I have given up the external interrupt way and am going with polling instead, I will post the solution when it is something to show.


Re: [AVR/ARM] Requesting help in writing code to enable single-, double-click & hold.

According to my experience, multiple incremental encoders can be best interfaced by polling them in a timer interrupt.

The solution works even with small processors that neither have a dedicated incremental encoder interface nor sufficient interrupt capable inputs. The only condition to fulfill is that you don't turn the encoder faster than one step per interrupt tic, which works for me even in servo drive feedback. A few kHz interrupt frequency are usually sufficient.

Re: [AVR/ARM] Requesting help in writing code to enable single-, double-click &amp; hold.

Okey, When I think about polling I think of simply one time every loop call a function to check buttons&encoders but it had not occurred to me to use a timer and poll thing with specific intervals as when you use a time. Although I had come to the conclusion that I can't over look timers no more, though they are intimidating. I do know what they are and how the work but the Due's(SAM3X8E) timers have given me problems before.

I have written a library(a class within a .h & .cpp file) that for me is pretty neat, for the moment it has seven clicks which I use to change color of the encoder RGB to demonstrate but from the main sketch which uses my lib one can set what color the RGB should take on as well as what function should be called. This is my first program using function pointers but they sure are fun to use, and since I am now polling the switch I don't think I will have any trouble implementing double-clicks and click-and-hold.

But I will at the same time start looking into how to do these things with timer interrupts as suggested.
The only quirk is that the Due(SAM3X8E) functions very differently from all other Arduinos, though I am slowly but steadily crawling away from arduino into ATMEL Studio since I want to ditch arduino and do it my self in C/C++. But I do not like there ASF.
I am about to start learning about XMEGA(ATxmega) which is a really interesting family. It has all that justifies my use of the Due while still being 8bit and compatible with the old style of programing the ATmega families.

XMEGA has 12bit ADC & DAC as well as DMA's(Direct-Memory-Access) that can do things by them self's without the intervention of the CPU, and they have multiple SPI, UARTs I2C busses(at least the A series) and many more external interrupts then the ATmegas and a new event system which I don't have learned jet but it seems great. Oh and the can be run at 32MHz from a internal oscillator, and there a more cool stuff as well.
Sound great to me anyway.


- - - Updated - - -

SAM3X8E has a couple of Quadrature encoder interfaces but I have jet to figure out if they are suitable to be used for encoders that one adjusts with ones fingers in place of pots.

Not open for further replies.

Part and Inventory Search

Welcome to