Results 1 to 2 of 2
  1. #1
    Our new friend needs to reach 10 posts to get to the next flight level
    Join Date
    Mar 2014
    Location
    USA
    Posts
    5
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Help! Rotary encoders

    So when I twist my rotary encoder fast one direction I get data like this:
    A02
    A01
    A01
    A01
    A02
    A01
    A01
    A02
    A01
    A02
    A01
    A02
    But when I twist the knob slowly it seems to function properly.
    Code:
    #include 
    Encoder knobLeft(52,53);
    Encoder knob2(50, 51);
    long oldright1=0;
    long oldleft1=0;
    long newLeft1, newRight1;
    int counterLFW1, counterLBW1;
    long oldright2=0;
    long oldleft2=0;
    long newLeft2, newRight2;
    int counterLFW2, counterLBW2;
    #define button 11
    #define LEDA 22
    #define LEDB 23
    #define LEDC 24
    #define LEDD 25
    #define LEDE 26
    #define LEDF 27
    #define LEDG 28
    #define LED1 29
    #define LED2 30
    #define LED3 31
    #define LED4 32
    #define LED5 33
    #define DP   34
    #define off HIGH
    #define on LOW
    #include 
    elapsedMillis timer0;
    #define interval 1000
    int com1sbint =0;
    int CodeIn;
    String comsb;
    String AvVoltsS;
    int AvVolts;
    int buttonstate=0;
    int lastbuttonstate=0;
    void setup() {
      Serial.begin(115200);
      Serial.println("TwoKnobs Encoder Test:");
      pinMode(50, OUTPUT);
      pinMode(51, OUTPUT);
      pinMode(52, OUTPUT);
      pinMode(53, OUTPUT);  
      pinMode(LEDA, OUTPUT);
      pinMode(LEDB, OUTPUT);
      pinMode(LEDC, OUTPUT);
      pinMode(LEDD, OUTPUT);
      pinMode(LEDE, OUTPUT);
      pinMode(LEDF, OUTPUT);
      pinMode(LEDG, OUTPUT);
      pinMode(LED1, OUTPUT);
      pinMode(LED2, OUTPUT);
      pinMode(LED3, OUTPUT);
      pinMode(LED4, OUTPUT);
      pinMode(LED5, OUTPUT);
      pinMode(DP, OUTPUT);
      pinMode(button, INPUT);
      digitalWrite(LEDA, HIGH);
      digitalWrite(LEDB, HIGH);
      digitalWrite(LEDC, HIGH);
      digitalWrite(LEDD, HIGH);
      digitalWrite(LEDE, HIGH);
      digitalWrite(LEDF, HIGH);
      digitalWrite(LEDG, HIGH);
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      digitalWrite(LED5, HIGH);
      digitalWrite(DP,   HIGH);
    }
    
    long positionLeft  = -999;
    long positionRight = -999;
    void loop() {
      lastbuttonstate = buttonstate;
      buttonstate = digitalRead(button);
    if(buttonstate == LOW && lastbuttonstate == HIGH)
    {
      Serial.println("A06");
    }
      oldleft1 = newLeft1;
      oldright1 = newRight1;
      
      newLeft1 = knobLeft.read();
      if(newLeft1 > oldleft1)
      {
        counterLFW1++;
      }
      if(counterLFW1>3){
      Serial.println("A03");
      
      counterLFW1 =0;
      }
      if(newLeft1 < oldleft1)
        counterLBW1++;
      if(counterLBW1 > 3){
        Serial.println("A04");
        oldleft1 = newLeft1;
        counterLBW1 =0;
      }
      //other knob
      oldleft2 = newLeft2;
      oldright2 = newRight2;
      
      newLeft2 = knob2.read();
      if(newLeft2 > oldleft2)
      {
        counterLFW2++;
      }
      if(counterLFW2>3){
      Serial.println("A01");
      counterLFW2 =0;
      }
      if(newLeft2 < oldleft2)
        counterLBW2++;
      if(counterLBW2 > 3){
        Serial.println("A02");
        oldleft2 = newLeft2;
        counterLBW2 =0;
      }
      
      
      if(Serial.available())
        {
          CodeIn = getChar();
          if(CodeIn == '?')
          {
            delay(11);
            CodeIn = getChar();
            if(CodeIn == 'U')
            {
              AvVoltsS = "";
              AvVoltsS += getChar();
              AvVoltsS += getChar();
            }
          }
          if(CodeIn == '=')
          {
            delay(11);
            CodeIn = getChar();
            if(CodeIn == 'B')
              {
                comsb = "";
                comsb += getChar();
                comsb += getChar();
                comsb += getChar();
                getChar();
                comsb += getChar();
                comsb += getChar();
              }
          }
        }
        AvVolts = AvVoltsS.toInt();
        if(AvVolts > 0){
          com1sbint = comsb.toInt();
          Number99999(com1sbint);
        }
        else
      {
        Displayoff();
      }
    }
    void Displayoff()
    {
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      digitalWrite(LED5, HIGH);
    }
    void Number99999(int n)
    {
      int t = 3;
      int digit1 = n%10;
      int digit2 = ((n - digit1)/10)%10;
      int digit3 = (((n-digit1)/10-digit2)/10%10);
      int digit4 = ((((n-digit1)/10-digit2)/10-digit3)/10%10);
      int digit5 = (((((n-digit1)/10-digit2)/10-digit3)/10-digit4)/10%10);
      delay(t);
      TurnOnLED(digit1);
      digitalWrite(LED1, off);
      digitalWrite(LED2, off);
      digitalWrite(LED3, off);
      digitalWrite(LED4, off);
      digitalWrite(LED5, on);
      delay(t);
      TurnOnLED(digit2);
      digitalWrite(LED1, off);
      digitalWrite(LED2, off);
      digitalWrite(LED3, off);
      digitalWrite(LED4, on);
      digitalWrite(LED5, off);
      delay(t);
      TurnOnLED(digit3);
      digitalWrite(LED1, off);
      digitalWrite(LED2, off);
      digitalWrite(LED3, on);
      digitalWrite(LED4, off);
      digitalWrite(LED5, off);
      delay(t);
      TurnOnLED(digit4);
      digitalWrite(LED1, off);
      digitalWrite(LED2, on);
      digitalWrite(LED3, off); 
      digitalWrite(LED4, off);
      digitalWrite(LED5, off);
      delay(t);
      TurnOnLED(digit5);
      digitalWrite(LED1, on);
      digitalWrite(LED2, off);
      digitalWrite(LED3, off);
      digitalWrite(LED4, off);
      digitalWrite(LED5, off);
      
      
    }
    void TurnOnLED(int N)
    {
      switch (N){
        case 0:
        {
        digitalWrite(LEDA, HIGH);
        digitalWrite(LEDB, HIGH);
        digitalWrite(LEDC, HIGH);
        digitalWrite(LEDD, LOW);
        digitalWrite(LEDE, HIGH);
        digitalWrite(LEDF, HIGH);
        digitalWrite(LEDG, HIGH);
        break;
        }
          case 1:
        {
        digitalWrite(LEDA, LOW);
        digitalWrite(LEDB, LOW);
        digitalWrite(LEDC, HIGH);
        digitalWrite(LEDD, LOW);
        digitalWrite(LEDE, LOW);
        digitalWrite(LEDF, HIGH);
        digitalWrite(LEDG, LOW);
        break;
        }
          case 2:
        {
        digitalWrite(LEDA, HIGH);
        digitalWrite(LEDB, LOW);
        digitalWrite(LEDC, HIGH);
        digitalWrite(LEDD, HIGH);
        digitalWrite(LEDE, HIGH);
        digitalWrite(LEDF, LOW);
        digitalWrite(LEDG, HIGH);
        break;
        }
          case 3:
        {
        digitalWrite(LEDA, HIGH);
        digitalWrite(LEDB, LOW);
        digitalWrite(LEDC, HIGH);
        digitalWrite(LEDD, HIGH);
        digitalWrite(LEDE, LOW);
        digitalWrite(LEDF, HIGH);
        digitalWrite(LEDG, HIGH);
        break;
        }
          case 4:
        {
        digitalWrite(LEDA, LOW);
        digitalWrite(LEDB, HIGH);
        digitalWrite(LEDC, HIGH);
        digitalWrite(LEDD, HIGH);
        digitalWrite(LEDE, LOW);
        digitalWrite(LEDF, HIGH);
        digitalWrite(LEDG, LOW);
        break;
        }
          case 5:
        {
        digitalWrite(LEDA, HIGH);
        digitalWrite(LEDB, HIGH);
        digitalWrite(LEDC, LOW);
        digitalWrite(LEDD, HIGH);
        digitalWrite(LEDE, LOW);
        digitalWrite(LEDF, HIGH);
        digitalWrite(LEDG, HIGH);
        break;
        }
          case 6:
        {
        digitalWrite(LEDA, HIGH);
        digitalWrite(LEDB, HIGH);
        digitalWrite(LEDC, LOW);
        digitalWrite(LEDD, HIGH);
        digitalWrite(LEDE, HIGH);
        digitalWrite(LEDF, HIGH);
        digitalWrite(LEDG, HIGH);
        break;
        }
          case 7:
        {
        digitalWrite(LEDA, HIGH);
        digitalWrite(LEDB, LOW);
        digitalWrite(LEDC, HIGH);
        digitalWrite(LEDD, LOW);
        digitalWrite(LEDE, LOW);
        digitalWrite(LEDF, HIGH);
        digitalWrite(LEDG, LOW);
        break;
        }
          case 8:
        {
        digitalWrite(LEDA, HIGH);
        digitalWrite(LEDB, HIGH);
        digitalWrite(LEDC, HIGH);
        digitalWrite(LEDD, HIGH);
        digitalWrite(LEDE, HIGH);
        digitalWrite(LEDF, HIGH);
        digitalWrite(LEDG, HIGH);
        break;
        }
            case 9:
        {
        digitalWrite(LEDA, HIGH);
        digitalWrite(LEDB, HIGH);
        digitalWrite(LEDC, HIGH);
        digitalWrite(LEDD, HIGH);
        digitalWrite(LEDE, LOW);
        digitalWrite(LEDF, HIGH);
        digitalWrite(LEDG, LOW);
        break;
        }
      }
    }
    char getChar()
    {
      while(Serial.available() == 0);
      return(((char)Serial.read()));
    }
    Any ideas for this strange behavior?

  2. #2
    75+ Posting Member
    Join Date
    Apr 2014
    Location
    Tampa, FL, USA, Earth..umm
    Posts
    77
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Help! Rotary encoders

    That's a lot of code. I use QuadEncoder library. At the top after the #include I set it up:

    int qe1Move=0;
    int qe2Move=0;
    int qe3Move=0;
    int qe4Move=0;
    int qe5Move=0;

    QuadEncoder qe1(2,3); // COM 1
    QuadEncoder qe2(4,5); // NAV 1
    QuadEncoder qe3(6,7); // NAV 2
    QuadEncoder qe4(8,9); // Transponder digits 1 & 2
    QuadEncoder qe5(10,11); // Transponder digits 2 & 3

    In my INPUTS() section, read during the loop, I set my encoders up to set the MHz (or KHz if the button on the encoder is pressed):

    // rotary 1 = COM 1

    qe1Move=qe1.tick();
    buttonState == "";
    buttonState = String(digitalRead(52));
    if (qe1Move=='<') { if (buttonState == "1") { Serial.println("A02");} else { Serial.println("A04"); } }
    if (qe1Move=='>') { if (buttonState == "1") { Serial.println("A01");} else { Serial.println("A03"); } }

    Are you using quarter, half or full encoders? It sounds more like the issue, but I'm no encoder expert. I did have that same problem, though, but the encoder library worked for me. Now I can spin them fast or slow and doesn't matter, but you have to know if they are quarter, half or full type.

    Link to some good info on Jim's page: http://www.jimspage.co.nz/encoders2.htm

    Mike