Hello,
I'm developing a hardware controller for a big Max/Msp patch. The device is a sequencer and drum machine.
I recently bought these (https://www.amazon.de/gp/product/B08728PS6N/ref=ppx_yo_dt_b_asin_title_o03_s00?ie=UTF8&psc=1) potentiometers, as I need them for some controls over the patch, but I can get my head around reading the clicks.
My purpose is to read clockwise/counterclockwise clicks as +1/-1 signals to change the parameters on the patch.
I'm very new to Arduino programming and for my small projects I have always used the standard "Analog Read Serial" with some simple tweaks here and there. Can somebody help me to solve this issue?
Thank you very much!
The clicks are very easy to read, there is a special pin for that, marked "Switch" in the picture.
BTW, that's not a potentiometer, that's a rotary encoder. To get the rotation just search for Arduino and rotary encoder and you get tons of tutorials about how to read them.
Thanks, I found a very straightforward tutorial (find it here https://lastminuteengineers.com/rotary-encoder-arduino-tutorial/) that helped me to read the clicks.
This code reads the count and it tells me when the button is pressed, which is amazing. However, I'd like to use the rotation when the button is not clicked to control one parameter and the one when the button is clicked to control another one. As far as I understood it is possible. How?
Here is the code
// Rotary Encoder Inputs
#define CLK 2
#define DT 3
#define SW 4
int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;
void setup() {
// Set encoder pins as inputs
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);
// Setup Serial Monitor
Serial.begin(9600);
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
}
void loop() {
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
counter --;
currentDir ="CCW";
} else {
// Encoder is rotating CW so increment
counter ++;
currentDir ="CW";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
// Read the button state
int btnState = digitalRead(SW);
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
}
// Remember last button press event
lastButtonPress = millis();
}
// Put in a slight delay to help debounce the reading
delay(1);
}
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.