diff --git a/diagrams/ec11_encoder.png b/diagrams/ec11_encoder.png new file mode 100644 index 0000000..444179c Binary files /dev/null and b/diagrams/ec11_encoder.png differ diff --git a/src/main.cpp b/src/main.cpp index 87fbfdb..d5ae9a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,25 @@ #include +#include const int ONBOARD_LED_PIN = 2; const int ANALOG_INPUT_PIN = 34; const int ANALOG_OUTPUT_PIN = 32; +const int ENTER_BUTTON_PIN = 14; +const int ENCODER_A_PIN = 12; +const int ENCODER_B_PIN = 13; + void handleToneGeneration(void *pvParameters); +void handleInputControls(void *pvParameters); bool toneOn = false; int frequency = 220; // A4 +Versatile_RotaryEncoder *encoder; +void handlePress(); +void handleRelease(); +void handleRotate(int8_t rotation); + void setup() { Serial.begin(115200); @@ -16,7 +27,17 @@ void setup() pinMode(ANALOG_INPUT_PIN, INPUT); pinMode(ANALOG_OUTPUT_PIN, OUTPUT); + pinMode(ENTER_BUTTON_PIN, INPUT); + pinMode(ENCODER_A_PIN, INPUT); + pinMode(ENCODER_B_PIN, INPUT); + + encoder = new Versatile_RotaryEncoder(ENCODER_A_PIN, ENCODER_B_PIN, ENTER_BUTTON_PIN); + encoder->setHandlePress(handlePress); + encoder->setHandlePressRelease(handleRelease); + encoder->setHandleRotate(handleRotate); + xTaskCreate(handleToneGeneration, "handleToneGeneration", 2048, NULL, 1, NULL); + xTaskCreate(handleInputControls, "handleInputContols", 2048, NULL, 1, NULL); Serial.println("setup complete"); } @@ -51,4 +72,37 @@ void handleToneGeneration(void *pvParameters) } delay(10); } +} + +void handleInputControls(void *pvParameters) +{ + while (true) + { + encoder->ReadEncoder(); + delay(1); + } +} + +void handlePress() +{ + Serial.println("Encoder pressed"); + digitalWrite(ONBOARD_LED_PIN, HIGH); +} + +void handleRelease() +{ + Serial.println("Encoder released"); + digitalWrite(ONBOARD_LED_PIN, LOW); +} + +void handleRotate(int8_t rotation) { + Serial.print("Encoder rotated by "); + Serial.println(rotation); + if (rotation > 0) { + frequency += 10; + } else if (rotation < 0) { + frequency -= 10; + } + Serial.print("Frequency: "); + Serial.println(frequency); } \ No newline at end of file