/* EEncoder Basic Example Demonstrates simple usage of the EEncoder library with a rotary encoder and push button for RP2040-based boards. Hardware: - Connect encoder A pin to GPIO 2 - Connect encoder B pin to GPIO 3 - Connect encoder button to GPIO 4 - Assumes encoder module has built-in pull-up resistors This example: - Increments/decrements a value with the encoder - Resets to zero on button press - Enters fine-tune mode on long press - Shows acceleration when turning quickly */ #include #include #include #include #include "Adafruit_DRV2605.h" Adafruit_DRV2605 drv; #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire1, OLED_RESET); // Pin definitions const uint8_t ENCODER_PIN_A = 5; const uint8_t ENCODER_PIN_B = 13; const uint8_t ENCODER_BUTTON = 3; // Create encoder instance // The last parameter (4) indicates this encoder produces 4 counts per physical detent // Adjust this value based on your specific encoder hardware EEncoder encoder(ENCODER_PIN_A, ENCODER_PIN_B, ENCODER_BUTTON, 4); // Variable to track the current value int currentValue = 0; bool fineTuneMode = false; bool triggered = false; // Encoder rotation callback void onEncoderRotate(EEncoder& enc) { // Get the increment - always ±1 per physical detent (normalized) int8_t increment = enc.getIncrement(); // In fine-tune mode, ignore acceleration if (fineTuneMode) { currentValue += increment; // 1 per click } else { // Normal mode - use the increment (may be accelerated to ±5) currentValue += increment; } if(currentValue > 122) currentValue = 0; if(currentValue < 0) currentValue = 122; // Print the new value Serial.print("Value: "); Serial.print(currentValue); if (fineTuneMode) Serial.print(" [FINE]"); if (abs(increment) > 1) Serial.print(" [FAST]"); Serial.println(); } // Button press callback void onButtonPress(EEncoder& enc) { // Reset value to zero fineTuneMode = false; triggered = true; Serial.println("Button pressed - haptic feedback triggered"); } // Long press callback void onLongPress(EEncoder& enc) { // Toggle fine-tune mode fineTuneMode = !fineTuneMode; Serial.print("Long press - Fine tune mode "); Serial.println(fineTuneMode ? "ON" : "OFF"); } void setup() { // Initialize serial communication Serial.begin(115200); while (!Serial) { ; // Wait for serial port to connect (needed for some boards) } Wire1.setSCL(15); Wire1.setSDA(14); // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } // Show initial display buffer contents on the screen -- // the library initializes this with an Adafruit splash screen. display.display(); delay(2000); // Pause for 2 seconds display.clearDisplay(); Serial.println("EEncoder Basic Example"); Serial.println("Rotate encoder to change value, press button to reset"); Serial.println("Long press to toggle fine-tune mode"); Serial.println("Turn quickly to see acceleration!"); // Set up the callbacks encoder.setEncoderHandler(onEncoderRotate); encoder.setButtonHandler(onButtonPress); encoder.setLongPressHandler(onLongPress); // Enable acceleration with 5x multiplier encoder.setAcceleration(true); encoder.setAccelerationRate(5); // Optional: adjust timing if needed // encoder.setDebounceInterval(5); // Faster debounce // encoder.setLongPressDuration(750); // Longer press required display.setTextSize(2); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text Wire.setSCL(17); Wire.setSDA(16); if (! drv.begin()) { Serial.println("Could not find DRV2605"); while (1) delay(10); } drv.writeRegister8(DRV2605_REG_OVERDRIVE, 1); drv.writeRegister8(DRV2605_REG_RATEDV, 255); drv.selectLibrary(1); // I2C trigger by sending 'go' command // default, internal trigger when sending GO command drv.setMode(DRV2605_MODE_INTTRIG); } void setup1(){ } String value = ""; void loop() { // Update the encoder - this must be called as often as possible encoder.update(); if(triggered){ // set the effect to play drv.setWaveform(0, currentValue); // play effect drv.setWaveform(1, 0); // end waveform // play the effect! drv.go(); delay(500); triggered = false; } } void loop1(){ display.clearDisplay(); display.setCursor(0,0); // Start at top-left corner display.println(currentValue); display.display(); delay(20); }