Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I've researched how to attach multiple buttons to a single analog input using a resistor network. Instructables has a good lesson on this: http://www.instructables.com/id/How-to-access-5-buttons-through-1-Arduino-input/?ALLSTEPS

My requirement is that I need to attach an interrupt to buttons (ie the analog input) before reading the analog value, so that all the buttons can act as a trigger.

This works for voltages in the higher range, but not in the lower range (the trigger input waits for a RISING edge).

Can any one suggest a circuit that steps a >0V+ to +5V input to a +5V output so that any button push would trigger the input?

Otherwise, does anyone have other suggestions on how to use triggers for each button?

share|improve this question

1 Answer 1

Use the analog comparator in the ATmega328P to trigger an interrupt once the input voltage rises enough to indicate a button press.

Connect the analog network to both the analog input and D6.

If your lowest analog voltage is not greater than 1.2V then apply a voltage greater than 0V but more than 40mV less than your lowest analog voltage to D7. If you have not connected the voltage then configure the comparator to use the internal bandgap reference (ACSR |= _BV(ACBG)).

Enable the comparator interrupt and capture on a rising edge (ACSR |= _BV(ACIE) | _BV(ACIS1) | _BV(ACIS0)).

Disable the digital input on D6 (AIN0), and on D7 (AIN1) if connected to a voltage reference (DIDR1 |= _BV(AIN0D), DIDR1 |= _BV(AIN1D)).

Write an ISR that does what you want on comparator trigger (ISR(ANALOG_COMP_vect) { ... }).

Now go back and read all that three more times until you understand it. As always, see the ATmega328P datasheet for details.

share|improve this answer
2  
Cool. I learned something new today. (Voted). I did not know that ATMega chips include an analog comparator, much less that you can trigger an interrupt based on comparator results. I would have built something using an op-amp, but I like the software-only solution. It looks like the ATMega2560 has the same feature. Cool! (I guess I said that...) –  Duncan C Sep 9 '14 at 0:09
    
Do you have any links to sample sketches using the analog comparator, particularly using it as an interrupt? The registers and configuration on the AVR family are a little quirky and I find a working example very helpful in understanding how to use them. (I'm an old assembler jockey, so I'm pretty good at this stuff.) –  Duncan C Sep 9 '14 at 0:13
    
Nope. But there is AVR128. –  Ignacio Vazquez-Abrams Sep 9 '14 at 0:52
    
That'll do nicely. I didn't know that the ACI flag was set when an interrupt condition was met, even if interrupts are disabled. That's very cool, and useful for writing a tight polling loop rather than an ISR. –  Duncan C Sep 9 '14 at 2:01
    
I re-read this 10 times and I still don't understand it... –  hagope Sep 9 '14 at 23:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.