PinMode(pinB, INPUT_PULLUP) // set pinB as an input, pulled HIGH to the logic voltage (5V or 3. PinMode(pinA, INPUT_PULLUP) // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases) Volatile byte reading = 0 //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent Volatile uint16_t oldEncPos = 0 //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor) Change to int or uin16_t instead of byte if you want to record a larger range than 0-255 Volatile uint16_t encoderPos = 0 //this variable stores our current value of encoder position. Volatile byte bFlag = 0 // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set) Some schematics led me to believe Pin 18 for example is Analog input 4. I have pin 2, and 3 working quite well however. However I cannot seem to find these last 4 pins. Volatile byte aFlag = 0 // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent The Mega 2560 is capable of 6 external interrupts, which are 0-5 on pins 2, 3, 21, 20, 19, 18 respectively. Static int selectSwitch = 9 //The select switch for our encoder. Static int pinB = 3 // Our second hardware interrupt pin is digital pin 3 I know this reply is a little late, I had the same problem: interrupts on pin 2, 3, 18 and 19 are no problem, but interrupts on pin 20 (SDA) and 21 (SCL) wont work. The Arduino has 3 timers and 6 PWM output pins. Static int pinA = 2 // Our first hardware interrupt pin is digital pin 2 The PWM functionality is often shared with other pin functionality. Modified at EE to include the select switch *******Interrupt-based Rotary Encoder Sketch*******īy Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence I am trying to adapt the following code for Mega, as it was originally written for, and works well with, Uno: it interfaces with a KY-040 rotary encoder to simply increments/decrement a counter based on a clockwise/counterclockwise rotation of the rotary encoder.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |