Results 1 to 7 of 7
  1. #1
    Our new friend needs to reach 10 posts to get to the next flight level
    Join Date
    Sep 2013
    Location
    Tamworth Australia
    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 with extracts

    G’day Folk,

    I had some simple code working (taken from Jims and Cohorts) where in FS9 if the
    airspeed went above 60 kts it would turn on the LED pin 13. I have hooked this output to mechanical VDO via a mosfet. All **** of a learning curve but it worked well and very pleasing since I don’t know much about C at all – thanks to Jim’s and others help of course.

    The only problem is when I pause the simulator, the VDO keeps working – and this is undesirable. Jims new Link2fs_Multi_FS9 has a pause extract (?f). This is great.
    However my eagerness far outweighs my ability. I would like the code to say

    IF speed is > than 60 kts and ?f is <1, then pin 13 to high.

    I have had a go at the code, but I have a feeling you cant do what I tried to do.
    If someone had the time to peruse and offer suggestions I would be most grateful.
    Hopefully code is attached


    Regards,

    David
    Attached Files Attached Files

  2. #2
    25+ Posting Member
    Join Date
    May 2014
    Location
    Bulgaria and United Kingdom
    Posts
    56
    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 with extracts

    Hi,
    Pin 13 is no good for what you are trying to do ( turning things on-off) and here is why:
    1- it is difficult to turn pin 13 LOW just by “ digitalWrite(13, LOW); It keeps going High again , unless you connect additional 10k resistor btw. Pin 13 and GND to keep it LOW when you don’t need it High.
    2- during USB connect/disconnect pin13 floats , which results in your device being turned on-off couple of times and this is undesirable.
    If you consider using a different pin – for example – pin 12, here is a code you could try:
    String asi, asiold, hold;
    int airspeed;

    int CodeIn;// for extractions

    void setup(){

    Serial.begin(115200);

    pinMode(12, OUTPUT); digitalWrite(12,LOW);

    }

    void loop() {
    if (Serial.available()) {
    CodeIn = getChar();

    if (CodeIn == '<') {LESSTHAN();}
    if (CodeIn == '?') {QUESTION();}
    }
    }

    char getChar()
    {
    while(Serial.available() == 0);
    return((char)Serial.read());
    }


    void LESSTHAN(){
    CodeIn = getChar();
    switch(CodeIn) {

    case 'P':
    delay (10); // It seems to need a delay here

    asi = "";
    asi += char(Serial.read());
    asi += char(Serial.read());
    asi += char(Serial.read());

    if (asi != asiold){
    airspeed = asi.toInt();
    if(airspeed > 60 && hold == "0" ) { digitalWrite(12, HIGH); }
    else { digitalWrite(12, LOW); }
    asiold=asi; }
    break;

    }
    }

    void QUESTION(){
    CodeIn = getChar();
    switch(CodeIn) {

    case 'f':
    hold = "";
    hold += char(Serial.read());
    break;

    }
    }

  3. #3
    Our new friend needs to reach 10 posts to get to the next flight level
    Join Date
    Sep 2013
    Location
    Tamworth Australia
    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

    Re: Working with extracts

    Thanks Trakon - that code looks nice and tidy.
    I am looking forward to trying your suggestion
    when I get home this evening.
    Will let you know how it goes.
    Cheers,
    David

  4. #4
    Our new friend needs to reach 10 posts to get to the next flight level
    Join Date
    Sep 2013
    Location
    Tamworth Australia
    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

    Re: Working with extracts

    hi again Trakon,

    Nearly there - tied pin 13 to ground via a 10K resister - going for a quick remedy
    and used your code (changing pinouts- to 13 for the moment - Have taken on
    board your advice though)

    The led turns on >60 knots and off again below 60 kts as planned.
    The only problem is the pause to make pin go LOW is not working.

    Suggestions?

    Thanks, David

  5. #5
    25+ Posting Member
    Join Date
    May 2014
    Location
    Bulgaria and United Kingdom
    Posts
    56
    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 with extracts

    I havn't debbuged the code ( sorry for that ). You could modify the code by cutting
    if(airspeed > 60 && hold == "0" ) { digitalWrite(12, HIGH); } else { digitalWrite(12, LOW); }
    from the 'asi' extraction area and paste it in the 'f' extraction area to make it possible to execute, when there is
    no change in airspeed.

    Alternatively, the better approach is not to do things in the 'extractions area' , but to do them in dedicated void functions. It should look like that:


    String asi, asiold, hold;
    int airspeed;
    int CodeIn;// for extractions

    void setup(){
    Serial.begin(115200);

    pinMode(12, OUTPUT); digitalWrite(12,LOW);

    }


    void loop() {

    {OnOff();}
    if (Serial.available()) {
    CodeIn = getChar();

    if (CodeIn == '<') {LESSTHAN();}
    if (CodeIn == '?') {QUESTION();}
    }
    }

    char getChar()
    {
    while(Serial.available() == 0);
    return((char)Serial.read());
    }


    void LESSTHAN(){
    CodeIn = getChar();
    switch(CodeIn) {


    case 'P':
    delay (10); // It seems to need a delay here
    asi = "";
    asi += char(Serial.read());
    asi += char(Serial.read());
    asi += char(Serial.read());

    if (asi != asiold){
    airspeed = asi.toInt();
    asiold=asi; }
    break;
    }
    }


    void QUESTION(){
    CodeIn = getChar();
    switch(CodeIn) {

    case 'f':
    hold = "";
    hold += char(Serial.read());
    break;
    }
    }

    void OnOff(){ if(airspeed > 60 && hold == "0" ) { digitalWrite(12, HIGH); }
    else { digitalWrite(12, LOW); } }
    PS: make sure in Link2fs connection area 'Refresh Time' is set to 5 or so and is not '0'( for some funny reason extraction 'f' sometimes is missed and need to be re-sent).

  6. #6
    Our new friend needs to reach 10 posts to get to the next flight level
    Join Date
    Sep 2013
    Location
    Tamworth Australia
    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

    Re: Working with extracts

    Alternatively, the better approach is not to do things in the 'extractions area' , but to do them in dedicated void functions........

    Excellent work Trakon - you have made my night. I uploaded the 'dedicated void functions' concept you wrote. Works really
    well.

    I appreciate your effort - makes me want to get more serious with learning C.

    Thanks again,

    Regards, David

  7. #7
    25+ Posting Member
    Join Date
    May 2014
    Location
    Bulgaria and United Kingdom
    Posts
    56
    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 with extracts

    I am glad it worked.