Results 1 to 10 of 10
  1. #1
    10+ Posting Member
    Join Date
    Dec 2007
    Location
    england
    Posts
    18
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    1/4 cycle encoder problem

    Thanks to Jims great Links2fs program and his code for the Multi_Radiohead_Annun_Mega
    I was able to succesfully build a working radio unit



    I used jims code exactly as is except for one minor change which let me use an I2C LCD display as I had some to hand

    The unit works great and as it should displaying all the things it supposed to but I have one small flaw .
    I have to turn the radio knob 4 clicks to move the radio frequency one step up or down .
    As I understand it this is because I am using a 1/4 cycle encoder giving one pulse per detent not 2 or 4 that the program expects

    I know Jim has made a demo of how to use the a 1/4 cycle encoder and I have spent all afternoon till 7PM trying to get it going but with no luck.
    When I was younger things sunk in my noggin easily but now I am a pensioner it takes rather longer and even then doesnt always click with me
    If I stay with it any longer I think I will go cross eyed
    The encoder section of the main radio code is in several similar sections , one for each area of the radio , Com1 , Com2 , Nav1 etc
    and I am sure if I knew how to make one section work with my 1/4 cycle encoder that I could use the same principle on all the other sections

    What didnt help was the Multi_Radiohead_Annun_Mega code has seperate sections for encoders and inputs but the demo for 1/4 cycle encoders
    combined both the encoder and input sections together and what with that and the code to change it to use my encoders , it just isnt making
    enough sense to me

    I even thought of cheating and keeping the code as is and swapping my encoders for ones that are not 1/4 cycle but try as I might I cant find
    any on sale in the UK . I dont really want to swap to another encoder as I have 15 of the 1/4 cycle ones in my parts bin so dont want to buy more

    Please can someone help me get going and show me how to code one section ( say Com1 ) for 1/4 cycle encoders , and tell me any new vars I need to declare at the top of the program ( I saw in the 1/4 cycle demo there were variables not yet used in the radio code ) and then I am sure I can manage to copy the same idea to all the other sections as needed

    I hate to admit that I cant work it out for myself because it makes me see as I get older I am starting to lose my marbles , but after trying all afternoon
    I have to admit defeat

    So , can you help ?

    I hope so

    Don
    Attached Images Attached Images

  2. #2
    150+ Forum Groupie BuzziBi's Avatar
    Join Date
    Mar 2013
    Location
    Norway
    Posts
    275
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1/4 cycle encoder problem

    Can you post the part of the code for the Rotary Encoder?
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  3. #3
    10+ Posting Member
    Join Date
    Dec 2007
    Location
    england
    Posts
    18
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1/4 cycle encoder problem

    Hi BuzziBi

    Thanks for replying , yes sure here is the code that is for the encoder for Comm1 and Comm2
    all the other encoder sections are pretty much the same except they check if the selector switch is on
    com1 , com2 nav1 etc



    Code:
    void ROTARYS() {//now the bit for the rotary encoder input
      X = (quad1.position());
      if (X != Xold) { // checks to see if it different
        (Xdif = (X-Xold));// finds out the difference
     
        if (active == 14){// Com1 rotary encoder output
          if (Xdif == 1) {
            if (digitalRead(10)==0 ) { 
              Serial.println("A01");
            } 
            else Serial.println("A03");
          }
          if (Xdif == -1) {
            if (digitalRead(10)==0 ) { 
              Serial.println("A02");
            } 
            else Serial.println("A04");
          }
        }
        if (active == 15){// Com2 rotary encoder output
          if (Xdif == 1) {
            if (digitalRead(10)==0 ) { 
              Serial.println("A07");
            } 
            else Serial.println("A09");
          }
          if (Xdif == -1) {
            if (digitalRead(10)==0 ) { 
              Serial.println("A08");
            } 
            else Serial.println("A10");
          }
        }
    As I said in my first post . My radio panel is working great , I can display and change everything as it should be except that I have to turn the encoder 4 clicks for every inc/dec I want to make . By swapping this code for the code in Jims 1/4 cycle encoder demo it would mean each click would increment or decrement the display rather thean every 4th click . But I just cant see how to code it

    I cant even get the code for the 1/4 cycle encoder demo to run correctly
    when you run it you should get an output in the monitor screen that could either be "UP" or "UP." ( with a full stop) or "down" or "down." again with a full stop
    I assume turning one way I would get
    UP
    UP
    UP
    UP but instead I get

    UP.
    UP.
    UP.
    UP ( this one is without the full stop)

    going counter clockwise is even stranger I expected

    down
    down
    down
    down

    but actually get
    down
    down
    UP. Why UP. ?
    down. why with a full stop ?

    the demo is being run as is , I havent altered the code but thats the results I got , what am I doing wrong
    I am obviously having a senior momment again

    Changing the ups and downs for A01, A02 , A03 and A04 as it tells you in the demo it looks even stranger

    The frequencies in FSX planes go ( turning CW )
    129.2
    130.2 MHz increases
    131.17 mhz increases but Khz decreases
    131.17 nothing changes
    131.2 Khz increases
    132.2
    133.17
    133.17

    and turning CCW

    133.17
    133.17 nothing changes
    134.15 Mhz went up and Khz went down
    133.15 Mhz went down
    133.12 Khz went down
    133.12 nothing changes
    134.10
    133.10

    I cant understand why its doing this
    Please can someone sort me out before I go completely mad
    They said Life begins at 40 but they didnt tell me it goes down hill really fast after you reach 65

    Cheers
    Don

    The full original code for the radio is available on Jims page http://www.jimspage.co.nz/Multi_Radio.htm
    and the 1/4 cycle demo is in the new demo ino's folder that is included in the links multi for FSX and links multi for FSX for Experts downloads again on Jims page http://www.jimspage.co.nz/intro.htm




  4. #4
    150+ Forum Groupie BuzziBi's Avatar
    Join Date
    Mar 2013
    Location
    Norway
    Posts
    275
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1/4 cycle encoder problem

    Did you read this from Jim's demo:

    If you are using quarter cycle encoders there will be 1 "up" (or down) per click.
    If you get 2 "up" (or down) per click then they are half cycle encoders.
    If there are 4 "up" (or down) per click then they are full cycle encoders.
    For half cycle and full cycle you need to use the library and the samples attached.
    If ,, while turning it the one way, you get a few 'wrong' readings ,, then replace the encoder with
    a newer, better one or include a "debounce" system in the INO.

    Personal I have always used the:
    Code:
    void ENCODER(){R =(quad1.position()/2); //The /2 is to suit the encoder(See my website)
      if (R != Rold) { // checks to see if it different
        (Rdif = (R-Rold));// finds out the difference
       if (Rdif == 1) Serial.println ("A01");//Com1 Mhz down
       if (Rdif == -1) Serial.println ("A02"); //Com1 Mhz up
       Rold = R; // overwrites the old reading with the new one.
      }
    and I have never had probles with the encoders.
    I found out that changing the (quad1.position()/2) would give a difference in how many cliks on the encoder to get 1 value.
    I mean changing the /2 to /4 or /8 and so on.
    I guess you also can use *2 , multiplication.
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  5. #5
    10+ Posting Member
    Join Date
    Dec 2007
    Location
    england
    Posts
    18
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1/4 cycle encoder problem

    Quote Originally Posted by BuzziBi View Post
    Did you read this from Jim's demo:

    If you are using quarter cycle encoders there will be 1 "up" (or down) per click.
    If you get 2 "up" (or down) per click then they are half cycle encoders.
    If there are 4 "up" (or down) per click then they are full cycle encoders.
    For half cycle and full cycle you need to use the library and the samples attached.
    If ,, while turning it the one way, you get a few 'wrong' readings ,, then replace the encoder with
    a newer, better one or include a "debounce" system in the INO.

    Personal I have always used the:
    Code:
    void ENCODER(){R =(quad1.position()/2); //The /2 is to suit the encoder(See my website)
      if (R != Rold) { // checks to see if it different
        (Rdif = (R-Rold));// finds out the difference
       if (Rdif == 1) Serial.println ("A01");//Com1 Mhz down
       if (Rdif == -1) Serial.println ("A02"); //Com1 Mhz up
       Rold = R; // overwrites the old reading with the new one.
      }
    and I have never had probles with the encoders.
    I found out that changing the (quad1.position()/2) would give a difference in how many cliks on the encoder to get 1 value.
    I mean changing the /2 to /4 or /8 and so on.
    I guess you also can use *2 , multiplication.
    Hi BuzziBi
    and thanks foir replying again but I think maybe you may have miss read my first post because I thought I had explained after reading what Jim wrote in his demo that I understood the differences between 1 , 2 and 4 step per click encoders , Thats how knew my problem was because of using a quarter cycle one . and the code you say you use without a problem is actually the code I uploaded for you and had already said I was using and which will not work for my encoder.
    If you are getting an increment or decrement for every click of your encoder then your encoder must be a 2 or 4 step type so will not experience the problem
    I understand that I can change the 2 in the line (quad1.position()/2) but that only makes it worse for a 1/4 cycle encoder
    If I leave out the 2 so there is no division at all it takes 4 clicks to increment a number once, with the 2 in the line it takes 8 clicks and with 4 in the line its 16 clicks which is a full rotation of the encoder for one didgit increment. You did suggest putting *2 but that wont work either , it just means I still have to turn it 4 clicks and then it increments up by 2 so lets say I am on 132.00 Mhz I would turn it 4 clicks and it would then read 134.oo Mhz . There would be no way to dial in 131.00Mhz
    So either I have to change my encoders for ones that work on 2 or 4 pulses per click like you use ( but I cant find one for sale in England) or
    I have to find someone who can help me change the code over from what you use ,to what Jim shows in his 1/4 cycle encoder demo

    can you tell me the part number of your encoders so I have something to look for ?


    Cheers Don

  6. #6
    150+ Forum Groupie BuzziBi's Avatar
    Join Date
    Mar 2013
    Location
    Norway
    Posts
    275
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1/4 cycle encoder problem

    A bit confused here
    You say(quote)​ in the monitor screen that could either be "UP" or "UP." )
    If you make a debug line that says to serialprint "UP" it will serialprint "UP" or nothing. Depending on whether your code is correct or not. It can not print "UP." if your code says:
    if (digitalRead(10)==0 ) {
    Serialprintln("UP"); }
    If you get "UP." there must be something like noise or ??? that destroys the printline.
    This is why I asked if you have read the part:
    If ,, while turning it the one way, you get a few 'wrong' readings ,, then replace the encoder with
    a newer, better one or include a "debounce" system in the INO.
    I have no
    experience with using debounce with rotary encoders, so I can not help you there.
    I realy can not remember where i bougth ny encoders
    But I think it was here: http://www.miniinthebox.com/index.ph...rotary+encoder
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  7. #7
    10+ Posting Member
    Join Date
    Dec 2007
    Location
    england
    Posts
    18
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1/4 cycle encoder problem

    Quote Originally Posted by BuzziBi View Post
    A bit confused here
    You say(quote)​ in the monitor screen that could either be "UP" or "UP." )
    If you make a debug line that says to serialprint "UP" it will serialprint "UP" or nothing. Depending on whether your code is correct or not. It can not print "UP." if your code says:
    if (digitalRead(10)==0 ) {
    Serialprintln("UP"); }
    If you get "UP." there must be something like noise or ??? that destroys the printline.
    This is why I asked if you have read the part:
    If ,, while turning it the one way, you get a few 'wrong' readings ,, then replace the encoder with
    a newer, better one or include a "debounce" system in the INO.
    I have no
    experience with using debounce with rotary encoders, so I can not help you there.
    I realy can not remember where i bougth ny encoders
    But I think it was here: http://www.miniinthebox.com/index.ph...rotary+encoder

    Hi BuzziBi , Sorry for confusing you
    I think we are thinking about two different things here . There is what I am talking about and what you imagine I am talking about and I fear they are not the same
    I did send you the link to the demo prog so you could see for yourself thinking it may have made it clearer for you as to what I was getting at

    Yes the reason I said in the monitor screen it could be UP or UP. or down or down. is because in the demo program there is this code
    which I thought made that possible

    Code:
      {     // 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)Serial.println("UP");
             if (RE1 ==2)Serial.println("down");
             } //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)Serial.println("UP.");
             if (RE1 ==2)Serial.println("down.");
              }//end of RE1's 'other' pin cycle
    So I took that to mean dependant on what inputs and existing states I have that any one of up , up. , down or down. could get sent with println
    depending on what is relevent at the time
    I agree as you say if the debug line just said "print UP" then it has to print " UP" or nothing but its not saying that , here its saying if this is true then print "UP" else print down etc which I believe now does give it the ability to print several things

    I understand that if the contacts bounce I will get noise and spurious triggering but this is not what is happening in this case
    its the code and the last encoder states that is deciding which of the four choices gets sent ,
    As I said my encoders are working fine in the radio annun prog at the momment with no spurious triggering so have no reason to doubt them in the new code . I just need to change it over to code similar to the 1/4 cycle encoder demo to get it to speed up the frequency change

    but if you try running the demo code yourself I think you will see its not giving what I would expect as an output , unless of course I am doing something silly and at my age thats not unusual . I can have a senior momment at any time these days

    Don

  8. #8
    150+ Forum Groupie BuzziBi's Avatar
    Join Date
    Mar 2013
    Location
    Norway
    Posts
    275
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1/4 cycle encoder problem

    Try using the "Multi_Keys_RotaryEncoders" demo .ino
    include "math.h"
    include "Quadrature.h"
    and test with the code in that sketch.
    I suspect that your encoder is a full cycle encoder.
    If you dont have the Quadrature library, you find it here:
    http://www.neufeld.newton.ks.us/elec...s/?page_id=249
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------

  9. #9
    10+ Posting Member
    Join Date
    Dec 2007
    Location
    england
    Posts
    18
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1/4 cycle encoder problem

    Hi BuzziBi

    Please dont think I am ungrateful for your replies , I really am not , but we seem to be going round in circles
    and getting nowhere

    In your last post you wrote

    " I suspect that your encoder is a full cycle encoder. "

    but in every post we have exchanged I have told you clearly that my encoder is not a full cycle encoder its a 1/4 cycle encoder.
    If it was a full cycle encoder I wouldnt be having these problems . The radio stack is working fine , it shows all the right digits
    and it changes the frequency as it should it just takes four clicks to change a number instead of one click as it should and this is all due
    to me using a 1/4 cycle encode ( needs four times as many clicks) rather than a full cycle one

    You also wrote

    "Try using the "Multi_Keys_RotaryEncoders" demo .ino
    include "math.h
    include "Quadrature.h "

    But on Jim NZ's page ( who wrote Links2FS ) he states

    " Keiths library does half and full cycle encoders ,, there is however a another type on the block that is getting more
    and more prevalent and that is a QUARTER CYCLE encoder. (One state change per click)
    If your encoders are quarter cycle type then you donít need to use a library at all.
    I have enclosed a demo in the download called "quarter_cycle_encoder_demo" that has 2 different types of code showing how to do it.
    DON'T USE A LIBRARY if you have quarter cycle encoders. "

    So you see just by the fact you tell me I should include the quadrature library tells me it cannot work so is pointless me trying

    I have managed to source some 1/2 cycle encoders and have ordered 10 from Mouser they arrive on Friday from the USA
    I am confident that my radio stack will then work exactly as it should .
    Its just a shame that I cant yet use my 1/4 cycle ones because I have 15 of those in stock. Jim says they will work using his 1/4 cycle demo
    but I cant get them to do so, I suspect the demo code is not quite right . In the mean time I am going to try re writing the code myself . I expect I will fail but it costs nothing to try . I asked on here because I thought someone with much more knowledge and experience than me might have known how to get them going, which is why my first post asked " Can any one recode one section of the encoders part so I can see how to do the rest myself"

    I will let you know how I get on
    Either I will find out how to recode it myself , ( unlikely)
    or I will fit the new encoders on Friday and can then use the original code

    Don

  10. #10
    10+ Posting Member
    Join Date
    Dec 2007
    Location
    england
    Posts
    18
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: 1/4 cycle encoder problem

    I had a bit of success

    I know its not pretty
    and its definately not the best way to do it
    and I wish someone would show me a better bit of code to do the same job
    but despite all that

    it actually works which surprises me even more than it suprises you

    Code:
    int AOLD = 0 ; //A Channels old reading
    // int ANEW ;     //A Channels new reading
    int BOLD = 0 ; //B Channels old reading
    // int BNEW ;     //B Channels new reading
    int PinNum;
    
    
    void setup() {
      // put your setup code here, to run once:
    for (int PinNum =2; PinNum <=69; PinNum++) // Make all pins inputs
      {
        pinMode(PinNum,INPUT);
        digitalWrite (PinNum,HIGH);
      }
      
     Serial.begin(115200);
     
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    ENCODER();
    
    }
    
    void ENCODER() 
    {
    
      int ANEW=(digitalRead(8));  //Ist Encoder Pin Chan A
      int BNEW=(digitalRead(9));  // 2nd encoder pin Chan B
      
      //The next lines decode which Direction the encoder is moving
      
      if (AOLD == 0 and ANEW == 1 and BNEW == 0){  
        AOLD = ANEW;
        UP();
      }
         if (AOLD == 0 and ANEW == 1 and BNEW == 1){
        AOLD = ANEW;
        DOWN();
       }
         if (AOLD == 1 and ANEW == 0 and BNEW == 1){
       AOLD = ANEW;
        UP();
      }
         if (AOLD == 1 and ANEW == 0 and BNEW == 0){
        AOLD = ANEW;
        DOWN();
     }
    
     if(BOLD == 0 and BNEW == 1 and ANEW == 1 ) {
      BOLD = BNEW;
      UP();
     
     }
      if(BOLD == 0 and BNEW == 1 and ANEW == 0 ) {
      BOLD = BNEW;
      DOWN();
     
     }
      if(BOLD == 1 and BNEW == 0 and ANEW == 0 ) {
      BOLD = BNEW;
      UP();
     
     }
      if(BOLD == 1 and BNEW == 0 and ANEW == 1 ) {
      BOLD = BNEW;
      DOWN();
     
     }
    }   
    void UP()     //If the frequency is moving up use this function
                {
                if( digitalRead(14) == 0 and digitalRead(10)==0){ 
                  Serial.println ("A02");   // Increment Mhz
                }
               else if(digitalRead(14) == 0 and digitalRead(10)==1){ 
                  Serial.println ("A04");   // Increment Khz
                }
    
    }
    
    void DOWN() // If the frequency is decreasing use this function
               {
                if( digitalRead(14) == 0){   // Selector set to Comm1
                if(digitalRead(10)==0){      // Encoder switch is pressed
                  Serial.println ("A01");    // Decrement Mhz
                }
                  if(digitalRead(10)==1){    //Encoder switch not pressed
                  Serial.println ("A03");    //Decrement Khz
                }
             
    }
     
    }
    The encoder is conected to pins 8 and 9
    and the switch on the encoder to pin 10
    pin 14 selects comm1
    pin 15 comm2
    pin 16 nav1 etc

    It makes 4 variables AOLD , ANEW, BOLD and BNEW to hold the new , and the previous values of the
    A & B channel pins on the encoder

    Void loop just tells it to run my function ENCODER()
    and that function uses the 4 variable ANEW,AOLD,BNEW,BOLD to determine which way the encoder is moving
    when Its decided it updates AOLD and BOLD with the new values then goes to one of two functions UP() or DOWN()
    and those two funtions println what ever is needed to make COMM1's frequency go up and down in Mhz or Khz

    As I said , Its not pretty and probably very inefficient , but it uses no libraries and it works for my 1/4 cycle encoder
    which is what I wanted . It wont work if you have a 1/2 cycle or full cycle encoder
    If you are a bit clever at programming , ( which I am not) and you can write a better version to do this I would love a copy of it

    Don