To Robin2,
I have changed according to your suggestions and I have also used different places for the change og the interrupt setting.
With no luck.
This is the full code according to you recommendations:
/* Sketch - based on:
* Bounce-Free Rotary Encoder for the Arduino Uno - see http://www.technoblogy.com/show?1YHJ
* David Johnson-Davies - www.technoblogy.com
*******************************************************************************************************/
#include "SSD1306Ascii.h" // OLED
#include "SSD1306AsciiAvrI2c.h" // OLED
#include "Classic8x8.h" // OLED
#include "ClassicX8x16.h" // OLED
#include "ClassicX8x8.h" // OLED
SSD1306AsciiAvrI2c oled; // OLED
#define I2C_ADDRESS 0x3C // OLED
const int LED = 8;
const int EncoderA = 4;
const int EncoderB = 5;
const int EncoderAInt = PCINT20;
volatile int a0;
volatile int c0;
volatile int Count = 250;
volatile int menuActivated = true; // intended to be the variable for switching between the rotary modes
//-------------------------------------------------------------------------------------------------------
ISR (PCINT2_vect){ // Encoder control
byte a = PIND>>EncoderA & 1;
byte b = PIND>>EncoderB & 1;
byte changeVal;
if (a != a0){
a0 = a;
if (b != c0){
c0 = b;
changeVal = (a == b);
}
if (menuActivated == true) {
Count = max(min((Count + (changeVal ? 1 : -1)), 6), 3);
}
else{
Count = max(min((Count + (changeVal ? 1 : -1)), 400), 100);
}
//newEncoderVal = true;
}
}
/*
void ChangeValue (bool Up){ // Called when rotary encoder value changes
if(menuActivated == HIGH){
Count = max(min((Count + (Up ? 1 : -1)), 400), 100);} // VCA control --- max = 400 / min = 100
if(menuActivated == LOW){
Count = max(min((Count + (Up ? 1 : -1)), 6), 3); // Menu control --- max = 6 / min = 3
for(int i = 3; i < 7; i++){oled.setCursor(1, i); oled.print(" ");}
oled.setCursor(1, Count); oled.print(">");}
}
ISR (PCINT2_vect){ // Encoder control
int a = PIND>>EncoderA & 1;
int b = PIND>>EncoderB & 1;
if (a != a0){
a0 = a;
if (b != c0){c0 = b;ChangeValue(a == b);}
}
}
*/
//--- Setup ----------------------------------------------------------------------------------------------
void setup() {
pinMode(LED, OUTPUT);
pinMode(EncoderA, INPUT_PULLUP);
pinMode(EncoderB, INPUT_PULLUP);
PCMSK2 = 1<");
//while(1);
}
//-------------------------------------------------------------------------------------------------------
void Menu(){
oled.setFont(ClassicX8x8);
oled.setCursor(10, 3); oled.print("Impdance");
oled.setCursor(10, 4); oled.print("SPL Level");
oled.setCursor(10, 5); oled.print("Distortion");
oled.setCursor(10, 6); oled.print("Response");
}
void toggle(){ // Called when push menuActivatedtion is activated
switch(Count){
case 3: Impedance();break;
case 4: dB();break;
case 5: Distortion();break;
case 6: Response();break;
}
}
//------------------------------------------------------------------------------------------------------
void Impedance(){
menuActivated = false;
oled.clear();
oled.setFont(ClassicX8x8);
oled.setCursor(30, 0); oled.print("Impedance");
oled.setCursor(40, 1); oled.print("T E S T");
oled.setCursor(0, 2); oled.print(Count);
//while(digitalRead(2)== LOW);
}
void dB(){
oled.setCursor(0, 2); oled.print("SPL");
}
void Distortion(){
oled.setCursor(0, 2); oled.print("Dist");
}
void Response(){
oled.setCursor(0, 2); oled.print("Resp");
}
//--- E N D ---------------------------------------------------------------------------------------------