On my other project I am using Nano, LCD connected via i2c, encoder and two buttons.
The encoder is connected to pins A2 and A3 and is controlled by this code:
In my current project I need more memory and more interrupts so I decided to use Mega instead of Nano.
I am going to use pins 20 and 21 for i2c, pins 2, 3, 18 and 19 for buttons. So I need two more interrupts for encoder.
Could you, please, help me with translation the above code for Mega? I still struggle to understand those pin change interrupts.
Thanks for advice. As far as I remember I tested that library before.
Unfortunately pins 20 and 21 will be used for the i2c bus, so only 4 interrupts left. And all 4 will be used for buttons input. That means for the encoder I need another 2 interrupts. That's why I am asking for PCINTs...
At this moment I need 3 buttons plus the encoder button. if I make everything working, I'll probably need even more inputs.
Possibly it can be done in another way, but with my experience it would be rather difficult.
loop() is mainly used to call other functions. Probably the longest operation would be the functions that display user interface and results on LCD.
So you are probably right - reading pins status instead of using interrupts is an option. But from my limited experience it looks like that could causing errors - I noticed the counters could be inaccurate. I understand that might be a code issue - I am not a programmer, so I know my code is far from optimal.
But there is another reason to do use interrupts - this project is based on another one, lot of the code is already written and tested and that code uses interrupts.
I know I could use library to sort my problem out but I already know my project will need memory so adding another library is rather not a good idea.
Michael_L:
Could you, please, help me with translation the above code for Mega? I still struggle to understand those pin change interrupts.
Thank you in advance.
The 24 pin change interrupts are on completely different set of pins on the Mega, and some of these aren't brought out at all.
This means pins 10..15, 50..53 and A8..A15, in other words only 18 are available, only on those pins. The datasheet for the chip will give more details.
Michael_L:
I know I could use library to sort my problem out but I already know my project will need memory so adding another library is rather not a good idea.
You are already using Mathias Hertel’s library for the rotary encoder, I’m suggesting trying another one that will detect for you if the pins support or not interrupts and do the configuration for you.
Rotary need fast reaction to ensure the code captures passing through different states in a 2 or 4 step sequence when you rotate the knob otherwise it can get confused, so using interrupt there makes sense. Buttons in my experience are pressed for longer time which is usually compatible with being assessed in the loop on standard pins
There is a nice and efficient button library (text in French but library quite understandable): la librairie de @bricoleau
If you need to make things faster, reading button state with PORT instead of multiple digitalRead() will help
I’ve many interactive programs using both libraries and they work ok.