I am working on a fairly large project based on an ATmega2561, and seem to have caused myself some issues when I did the hardware design and PCB layout.
This is the "Megacore" pin assignments, (the core I'm using to make my ATmega2561 Arduino-compatible:
Instead of tying my encoder to the PCINTx pins, I tied my encoder to the INTx pins-which while OK, isn't exactly convenient when using Rotary.h:
The above code works as expected...Serial.println() inside the ISR is goofy and broken but it does what I ask it to do here. The issue is that this is the only case of using Buxton's Rotary.h libary that I have seen the need for twin ISR's...remove one and the micro resets every time the interrupt pin is toggled by the encoder.
The plan is to use the encoder to increment/deincrement menus, delineated by the pushbutton, so i'm only incrementing and deincrementing a variable-the pushbutton will have to select the variable that I am incrementing and deincrementing, though.
I have LOTS of examples of how to control the clock generator with the Rotary library, but all with the single ISR...so I am looking at transitioning to the "Encoder" library to maybe make things easier to work with.
Does anyone have any advice on how and why I might want to use one over the other, outside of the "twin ISR" concern?
This is the "Megacore" pin assignments, (the core I'm using to make my ATmega2561 Arduino-compatible:
Instead of tying my encoder to the PCINTx pins, I tied my encoder to the INTx pins-which while OK, isn't exactly convenient when using Rotary.h:
Code:
#include <Rotary.h>
Rotary r = Rotary(20, 21);
const byte encoderbutton = 22;
const byte button2 = 23;
const byte button3 = 24;
const byte button4 = 25;
int Encoderbutton = 0;
int Button2 = 0;
int Button3 = 0;
int Button4 = 0;
void setup() {
Serial.begin(9600);
Serial.println("started the console");
EICRA |= (1 << EICRA);
EIMSK |= (1 << INT2) | (1 << INT3);
sei();
pinMode(encoderbutton, INPUT);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
}
void loop() {
Encoderbutton = digitalRead(encoderbutton);
Button2 = digitalRead(button2);
Button3 = digitalRead(button3);
Button4 = digitalRead(button4);
if (Encoderbutton == LOW) {
// turn LED on:
Serial.println("encoderbutton pushed!");
} else {
// turn LED off:
//Serial.println("");
}
if (Button2 == LOW) {
// turn LED on:
Serial.println("Button 2 pushed");
} else {
// turn LED off:
}
if (Button3 == LOW) {
// turn LED on:
Serial.println("Button 3 pushed");
} else {
// turn LED off:
//Serial.println("");
}
if (Button4 == LOW) {
// turn LED on:
Serial.println("Button 4 pushed");
} else {
// turn LED off:
// Serial.println("");
}
}
ISR(INT2_vect) {
unsigned char result = r.process();
if (result == DIR_NONE) {
// do nothing
}
else if (result == DIR_CW) {
Serial.println("ClockWise");
}
else if (result == DIR_CCW) {
Serial.println("CounterClockWise");
}
}
ISR(INT3_vect) {
unsigned char result = r.process();
if (result == DIR_NONE) {
// do nothing
}
else if (result == DIR_CW) {
Serial.println("ClockWise");
}
else if (result == DIR_CCW) {
Serial.println("CounterClockWise");
}
}
The above code works as expected...Serial.println() inside the ISR is goofy and broken but it does what I ask it to do here. The issue is that this is the only case of using Buxton's Rotary.h libary that I have seen the need for twin ISR's...remove one and the micro resets every time the interrupt pin is toggled by the encoder.
The plan is to use the encoder to increment/deincrement menus, delineated by the pushbutton, so i'm only incrementing and deincrementing a variable-the pushbutton will have to select the variable that I am incrementing and deincrementing, though.
I have LOTS of examples of how to control the clock generator with the Rotary library, but all with the single ISR...so I am looking at transitioning to the "Encoder" library to maybe make things easier to work with.
Does anyone have any advice on how and why I might want to use one over the other, outside of the "twin ISR" concern?