Suggestions for MIDI Encoder?

Hello! I am fairly new to programming and attempting to build a custom USB MIDI controller. The idea is to use an encoder as a general purpose CC input that can be set to any effect parameter (frequency, pitch, resonance, etc.). The basic code is not an issue, as I have the program up and running. The high level concept is to make the encoder function similar to a turntable/record player. When mapped to the right parameter within a DAW, one can achieve cool "DJ like" scratching effects. My question is, can an encoder be programmed to reset to neutral/zero once user input has ceased and say one second has passed? This way the user wouldn't have to manually find the original pitch/frequency of their sound, the encoder would reset the edited parameter automatically. Any suggestions or direction would be greatly appreciated.

Here is an example to try:

#define ClockPin 2 // Must be pin 2 
#define DataPin 3 // Must be pin 3
#define readA bitRead(PIND,2)//faster than digitalRead()  (((value) >> (bit)) & 0x01)
#define readB bitRead(PIND,3)//faster than digitalRead()  (((value) >> (bit)) & 0x01)
volatile long count = 0;
long lastCtr;
unsigned long SpareCycles;

void Encoder() {
  (readB)  ? count++ : count--;
}

void setup() {
  Serial.begin(115200); //115200
  pinMode(ClockPin, INPUT);
  pinMode(DataPin, INPUT);

  attachInterrupt(digitalPinToInterrupt(ClockPin), Encoder, RISING);

}

void loop() {
  long Counter;
  SpareCycles++;
  static unsigned long DelayTimer;
  if (lastCtr == Counter) {
    if ((millis() - DelayTimer) >= (1000)) { // Reset after 1 second
      noInterrupts ();
      count = 0;
      interrupts ();
      lastCtr = 0;
      Counter = 0;
    }
  } else {
    DelayTimer = millis();
  }

  noInterrupts ();
  Counter = count;
  interrupts ();
  if ((lastCtr != Counter) & (SpareCycles > 100)) {
    Serial.println(Counter);
    SpareCycles = 0;
  }
  lastCtr = Counter;
}

Z

Hey Z! Thanks for the response! I'll give this a try when I get home. Before I do, I should mention that I am using the Control Surface library. As far as I can tell, this library handles a lot of the higher level code "under the hood" so to speak. Noobs like me just have to instantiate the encoder and select the pins, and the library handles the rest. Do you think the code you provided might have any conflicts? Control Surface seems pretty well documented, but most of it is gibberish to me at my level, so most concepts are over my head. I appreciate it friend!

The code I shared shouldn't cause any conflicts. It does require pins 2&3 to be used but it can be simplified to only require one of the two pins and one other pin. There is no blocking code so as long as this isn't an issue it should work. With that said I don't have any knowledge of the midi library you mentioned.
I hope this helps.
Z

Awesome! It can't hurt to try, right? I am using pins 2 & 3, so hopefully this integrates flawlessly. I really appreciate the help, and I'll let you know how it goes. Thanks a bunch!

Tried the sketch on it's own, to try and educate myself and understand it better. I'm getting values that continuously increase with every turn of the encoder (clockwise or counter clockwise), but it is not resetting to zero after a pause. Also tried to integrate it with my code just to see if I could get it to verify, but could not. Safe to assume that's my fault :sweat_smile: Is there a specific library I should have installed? Please feel free to ignore me at this point :rofl: :stuck_out_tongue_closed_eyes:


How it works: When output A
attachInterrupt(digitalPinToInterrupt(ClockPin), Encoder, RISING);
pulses High (transitions from low to high) output B will be either low or high depending on which way it is rotating.
(readB) ? count++ : count--;
ReadB reads the value of pin 3 directly bitRead(PIND,3)
if pin 3 is true then we count++
if pin 3 is false then we count--

send us a picture of your setup and or a wiring diagram with part numbers.

Z

Hey Z. Sorry I took so long to respond. I REALLY appreciate your effort. I understand how the encoder works in the mechanical sense. Still, thank you. From your last response however, I feel I have SO MUCH to learn before I can even comprehend the code you have offered. I decided to shelve this project temporarily, and really dig in, go back to the basics, and get more fluent with the code first. I kind of got ahead of myself on this one :grin: Hopefully soon I'll be able to revisit this thread with full understanding of what you tried to share with me. Until then, I wish you the best, and thanks again. Take care!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.