Results 1 to 1 of 1
  1. #1
    New Member
    Join Date
    Nov 2014
    Location
    United States
    Posts
    1
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Quarter Rotary Encoder with Acceleration (Working) Complete write up

    This is my first post and want to share my success. I wanted to get Rotary Encoders working with acceleration. My problem was I have quarter cycle encoders from leobodnar.com, which are VERY nice encoders by the way. I started with Jim's "quarter_cycle_encoder_demo" which worked right out of the box (Thanks Jim!) But it doesn't have acceleration. I tried his acceleration example but didn't get it working with my 1/4 cycle encoders. I searched around a bit to see if someone already got it working but came up empty. So forgive me if this post is redundant.

    I took Jim's two sketches and morphed them together. I took the "acceleration" logic from Jim's acceleration example and added it to the "quarter_cycle_encoder_demo.ino". Those two sketches come in Jim's download. It took me a while but I got it working. (Again, thanks Jim!) I wanted to share with everyone in case it wasn't already posted.

    My setup:
    Leobodnar rotary encoders, CTS288VSS (Quarter cycle)
    Arduino Mega
    Links2fs Multi for FSX
    FSX

    I used two files from Jim's download:

    The main one is "quarter_cycle_encoder_demo". I used it as my base and modified it. It has two different examples for encoders. I ended up going with the logic in example one and duplicated it for my second encoder. A third encoder should be as easy as copying the logic again.

    The other file was "Multi_acceleration_demo". I ripped out the acceleration logic and added it to the first file, with some tweaks of course.

    In case you are new, the encoders have 3 terminals. The middle goes to ground. The two outer terminals we'll call "A" and "B". They wire thru resistors to 5 Volts. They also jump off and go to Arduino pins 2 & 3.

    Encoder wiring:



    The second encoder connects to Arduino pins 4 & 5.

    Below is my working Arduino sketch. It has two encoders. One to adjust VOR1 and the other adjusts the Heading Bug. If you want to see how it's modified, compare it to Jim's 1/4 cycle example with a diff utility.


    Code:
    /*
    This sketch has controls 2 Rotary Encoders from Leobodnar.com. They are quarter cycle. It also accelerates the speed 
    when they are rotated quickly. 
    This is a combination of two files from Jimspage.co/nz/intro.htm using his Link2fs_Multi_for_for_FSX software. 
    The example sketches are "quarter_cycle_encoder_demo" and "Multi_acceleration_demo". 
    Jim and the other's he mentions are the main contributors of this code. I just modified it to suite my needs. 
    -vinman1000
    */
    
    
    //Not sure if math.h is needed but it was in the example acceleration config I used. I may try removing it and see what happens.
    #include "math.h"
    
    
    int CodeIn;// used on all serial reads
    int KpinNo; 
    int Koutpin;
    byte RE1;
    byte RE2;
    String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
    
    // additional variables from "acceleration example".
    long timePrevious;
    long timeNow;
    long timeInterval = 50;//you adjust this for "your" feel of fast
    int fastTurn = 0;//variable for turn-speed state
    
    
    void setup() 
    {
      Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
      
      for (int KoutPin = 2; KoutPin <= 19; KoutPin++)// Get all the pins ready as imputs 
      {
        pinMode(KoutPin, INPUT);
        digitalWrite(KoutPin, HIGH);  
      }
     Serial.begin(115200);   
    }//end of setup
    
    void loop() {
     timeNow = millis();//get the milli count. (for speed check)
     {INPUTPINS();} //Check the Simconnect and "keys" section
      if (Serial.available()) {
        CodeIn = getChar();
        //if (CodeIn == '=') {EQUALS();} // The first identifier is "="
       // if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<"
        //if (CodeIn == '?') {QUESTION();}// The first identifier is "?"
        //if (CodeIn == '/') {SLASH();}// The first identifier is "/" (Annunciators)
      }
    }//end of void loop
    
    char getChar()// Get a character from the serial buffer
    {
      while(Serial.available() == 0);// wait for data
      return((char)Serial.read());// Thanks Doug
    }//end of getChar
    
    void INPUTPINS() // Simconnect codes and "Keys" section (combined)
    {
      if(timeNow - timePrevious < timeInterval) {fastTurn = 1;}else{ fastTurn = 0;} //is it being turned fast or not ? 
    
      Kstringnewstate = "";
      for (int KpinNo = 2; KpinNo <= 19; KpinNo++){ //set to the input pins. (pins 2 to 70 ,, change for Uno cards)
        KpinStateSTR = String(digitalRead(KpinNo)); 
        KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 2));// set to the first pin read (The 2)
        if (KpinStateSTR != KoldpinStateSTR)
     
        
        {
    // check rotary encoder No.1 --------------------------------------- on pins 2 and 3
            if (KpinNo == 2){  // the first pin of rotary encoder no.1
             int otherPin = digitalRead(3);
             if (KpinStateSTR == "0"){
             if  (otherPin == HIGH){RE1 = 1;}else {RE1 = 2;}
             }else{ 
             if  (otherPin == LOW){RE1 = 1;}else {RE1 = 2;}
             }
             if (RE1 ==1)
              if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
              for (int accel = 1; accel <=5; accel++){//loop to send "A56" 5 times ,, use more or less to suit.
              Serial.println("A56");
              }}else{
              Serial.println("A56"); 
              }
             
             if (RE1 ==2)
              if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
              for (int accel = 1; accel <=5; accel++){//loop to send "A55" 5 times ,, use more or less to suit.         
              Serial.println("A55");
              }}else{         
              Serial.println("A55");
             } //end of RE1's first pin cycle
            } 
              
            if (KpinNo == 3){// the other pin of rotary encoder no.1
             int otherPin = digitalRead(2);
             if (KpinStateSTR == "0"){
             if  (otherPin == LOW){RE1 = 1;}else {RE1 = 2;}
             }else{ 
             if  (otherPin == HIGH){RE1 = 1;}else {RE1 = 2;}
             }
             if (RE1 ==1)
              if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
              for (int accel = 1; accel <=5; accel++){//loop to send "A56" 5 times ,, use more or less to suit.
              Serial.println("A56");
              }}else{
              Serial.println("A56");
              }
             
             if (RE1 ==2)
              if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
              for (int accel = 1; accel <=5; accel++){//loop to send "A55" 5 times ,, use more or less to suit.                  
              Serial.println("A55");
              }}else{         
              Serial.println("A55");
              }//end of RE1's 'other' pin cycle 
             }
           
           
    // check rotary encoder No.2 --------------------------------------- on pins 4 and 5
            if (KpinNo == 4){ // the first pin of rotary encoder no.2
             int otherPin = digitalRead(5);
             if (KpinStateSTR == "0"){
             if  (otherPin == HIGH){RE2 = 1;}else {RE2 = 2;}
             }else{ 
             if  (otherPin == LOW){RE2 = 1;}else {RE2 = 2;}
             }
             if (RE2 ==1)
             if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
             for (int accel = 1; accel <=5; accel++){//loop to send "A58" 5 times ,, use more or less to suit.
             Serial.println("A58");
             }}else{
             Serial.println("A58"); 
             }
             
            if (RE2 ==2)
             if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
             for (int accel = 1; accel <=5; accel++){//loop to send "A57" 5 times ,, use more or less to suit.         
             Serial.println("A57");
             }}else{         
             Serial.println("A57");
             } //end of RE2's first pin cycle
             } 
    
               
            if (KpinNo == 5){// the 'other' pin of rotary encoder no.2
             int otherPin = digitalRead(4);
             if (KpinStateSTR == "0"){
             if  (otherPin == LOW){RE2 = 1;}else {RE2 = 2;}
             }else{ 
             if  (otherPin == HIGH){RE2 = 1;}else {RE2 = 2;}
             }
            if (RE2 ==1)
             if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
             for (int accel = 1; accel <=5; accel++){//loop to send "A58" 5 times ,, use more or less to suit.
             Serial.println("A58");
             }}else{
             Serial.println("A58");
             }
            
             if (RE2 ==2)
              if (fastTurn == 1){//yes it's being turned fast ,, else ,, it's being turned slow
              for (int accel = 1; accel <=5; accel++){//loop to send "A57" 5 times ,, use more or less to suit.                  
              Serial.println("A57");
              }}else{         
              Serial.println("A57");
              }//end of RE2's 'other' pin cycle 
              }
       
           
             timePrevious = timeNow;//resets the previous time to timenow
    
             
             // ------------- now the "Keys" bit -------------------
             if (KpinNo > 5){//this number needs to be higher than the encoder pins
          Serial.print ("D"); 
          if (KpinNo < 10) Serial.print ("0");
          Serial.print (KpinNo);
          Serial.println (KpinStateSTR);
          }//end of the "keys" bit
          
        }//end of yes it's different       
        Kstringnewstate += KpinStateSTR;
      }//end of 'for' loop
      Kstringoldstate = Kstringnewstate;
    }//end of inputpins void
    I hope this is useful and helps others with their projects. I look forward to adding more functionality.

    -vinman1000
    Attached Images Attached Images