New project, new code….

/*
Rotary encoder:
Connect the middle pin of the three to ground.
The outside two pins of the three are connected to
digital pins 2 and 3
Encoder button on pin 4 with a pulldown resistor

DFPlayer module connected to digital pins 10 and 11

OLED connected to i2c SCL/SDA

*/

#include
#include
#include “SoftwareSerial.h”
#include “DFRobotDFPlayerMini.h”

#ifdef U8X8_HAVE_HW_SPI
#include
#endif
#ifdef U8X8_HAVE_HW_I2C
#include
#endif

U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

volatile int number = 1; // Testnumber, print it when it changes value,
// used in loop and both interrupt routines
int oldnumber = 0;
char num=’ ‘;
volatile boolean halfleft = false; // Used in both interrupt routines
volatile boolean halfright = false;
const int buttonPin = 4;
int buttonState = 0; // variable for reading the pushbutton status
int old_buttonState = 0; // variable for pervious pushbutton status
int state = 0; // state
int current = 0;

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

void setup(){
mySoftwareSerial.begin(9600);
Serial.begin(9600);
pinMode(2, INPUT);
digitalWrite(2, HIGH); // Turn on internal pullup resistor
pinMode(3, INPUT);
digitalWrite(3, HIGH); // Turn on internal pullup resistor
attachInterrupt(0, isr_2, FALLING); // Call isr_2 when digital pin 2 goes LOW
attachInterrupt(1, isr_3, FALLING); // Call isr_3 when digital pin 3 goes LOW
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH); // Turn on internal pullup resistor
u8g2.begin();
Serial.println();
Serial.println(F(“Initializing DFPlayer … (May take 3~5 seconds)”));

if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F(“Unable to begin:”));
Serial.println(F(“1.Please recheck the connection!”));
Serial.println(F(“2.Please insert the SD card!”));
while(true);
}
Serial.println(F(“DFPlayer Mini online.”));
myDFPlayer.volume(20); //Set volume value. From 0 to 30

}

void loop(){
if (number<=0) number=149; if (number>=150) number=1;
if(number != oldnumber){ // Change in value ?
Serial.println(number); // Yes, print it (or whatever)

u8g2.firstPage();

num=char(number);
do {
u8g2.setFont(u8g2_font_osb21_tf);
u8g2.drawStr(18,24,”Track:”);
// u8g2.setFont(u8g2_font_logisoso32_tn);
u8g2.setFont(u8g2_font_osb35_tn);
u8g2.setCursor(24,64);
u8g2.print(number);
} while ( u8g2.nextPage() );

oldnumber = number;

}
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if ((buttonState == HIGH) && (old_buttonState == LOW)) {
state = 1 – state;
delay(10);
if ((current == number) && (state == 1)) { myDFPlayer.start();}
// if (state == 0) {myDFPlayer.pause();}
if ((current == number) && (state == 0)) {myDFPlayer.pause();}
if ((current != number) && (state == 0)) {myDFPlayer.play(number);current=number;buttonState=old_buttonState;}
if ((current != number) && (state == 1)) { myDFPlayer.play(number);current=number;}

}

Serial.println(current);
old_buttonState=buttonState;
// Serial.println(state);
}

void isr_2(){ // Pin2 went LOW
delay(1); // Debounce time
if(digitalRead(2) == LOW){ // Pin2 still LOW ?
if(digitalRead(3) == HIGH && halfright == false){ // –>
halfright = true; // One half click clockwise
}
if(digitalRead(3) == LOW && halfleft == true){ // <–
halfleft = false; // One whole click counter-
number–; // clockwise
}
}
}
void isr_3(){ // Pin3 went LOW
delay(1); // Debounce time
if(digitalRead(3) == LOW){ // Pin3 still LOW ?
if(digitalRead(2) == HIGH && halfleft == false){ // <– halfleft = true; // One half click counter- } // clockwise if(digitalRead(2) == LOW && halfright == true){ // –>
halfright = false; // One whole click clockwise
number++;
}
}
}