Page 1 of 2 12 LastLast
Results 1 to 10 of 18
  1. #1
    Our new friend needs to reach 10 posts to get to the next flight level
    Join Date
    Jun 2010
    Location
    SFO
    Posts
    6
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Working: Easy Stepper Motor Control for Flaps Gauge

    Hi everyone,

    I've included Arduino sample code to control a flaps gauge using a stepper motor via Link2FS. You'll notice there's a function called flapMeasureTo737Degree(). This scales incoming flap readings to the specific needle displacement in degrees for a Boeing 737 Flaps Gauge. Code could easily be modified to work with any plane's gauge.

    I've looked at the great code and examples that Jim assembled and figured I'd give it a try! Very easy to configure with Arduinos! As I ramped up, I worked with a cheap, little stepper motor and driver I obtained from amazon. All of $5 USD! I also found the following page helpful learning how to control stepper motors using an arduinos: http://www.4tronix.co.uk/arduino/Stepper-Motors.php.

    So I give you v1 of Stepper control for Link2FS! As I said, I've built on top of examples from the web and those provided by Jim with Link2FS. I've tried to keep the code as clean and commented as possible. I've also tried to name the variables and functions specifically for flaps since I will probably add many more stepper motors to this code base enabling the one Arduino board to control multiple analog displays.

    I would appreciate any feedback, especially on how to make sure I'm using cycles on the Arduino efficiently. I've read that the single-threadedness of the Arduino can get blocked performing tasks like stepping a motor or reading inputs, I'd appreciate any improvements to the code. Note: This code is intended to be used for free by all and not to be included in any commercial packaging.

    This could easily morph into a really cool auto throttle! Next I'll be looking into multi-plexing MCP and NAV displays. Will see how many 7-segment displays we can run on one arduino card!

    Enjoy!
    Dan

    Code:
    //  Stepper Motor Flap Guage for Link2FS
    //  v. 1.0
    //  1/20/2014
    // 
    //  Outlines how to control a small stepper motor for a slaps gauge.
    //  Use for free. Do not distribute for fee.  Intended to be free forever...
    //
    //  Borrowed heavily from:
    //    Jim's page: http://www.jimspage.co.nz/Link2fs_Multi.htm
    //    4tronix page for my specific stepper: http://www.4tronix.co.uk/arduino/Stepper-Motors.php
    // 
    //  Built to work with the cheap, small stepper from amazon:
    //  28BYJ-48 28BYJ48 DC 5V 4-Phase 5-Wire Arduino Stepper Motor with ULN2003 Driver Board
    //
    //  Configured to map incoming data to corresponding steps for 737 gauge
    //
    ////////////////////////////////////////////////
    
    
    int CodeIn;           // used on all serial reads
    int flapMotorPosition;    // Global variable to track current step count for flap motor
    
    
    
    
    //declare variables for pins....
    int flapMotorPin1 = 8;    // Blue   - 28BYJ48 pin 1
    int flapMotorPin2 = 9;    // Pink   - 28BYJ48 pin 2
    int flapMotorPin3 = 10;   // Yellow - 28BYJ48 pin 3
    int flapMotorPin4 = 11;   // Orange - 28BYJ48 pin 4
    int flapMotorLookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
    
    
    
    
    //Main Config variables...
    float flapMultiplier = 1.422; //Used to multiply incoming flap degrees to number of steps for stepper. 512 steps per 360 degrees for me....
    int flapstepDelay = 3000;  //variable to set stepper speed. lower = faster motor speed...
    
    
    //////////////////////////////////////////////////////////////////////////////
    void setup() {
      //declare the motor pins as outputs
      pinMode(flapMotorPin1, OUTPUT);
      pinMode(flapMotorPin2, OUTPUT);
      pinMode(flapMotorPin3, OUTPUT);
      pinMode(flapMotorPin4, OUTPUT);
      Serial.begin(115200);    // NEEDS TO BE 115200 TO WORK WITH LINK2FS!!!!
    
    
      flapMotorPosition = 512; //Sets motor position to worst-case scenaio location
      stepTo(0);      //Index motor back to flap position 0. Assumes index pin on gauge face. 
      Serial.println("Gauge reset and configured.");
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    void loop(){
      if (Serial.available()) {
        CodeIn = getChar();//get a character
        //Serial.println(CodeIn);
        if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<"
      }//end of serial check
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    
    
    
    
    char getChar()// Get a character from the serial buffer
    {
      while(Serial.available() == 0);// wait for data
      return((char)Serial.read());// Thanks Doug
    }
    
    
    //////////////////////////////////////////////////////////////////////////////
    
    
    void LESSTHAN(){    // The first identifier was "<"
    
    
      CodeIn = getChar();       // Get another character
      switch(CodeIn) {          // Now lets find what to do with it
        case 'G':               // Found the second identifier ("G" Flaps position)
                                 // Initialize some variables needed for flaps...
          String  flapLink2FS;   // Store incoming flap metrics from Link2FS 
          float flaps737degree;  // Stores degrees deflection on 737 flap gauge.
          int flapStepPosition;  // Store Position count for flap stepper motor 
          
          flapLink2FS += getChar();  //Get three characters for flap data from Link2fs
          flapLink2FS += getChar();
          flapLink2FS += getChar();
          flaps737degree = flapMeasureTo737Degree(flapLink2FS.toInt()); //Converts Link2FS Metric to degree deflection on 737 gauge
          flapStepPosition = flaps737degree * flapMultiplier; // Converts degrees deflection to step count 
          stepTo(flapStepPosition);
      }
    }
    
    
    float flapMeasureTo737Degree(int flapMeasure){
    //  flapMeasureTo737Degree converts the code coming from link2FS to degrees 
    //  the needle deflects on a 737 Flap guage...
    //  Input: int flapMeasure (number from 0 to 57).
    //  Ouput: int degrees for needle from 0 indication.
    
    
      if (flapMeasure==0){ //Flaps 0 on 737 in FSX
        return 0;
      }
      if (flapMeasure==1){ //Flaps 1 
        return 36;
      }
      if (flapMeasure==3){ //Flaps 2
        return 72;
      }
      if (flapMeasure<=7){ //Flaps 5 Setting
        return map(flapMeasure, 3, 7, 72, 114);
      }
      if (flapMeasure<=14){ //Flaps 10 Setting
        return map(flapMeasure, 7, 14, 114, 146);
      }
      if (flapMeasure<=21){ //Flaps 15 Setting
        return map(flapMeasure, 14, 21, 146, 180);
      }
      if (flapMeasure<=36){ //Flaps 25
        return map(flapMeasure, 21, 36, 180, 215);
      }
      if (flapMeasure<=43){ //Flaps 30
        return map(flapMeasure, 36, 43, 215, 240);
      }
      else{
        return map(flapMeasure, 43, 57, 240, 270);
      }
    }
    
    
    void stepTo(int targetPosition){
      do{
         if(flapMotorPosition < targetPosition){
           clockwise();
           flapMotorPosition++;
         }
         else if(flapMotorPosition > targetPosition){
          anticlockwise();
          flapMotorPosition--;
         }
      } while (flapMotorPosition != targetPosition );
    }
    
    
    
    
    
    
    void anticlockwise(){
    // step motor 1 step anti-clockwise
      for(int i = 0; i < 8; i++)
      {
        flapSetOutput(i);
        delayMicroseconds(flapstepDelay);
      }
    }
    
    
    void clockwise(){
    // step motor 1 step clockwise
      for(int i = 7; i >= 0; i--)
      {
        flapSetOutput(i);
        delayMicroseconds(flapstepDelay);
      }
    }
    
    
    void flapSetOutput(int out)
    {
      digitalWrite(flapMotorPin1, bitRead(flapMotorLookup[out], 0));
      digitalWrite(flapMotorPin2, bitRead(flapMotorLookup[out], 1));
      digitalWrite(flapMotorPin3, bitRead(flapMotorLookup[out], 2));
      digitalWrite(flapMotorPin4, bitRead(flapMotorLookup[out], 3));
    }

  2. Thanks Jim NZ thanked for this post
  3. #2
    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: Working: Easy Stepper Motor Control for Flaps Gauge

    Welcome aboard Dan ,, that code is really going to help some Guys so thanks heaps for that.

    Watch out tho ,,, some other Guys are scratching their heads for ideas with driving a compass with steppers so no doubt they will be digging thru your code.

    The code appears to be complete with nothing getting peeled off by the forum "machine" as I tested it and it compiles fine ,, although I don't have any steppers

    Welcome and thanks again Dan ,,, Jim
    www.jimspage.co.nz/intro.htm
    All this and Liz still loves me ! !

  4. #3
    New Member
    Join Date
    Mar 2014
    Location
    United Kingdom
    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

    Re: Working: Easy Stepper Motor Control for Flaps Gauge

    thank you so much.one question tho, how could we store the zero flaps position because if fsx crashes or you disconnect with flaps in any position other than zero, it will take this position and make it zero, making flaps gauge read incorrectly. or would it be possible to add a micro switch for manual reset - i.e. press once and needle slowly goes round then press again when at zero???

    oh and jim - BIG THUMBS UP DUDE - YOU STARTED A REVOLUTION THANK YOU SIR

  5. #4
    150+ Forum Groupie
    Join Date
    Jan 2007
    Location
    Netherlands
    Posts
    227
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Working: Easy Stepper Motor Control for Flaps Gauge

    Hi Dan


    Great job!! I think a lot of us are looking for this.

    Is it possible for you to add a picture of the Arduino board with stepper connected and flap gauge?

    Best regards
    Jan

  6. #5
    New Member
    Join Date
    Feb 2012
    Location
    Nine Mile Falls
    Posts
    3
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Working: Easy Stepper Motor Control for Flaps Gauge

    This is great as I am a newby to all this and am trying to make a flaps gauge fo my home built 737NG simulator. How do I print out this code? every time I try to print it i only get the 9 pages and no code in the box.

  7. #6
    500+ This must be a daytime job



    Join Date
    Oct 2009
    Location
    Juneau, AK
    Posts
    619
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Working: Easy Stepper Motor Control for Flaps Gauge

    What is the reference to an index pin? That sets the 0 point, correct? What is the physical mechanism? A switch?
    http://juneaucessnasim.blogspot.com
    N58243 (virtual)- Low and Slow...

  8. Likes Snegs liked this post
  9. #7
    New Member
    Join Date
    Jun 2014
    Location
    Brasil
    Posts
    2
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Working: Easy Stepper Motor Control for Flaps Gauge

    Hello, works well with standard 737, but why not work with the PMDG 737ngx?
    Noticed that PMDG has many more written in link2_fs monitor for Arduino Board than 737 standard. Help please! tks


  10. #8
    New Member
    Join Date
    Jun 2014
    Location
    Brasil
    Posts
    2
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Working: Easy Stepper Motor Control for Flaps Gauge

    Hello, works well with standard 737, but why not work with the PMDG 737ngx?
    Noticed that PMDG has many more written in link2_fs monitor for Arduino Board than 737 standard. Help please! tks


  11. #9
    25+ Posting Member
    Join Date
    Feb 2010
    Location
    KFLL
    Posts
    57
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Working: Easy Stepper Motor Control for Flaps Gauge

    Quote Originally Posted by djkoell View Post
    Hi everyone,

    Code:
    //  Stepper Motor Flap Guage for Link2FS
    //  v. 1.0
    //  1/20/2014
    // 
    //  Outlines how to control a small stepper motor for a slaps gauge.
    //  Use for free. Do not distribute for fee.  Intended to be free forever...
    //
    //  Borrowed heavily from:
    //    Jim's page: http://www.jimspage.co.nz/Link2fs_Multi.htm
    //    4tronix page for my specific stepper: http://www.4tronix.co.uk/arduino/Stepper-Motors.php
    // 
    //  Built to work with the cheap, small stepper from amazon:
    //  28BYJ-48 28BYJ48 DC 5V 4-Phase 5-Wire Arduino Stepper Motor with ULN2003 Driver Board
    //
    //  Configured to map incoming data to corresponding steps for 737 gauge
    //
    ////////////////////////////////////////////////
    
    
    int CodeIn;           // used on all serial reads
    int flapMotorPosition;    // Global variable to track current step count for flap motor
    
    
    
    
    //declare variables for pins....
    int flapMotorPin1 = 8;    // Blue   - 28BYJ48 pin 1
    int flapMotorPin2 = 9;    // Pink   - 28BYJ48 pin 2
    int flapMotorPin3 = 10;   // Yellow - 28BYJ48 pin 3
    int flapMotorPin4 = 11;   // Orange - 28BYJ48 pin 4
    int flapMotorLookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};


    I stumbled across this gem last week and am most intrigued.

    I am doing a B727-200 home cockpit in FS9 using real instruments which have been gutted.

    I'm assuming that the int flapMotorLookup[8] is for the 8 flap positions in the B737: 1, 2, 5, 10, 15, 25, 30 and 40

    For the B727, there are only 6 : 2, 5, 15, 25, 30 and 40.

    What are these 'B' numbers? I would have to delete two (2) of them for my B727.....but which?
    I don't have the ULN2003 but have L293Ds. Would this work?

    I would sincerely appreciate any guidance and tips as I am brand new to Arduino and Link2FS.

    Thank you very much.

    Jay
    KFLL

  12. #10
    500+ This must be a daytime job



    Join Date
    Jan 2009
    Location
    Tampa
    Posts
    531
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Working: Easy Stepper Motor Control for Flaps Gauge

    Unless I am way off base here, those "B" numbers are binary patterns in a lookup table. If you look at the rest of that code axample, you will see where the pins to the stepper controller are set using those declared values:

    //set pins to ULN2003 high in sequence from 1 to 4
    //delay "motorSpeed" between each pin setting (to determine speed)
    void anticlockwise()
    {
    for(int i = 0; i < 8; i++)
    {
    setOutput(i);
    delayMicroseconds(motorSpeed);
    }
    }

    void clockwise()
    {
    for(int i = 7; i >= 0; i--)
    {
    setOutput(i);
    delayMicroseconds(motorSpeed);
    }
    }

    void setOutput(int out)
    {
    digitalWrite(motorPin1, bitRead(lookup[out], 0));
    digitalWrite(motorPin2, bitRead(lookup[out], 1));
    digitalWrite(motorPin3, bitRead(lookup[out], 2));
    digitalWrite(motorPin4, bitRead(lookup[out], 3));
    }

Page 1 of 2 12 LastLast