Page 1 of 8 12345 ... LastLast
Results 1 to 10 of 78
  1. #1
    150+ Forum Groupie WJH308's Avatar
    Join Date
    Aug 2008
    Location
    San Francisco, CA
    Posts
    159
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    My PDE - Autopilot controls using Rotary Encoders and TM1640 displays

    First of all, a huge thanks to Jim!
    Second of all, this code compiles just fine in Arduino 1.01
    This code is heavily modified version of one of Jims examples.

    Code:
    
    
    
    #define ENCODER_DO_NOT_USE_INTERRUPTS
    #include < Encoder.h > //http://www.pjrc.com/teensy/td_libs_Encoder.html
    //#include < SoftwareSerial.h >
    #include < TM1638.h > // required because the way arduino deals with libraries
    #include < TM1640.h > //http://code.google.com/p/tm1638-library/
    
    
    
    
    // define a module on data pin 3, clock pin 2
    TM1640 module1(3, 2);
    TM1640 module2(5, 4);
    
    
    
    
    //define rotary encoders
    Encoder AltEnc(44, 45);
    Encoder HdgEnc(46, 47);
    Encoder Crs1Enc(48, 49);
    Encoder SpdEnc(50, 51);
    Encoder VsEnc(52, 53);
    
    
    
    
    
    
    
    
    int j1;
    int j2;
    int pinState;
    int pinNo; 
    int doutpin;
    
    
    
    
    
    
    
    
    long AltPosition = -999, HdgPosition = -999, CrsPosition = -999, VsPosition = -999, SpdPosition = -999;
    
    
    
    
    String oldpinStateSTR, pinStateSTR, stringnewstate, stringoldstate, com1, com1old, aphdgset, aphdgsetold, apcrs, apcrsold, output, outputold, apalt, apaltold;
    
    
    
    
    
    
    
    
    void setup() 
    {
    
    
    
    
    
    
    
    
    
    
    
    
      Serial.begin(115200);  
      for (int doutPin = 8; doutPin <= 43; doutPin++)  
      {  
        pinMode(doutPin, INPUT);
        digitalWrite(doutPin, HIGH);  
      }  
      stringoldstate = "1111111111111111111111111111111111111111111";
      Serial.flush(); 
    }
    
    
    
    
    
    
    
    
    
    
    
    
    void loop() {
      // read the oldest byte in the serial buffer:
      if (Serial.available() > 0) {
        j1 = Serial.read();
        if (j1 == 'r'){     // Found the reading "autopilot heading set"
          delay (11); // It seems to need a delay here
          aphdgset = "";
          aphdgset +=(char)Serial.read();
          aphdgset += (char)Serial.read();
          aphdgset += (char)Serial.read();
    
    
    
    
          if (aphdgset != aphdgsetold){  // checks to see if its different to the "old" reading
    
    
    
    
            aphdgsetold = aphdgset; // Writes the current reading to the "old" string.
          }        
        }
    
    
    
    
        if (j1 == 'a'){     // Found the reading "crs"
          delay (11);
          apcrs ="";
          apcrs += (char)Serial.read();
          apcrs += (char)Serial.read();
          apcrs += (char)Serial.read();
    
    
    
    
    
    
    
    
          if (apcrs != apcrsold){
    
    
    
    
            apcrsold = apcrs;
          }        
        } 
    
    
    
    
        if (j1 == 'p'){ //  Found the reading "AP ALT"
          delay (11);
          apalt = "";
          apalt += (char)Serial.read();
          apalt += (char)Serial.read();
          apalt += (char)Serial.read();
          apalt += (char)Serial.read();
          apalt += (char)Serial.read();
    
    
    
    
          if (apalt != apaltold){
    
    
    
    
            apaltold = apalt;
          }
        }
    
    
    
    
        output = apcrsold;
        output += " ";
        output += aphdgsetold;
        output += " ";
        output += apaltold;
    
    
    
    
        if (output != outputold){
          module1.setDisplayToString(output);
          outputold = output;
    
    
    
    
    
    
    
    
        }
      }
    
    
    
    
      // Now the bit for the "keys" input
      //oldpinStateSTR = "";
      //pinStateSTR = "";
      stringnewstate = "";
      for (int pinNo = 8; pinNo <= 43; pinNo++){ // <= 69 changed to 43 to avoid problems with rotary enc
        pinState = (digitalRead(pinNo)); 
        pinStateSTR = "";
        pinStateSTR += pinState;
        oldpinStateSTR = "";
        oldpinStateSTR += stringoldstate.charAt(pinNo-8);
    
    
    
    
    
    
    
    
        if (pinStateSTR != oldpinStateSTR)
        {
          Serial.print ("D"); 
          if (pinNo < 10) Serial.print ("0");
          Serial.print (pinNo);
          Serial.print (pinStateSTR);
          delay(11);
        }
        stringnewstate += pinState;
      }
    
    
    
    
      stringoldstate = stringnewstate;
      //delay(11);
    
    
    
    
      // Altitude Encoder Output
      long newAltPosition = AltEnc.read()/4;
      if (newAltPosition != AltPosition) {
        if(AltPosition > newAltPosition){
          Serial.print("D450");
        }
        if(AltPosition < newAltPosition){
          Serial.print("D440");
        }
        AltPosition = newAltPosition;
      }
      //Heading Encoder Output
      long newHdgPosition = HdgEnc.read()/4;
    
    
    
    
      if (newHdgPosition != HdgPosition) {
        if(HdgPosition > newHdgPosition){
    
    
    
    
          Serial.print("D470");
    
    
    
    
        }
        if(HdgPosition < newHdgPosition){
    
    
    
    
    
    
    
    
          Serial.print("D460");
        }
        HdgPosition = newHdgPosition;
      }
    
    
    
    
      //Course1 Encoder Output
    
    
    
    
      long newCrsPosition = Crs1Enc.read()/4;
    
    
    
    
      if (newCrsPosition != CrsPosition) {
        if(CrsPosition > newCrsPosition){
          Serial.print("D490");
        }
        if(CrsPosition < newCrsPosition){
          Serial.print("D480");
        }   
        CrsPosition = newCrsPosition;
      }
      //Speed Encoder Output
      long newSpdPosition = SpdEnc.read()/4;
      if (newSpdPosition != SpdPosition) {
        if(SpdPosition > newSpdPosition){
          Serial.print("D510");
        }
        if(SpdPosition < newSpdPosition){
          Serial.print("D500");
        }
    
    
    
    
        SpdPosition = newSpdPosition;
      }
      //Vertical Speed Encoder Output
      long newVsPosition = VsEnc.read()/4;
      if (newVsPosition != VsPosition) {
        if(VsPosition > newVsPosition){
          Serial.print("D530");
        }
        if(VsPosition < newVsPosition){
          Serial.print("D520");
        }
    
    
    
    
        VsPosition = newVsPosition;
      }
    
    }
    Now just so you know, I can't get the encoders to work fast I think I am going to program a separate MCU to act as a Quadrature to impulse converter. That would solve the rotary encoder issue for good.
    Second of all, I am using true quadrature encoders, EC11's from Digikey 1.30 each. 20 clicks per rev.

    Still waiting for my 2nd TM1640 display so that is not hooked up yet. I hope this project gives insight to many people here!
    Last edited by WJH308; 06-07-2012 at 04:04 PM. Reason: Fixed bugs in code

  2. Thanks Jim NZ, AK Mongo thanked for this post
  3. #2
    150+ Forum Groupie WJH308's Avatar
    Join Date
    Aug 2008
    Location
    San Francisco, CA
    Posts
    159
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: My PDE - Autopilot controls using Rotary Encoders and TM1640 displays


  4. Thanks Jim NZ thanked for this post
  5. #3
    500+ This must be a daytime job Jim NZ's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    764
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: My PDE - Autopilot controls using Rotary Encoders and TM1640 displays

    Good stuff ,, I particularly like those serial displays ,, it makes putting something together so much easier and only uses a few pins.

    On your rotary encoder troubles ,, have you read this thread ,,,,
    ROTARY ENCODERS and the like.


    Some history on this ,,,,
    SIMCONNECT FSX output Link2fs rotary encoders quadrature jim

    There are lots of ways to interface rotary encoders into the Arduino but most of them fall down on one thing or the other ,, too slow ,, difficult to have lots of them ,, too many mistakes ,, wont handle different cycles etc etc ,,, but the main one is that it must still perform well when the board is busy doing something else ,,, most of them failed miserably on that one.

    Your mission looks great and I can visualize the end product so a lot will be watching your progress.

    Thanks for the Kudos too ,,, Jim
    Last edited by Jim NZ; 09-17-2013 at 07:29 PM.
    www.jimspage.co.nz/intro.htm
    All this and Liz still loves me ! !

  6. Thanks WJH308 thanked for this post
  7. #4
    150+ Forum Groupie WJH308's Avatar
    Join Date
    Aug 2008
    Location
    San Francisco, CA
    Posts
    159
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: My PDE - Autopilot controls using Rotary Encoders and TM1640 displays

    Thanks Jim, I like the library I used, and when I write a sample program that only does the encoder thing, it is super fast with few errors. However with that said, you convinced me to try out the library you suggested in hopes of it being better!

    My other idea is to take a separate arduino mini and program it to be just a quadrature to impulse converter and feed the out lines directly into the pins on the mega. This way it removes all the processing of the encoders to a separate cpu and should give amazing performance.

    Another option is to wait until they make arduino's based on Arm processors with 20+ external interrupt pins! haha. Also wonder if boosting the arduino to 20mhz would help?

    Hmm bummer, his library has not been updated to use Arduino 1.0 and newer...
    Last edited by WJH308; 06-07-2012 at 06:20 PM. Reason: Added info about library not updated to 1.0

  8. #5
    500+ This must be a daytime job Jim NZ's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    764
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: My PDE - Autopilot controls using Rotary Encoders and TM1640 displays

    I had a play last night and yes I can confirm ,, it wont run in version 1. Bummer !!! It was by far the best of the million that I tried.

    I cant believe what Arduino have done to their IDE in the latest "upgrade" ,, no doubt there are reasons and improvements but at the end of the day they have "bust" the roll they had with the earlier ones and are making people "jump thru loops" just to have what they had in an earlier version.

    I do all my stuff in version 0022 (still available) and it's rock solid ,,, but when I get back into "PDE mode" I will have a proper play with version 1 and see "where to from there". The unknown here is their updated librarys compatibility.

    Sort of reminds me of ... XP up to Vista ,,,, then back down to XP

    Such is life ,,,, Jim
    www.jimspage.co.nz/intro.htm
    All this and Liz still loves me ! !

  9. #6
    150+ Forum Groupie WJH308's Avatar
    Join Date
    Aug 2008
    Location
    San Francisco, CA
    Posts
    159
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: My PDE - Autopilot controls using Rotary Encoders and TM1640 displays

    The WProgram.h file, which provides declarations for the Arduino API, has been renamed to Arduino.h. To create a library that will work in both Arduino 0022 and Arduino 1.0, you can use an #ifdef that checks for the ARDUINO constant, which was 22 and is now 100.
    Hmm... Let me try something...

  10. #7
    500+ This must be a daytime job Jim NZ's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    764
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: My PDE - Autopilot controls using Rotary Encoders and TM1640 displays

    Great ,,, I hope you sort it as Keith’s Quadrature library is brilliant.
    www.jimspage.co.nz/intro.htm
    All this and Liz still loves me ! !

  11. #8
    150+ Forum Groupie WJH308's Avatar
    Join Date
    Aug 2008
    Location
    San Francisco, CA
    Posts
    159
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: My PDE - Autopilot controls using Rotary Encoders and TM1640 displays

    I GOT IT TO WORK!!!! OH MY GOD, IT IS FAST AND WORKS AWSOME!!!!!! Woooo HOOOO!!!!!!!!!!!
    YESSSSSSSSSSSSSSSSSSSSS!!!!!
    Ok Boys and Girls, this is how you fix Keith's library to work!
    Open on up the quadrature.h file, and modify the top part, I simply copied this from the encoder library I was using before...
    Code:
    #if defined(ARDUINO) && ARDUINO >= 100
    #include "Arduino.h"
    #elif defined(WIRING)
    #include "Wiring.h"
    #else
    #include "WProgram.h"
    #include "pins_arduino.h"
    #endif
    #ifndef Quadrature_h
    #define Quadrature_h
    
    //#include < vector.h >
    
    //#include "WConstants.h"  **** Make sure to comment this out! *****
    
    
    #define DIGITAL_PINS (60) **** If you have a Mega change this to something like 60 ****
    The rest shouldn't be touched..

    Now you need to open up the Quadrature.cpp file and modify it this way...
    Code:
    #include "Quadrature.h"
    //#include "WConstants.h" //removed by Bill Holland to make compatible with Arduino 1.0+
    #include "Arduino.h" // Added by Bill Holland to make compatible with Arduino 1.0+
    //#include < avr/io.h >
    This library is by far the best!

  12. Thanks AK Mongo thanked for this post
  13. #9
    150+ Forum Groupie WJH308's Avatar
    Join Date
    Aug 2008
    Location
    San Francisco, CA
    Posts
    159
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: My PDE - Autopilot controls using Rotary Encoders and TM1640 displays


    I am a very happy camper!

  14. Thanks AK Mongo thanked for this post
  15. #10
    500+ This must be a daytime job Jim NZ's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    764
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: My PDE - Autopilot controls using Rotary Encoders and TM1640 displays

    YOU LITTLE BEAUTY ,,, IT WORKS.
    I just had a play and to start with it didnt compile ,,, There is a mistake in your posted code which is easy to fix.
    Where (in your code) it says,,,
    #define DIGITAL_PINS (60) **** If you have a Mega change this to something like 60 ****
    it should read ,,,
    #define DIGITAL_PINS (60) //**** If you have a Mega change this to something like 60 ****

    But this is great news Bill ,,, it makes changing up to version 1 not such a headache.
    You have every right to be a "Happy little camper"

    Thanks Bill ,,, Jim
    www.jimspage.co.nz/intro.htm
    All this and Liz still loves me ! !

Page 1 of 8 12345 ... LastLast