feat: implement rotary encoder
This commit is contained in:
parent
72b33e7705
commit
c2eb1ba3ba
BIN
diagrams/ec11_encoder.png
Normal file
BIN
diagrams/ec11_encoder.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 389 KiB |
54
src/main.cpp
54
src/main.cpp
@ -1,14 +1,25 @@
|
||||
#include <Arduino.h>
|
||||
#include <Versatile_RotaryEncoder.h>
|
||||
|
||||
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);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user