Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



different interrupt...
 
Share:
Notifications
Clear all

[Solved] different interrupt manner between arduino 1 vs arduino mega

8 Posts
2 Users
0 Reactions
925 Views
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

I used a rotary library in arduino 1 and It works but in arduino mega It dosn't work.

I try the pins 2 and 3 commons to each boards as interrupt.

 

a part of the code is indicated below:

#include <rotary.h>


// Rotary(Encoder Pin 1, Encoder Pin 2, Button Pin)
Rotary r = Rotary(2, 3, A0);

void setup() {
  pinMode(A0,INPUT); // Connect to a button that goes to GND on push
digitalWrite(A0,HIGH);
 
  lcd.begin();
   lcd.backlight(); //accende la retroilluminazione
  PCICR |= (1 << PCIE2);
  PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
  sei();
  Serial.begin(9600);
lcd.setCursor(hertzPosition,1);   
  lcd.print(hertz);

  // Load the stored frequency 
  if (ForceFreq == 0) {
    freq = String(EEPROM.read(0))+String(EEPROM.read(1))+String(EEPROM.read(2))+String(EEPROM.read(3))+String(EEPROM.read(4))+String(EEPROM.read(5))+String(EEPROM.read(6));
    rx = freq.toInt(); 
  }
}

.......

 

Thanks in advance

Antonio


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2796
 

It's been a very long time that I played with a rotary switch, but I think the biggest, and I've never used the Rotary library (I've found a few, but I'm assuming it is this rotary lib?), so I'm not sure what I'm even looking at.

Pins 2 and 3 seem a good choice since most common Arduino's support these 2 pins for attaching an interrupt to.

When looking at this example code for rotary.h; I see no purpose of A0?

/*
    Rotary Encoder - Interrupt Example
    
    The circuit:
    * encoder pin A to Arduino pin 2
    * encoder pin B to Arduino pin 3
    * encoder ground pin to ground (GND)
*/

#include <Rotary.h>

Rotary r = Rotary(2, 3);

void setup() {
  Serial.begin(9600);
  r.begin();
  PCICR |= (1 << PCIE2);
  PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
  sei();
}

void loop() {

}

ISR(PCINT2_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");
  }
}

   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

A0 is used to detect the push botton on the mechanical encoder.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2796
 

With limited info and zero experience with the rotary library, I can only throw out some random thoughts.

From the rotary lib documentation, I assumed the rotary lib would already handle the interrupts. I could be very wrong though.

Next, you say it works on an Arduino Uno (assuming this is what you mean with Arduino 1???) but not on the mega it does not.
Does the rotary example code for interrupts work?  (the one I just posted - copied from the rotary library documentation)


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

Thank you very much,

I try the sample inside of the Rotary library and It works.

The problem is in the setting of this register, probably Arduino one rev. 3 works in different manner of mega:

PCICR |= (1 << PCIE2);
  PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
  sei();

 

I must study the register and their use.

 

Bye

Antonio

 

 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2796
 

Oh cool! Good to know 👍🏻
Also thank you for posting the hint where to look -- others will benefit from it I'm sure.

Just being curious though; did you run the test on the Uno and the Mega? And both worked?
Just wondering if the additional "A0" causes and unexpected problem on the Mega - It's not part of the rotary, right?


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

I test with arduino mega and It works. 

The call to the Rotary function accepts 3 arguments, the first two being the interrupt pins coming from the encoder and the third the pressed button present in the mechanical encoder.

 

 

// Initialize the Rotary object
// Rotary(Encoder Pin 1, Encoder Pin 2, Button Pin)
Rotary r = Rotary(2, 3, A0);

bye 

Antonio


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2796
 

Ah OK ... see this is where it is good to know where the library came from.
And like I said: no experience with that library.

Additionally, not the type of rotary switch I was even thinking of.

I was thinking of one of these: (very old school model, source)  😜 


   
ReplyQuote
Share: