Problem with rotary library on arduino mega

Good morning everyone,
I need to use this encoder library that I use successfully on arduino 1 with this sw (the original is here: www.ad7c.com) to manage a command menu for a dds, I would like to use it with arduino mega but it doesn't work.
I immediately thought of the HW interrupt pins 2 and 3 and looking at the mega datasheet I tried pins 18 and 19, then A11 and A12 which correspond to PCINT19 and PCINT20 (defined in the library) but it still doesn't go.

Do you have any ideas?

I attach code:

/*
Main code by Richard Visokey AD7C - www.ad7c.com
Revision 2.0 - November 6th, 2013
used and midified by IZ1QXJ Antonio
*/

// Include the library code
#include <LiquidCrystal_I2C.h>
#include <rotary.h>
#include <EEPROM.h>

//Setup some items

#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
LiquidCrystal_I2C lcd(0x3f, 16, 2);//0x3f o 0x27

int_fast32_t rx=7150000; // Base (starting) frequency of VFO.  This only loads once.  To force load again see ForceFreq variable below.
int_fast32_t rx2=1; // variable to hold the updated frequency
int_fast32_t increment = 10; // starting VFO update increment in Hz.!!!!!!!!!
int_fast32_t iffreq = 4192000; // Intermedite Frequency - Amount to subtract (-) from base frequency. ********************************************
int buttonstate = 0;
int buttonstate2 = 0;
int GoIF = 1;
String hertz = "10 Hz";
int  hertzPosition = 5;
byte ones,tens,hundreds,thousands,tenthousands,hundredthousands,millions ;  //Placeholders
String freq; // string to hold the frequency
int_fast32_t timepassed = millis(); // int to hold the arduino miilis since startup
int memstatus = 1;  // value to notify if memory is current or old. 0=old, 1=current.

int ForceFreq = 1;  // Change this to 0 after you upload and run a working sketch to activate the EEPROM memory.  YOU MUST PUT THIS BACK TO 0 AND UPLOAD THE SKETCH AGAIN AFTER STARTING FREQUENCY IS SET!

// 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
// pinMode(A5,INPUT); // IF sense **********************************************
  digitalWrite(A0,HIGH);
  //digitalWrite(A5,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(); 
  }
}

void loop() {   
  // Update the display and frequency if the new Freq NEQ the old Freq 
  if (rx != rx2){   
        showFreq();
//        sendFrequency(rx);

  Serial.println(rx);
delay(500);
        rx2 = rx;       

      }
     

       
  // Rotate through the rate of tuning as you hold down the button
  buttonstate = digitalRead(A0);
  if(buttonstate == LOW) {
        setincrement();       
    };
   

    // Write the frequency to memory if not stored and 2 seconds have passed since the last frequency change.
    if(memstatus == 0){   
      if(timepassed+2000 < millis()){
        storeMEM();
        }
      } 
   
}
 
// Interrupt routine to catch the rotary encoder
ISR(PCINT2_vect) {
  unsigned char result = r.process();
  if (result) {   
    if (result == DIR_CW){rx=rx+increment;}
    else {rx=rx-increment;};       
      if (rx >=30000000){rx=rx2;}; // UPPER VFO LIMIT
      if (rx <=1000000){rx=rx2;}; // LOWER VFO LIMIT
  }
}

void setincrement(){
  if(increment == 10){increment = 50; hertz = "50 Hz"; hertzPosition=5;}
  else if (increment == 50){increment = 100;  hertz = "100 Hz"; hertzPosition=4;}
  else if (increment == 100){increment = 500; hertz="500 Hz"; hertzPosition=4;}
  else if (increment == 500){increment = 1000; hertz="1 kHz"; hertzPosition=6;}
  else if (increment == 1000){increment = 2500; hertz="2.5 kHz"; hertzPosition=4;}
  else if (increment == 2500){increment = 5000; hertz="5 kHz"; hertzPosition=6;}
  else if (increment == 5000){increment = 10000; hertz="10 kHz"; hertzPosition=5;}
  else if (increment == 10000){increment = 100000; hertz="100 kHz"; hertzPosition=4;}
  else if (increment == 100000){increment = 1000000; hertz="1 MHz"; hertzPosition=6;} 
  else{increment = 10; hertz = "10 Hz"; hertzPosition=5;}; 
   lcd.setCursor(0,1);
   lcd.print("                ");
   lcd.setCursor(hertzPosition,1);
   lcd.print(hertz);
   delay(250); // Adjust this delay to speed up/slow down the button menu scroll speed.
};

void showFreq(){
    millions = int(rx/1000000);
    hundredthousands = ((rx/100000)%10);
    tenthousands = ((rx/10000)%10);
    thousands = ((rx/1000)%10);
    hundreds = ((rx/100)%10);
    tens = ((rx/10)%10);
    ones = ((rx/1)%10);
    lcd.setCursor(0,0);
    lcd.print("                ");
   if (millions > 9){lcd.setCursor(1,0);}
   else{lcd.setCursor(2,0);}
    lcd.print(millions);
    lcd.print(".");
    lcd.print(hundredthousands);
    lcd.print(tenthousands);
    lcd.print(thousands);
    lcd.print(".");
    lcd.print(hundreds);
    lcd.print(tens);
    lcd.print(ones);
    lcd.print(" MHz  ");
    timepassed = millis();
    memstatus = 0; // Trigger memory write
};

void storeMEM(){
  //Write each frequency section to a EPROM slot.  Yes, it's cheating but it works!
   EEPROM.write(0,millions);
   EEPROM.write(1,hundredthousands);
   EEPROM.write(2,tenthousands);
   EEPROM.write(3,thousands);
   EEPROM.write(4,hundreds);       
   EEPROM.write(5,tens);
   EEPROM.write(6,ones);   
   memstatus = 1;  // Let program know memory has been written
};

Thanks in advance

bye
Antonio

why do you need it? can't you use something else if this one does not work?

I use the encoder library and never got into any kind of trouble with it

You can enable interrupts using attachInterrupt function.

Look this:

Hi, @ADuri
you use this code with arduino UNO and it works correctly.
But when using with MEGA it doesn't work. Correct?

Hi!

Are you asking to me?

I have it working on Mega.

Using Rotary library we can't call r.begin() when using attachInterrupt.

Best regards.

Sorry,
No, the question is for @ADuri
I corrected my post.

OP stated:

I would like to use it with arduino mega but it doesn't work

@ADuri

This means arduino UNO?

Yes this code works with arduino 1 rev. 3 but with arduino mega 2560 dosn't works. bye Antonio

Yes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.