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

    Success! and a lesson I learned

    Hi, I've been working off and on for about three months on a project that I thought would be nice to have as a self made add on for FSX. I used some code that I found here http://mkme.org/forum/viewtopic.php?f=2&t=33 that was written by WhiteStar01. I worked for better than two months trying to get the code to work and I couldn't understand why it wouldn't, but I was to stubborn to give up. I finally discovered the problem on night this week. Here is my code

    Code:
    #include 
    
    #include 
    #include "utility/Adafruit_PWMServoDriver.h"
    
    #include 
    Adafruit_MotorShield AFMS = Adafruit_MotorShield();
    
    Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(75, 1);
    Adafruit_StepperMotor *myMotor2 = AFMS.getStepper(75, 2);
    
    /* 
        This code is in the public domain
        For use with "Link2fs_Multi"
        Jimspage.co.nz
        My thanks to the Guys that gave me snippets of code. 
        
    // Code adapted for flight simulator interface of turn coordinator ball
    
    */
    
    int CodeIn;// used on all serial reads
    int KpinNo; 
    int Koutpin;
    String asi;
    int asil;
    int val;
    int asip;
    
    
    String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
    
    void setup() 
    { 
      AFMS.begin();
      myMotor1->setSpeed(45);     //200 FOR INTERLEAVE
      myMotor2->setSpeed(45);
      Serial.begin(115200); 
    }
    
    void loop() {
     
      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)
      }
    
    }
    
    char getChar()// Get a character from the serial buffer
    {
      while(Serial.available() == 0);// wait for data
      return((char)Serial.read());// Thanks Doug
    }
    
    void EQUALS(){ // The first identifier was "="
     CodeIn = getChar(); // Get another character
      switch(CodeIn) {// Now lets find what to do with it
        case 'A'://Found the second identifier
           //Do something
        break;
         
        case 'B':
           //Do something
        break;
         
        case 'C':
           //Do something
        break;
        
             //etc etc etc
         }
    }
    
     void LESSTHAN(){ // The first identifier was "<"
     CodeIn = getChar(); // Get another character
      switch(CodeIn) {// Now lets find what to do with it
        case 'P'://Found the second identifier Turn coordinator
           
        {
        asi = "";
        asi += getChar();
        asi += getChar();
        asi += getChar();
        //tc += getChar();
       // tc += getChar();
        //tc += getChar();
         asip = asi.toInt();
           
    //  asil = asip;            // reads the value of the data from FSX turn coordinator
      if((asip>asil) && (asip>55)){                               //Set to rotation speed or slightly above
      myMotor1->step(22, FORWARD, INTERLEAVE);        //changed from 18 to 22
      myMotor2->step(22, BACKWARD, INTERLEAVE);                        //60 for interleave
      Serial.println ("forward"); //added for troubleshooting
      }
      
      else if((asip55)){ 
      myMotor1->step(22, BACKWARD, INTERLEAVE);
      myMotor2->step(22, FORWARD, INTERLEAVE);
      Serial.println ("backward"); //added for troubleshooting
      }
      
       
        }
        break;
      
        case 'B':
           //Do something
        break;
         
        case 'C':
           //Do something
        break;
           
             //etc etc etc
         }
         asil = asip; 
    }
    
    void QUESTION(){ // The first identifier was "?"
    CodeIn = getChar(); // Get another character
      switch(CodeIn) {// Now lets find what to do with it
        case 'A'://Found the second identifier
           //Do something
        break;
         
        case 'B':
           //Do something
        break;
         
        case 'C':
           //Do something
        break;
           
             //etc etc etc
         }
    }
    void SLASH(){ // The first identifier was "/" (Annunciator)
      //Do something
    }
    The orginal code had a string called TC and a int called TC1. When I was changing the code over to airspeed instead of turn coordinator I was stumped because I actually needed two ints, one called asil for the previous reading, and another called asip that is what the string asi is converted to. When I tried to put something like: if (asi>asil) I would get the error that I was trying to compare a int to a string. Then light went off in my pea sized brain! As soon as I changed the line asil = asi.toInt(); to read asip = asi.toInt(); and then compared asip to asil all was good. I don't know whether it is my computer or what but the 1 and the small letter l look indentical.

    Just thought I'd toss this little life lesson out there in case someone else has the same trouble.

    Mark

  2. #2
    150+ Forum Groupie
    Join Date
    Nov 2013
    Location
    Evansville, Indiana
    Posts
    243
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Success! and a lesson I learned

    Congrats!

    When it comes to variables, I use long names because usually I either have unfinished work after months of not touching it, or I come up with a mod that needs tweaking.

    Like these...

    Code:
    int AP_alt_set[5];   
    int AP_vs_set[4];
    int AP_hdg_set[3];
    int AP_crs_set[3];
    int AP_speed_kt_set[3];
    int AP_mach_set[3];
    int AP_AutoThrottle_arm;
    int AP_Flight_Director_Active;
    It helps me get up to speed more quickly.
    I will only ever be, half the Geek that I wished I was.
    TheGeekForge.Com

  3. Likes BuzziBi liked this post