Results 1 to 5 of 5
  1. #1
    New Member
    Join Date
    Jan 2015
    Location
    Germany
    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

    Speed Indicator (Simple Code)

    Hello,

    im new in this forum. First Sorry for my English im from Germany but i try my best. I have used the search function, but im not able to find anything useful for me. So i try to understand the communication between Arduino and Link2fs and the very basic Commands of Ardiono. Now my first Simple Code for the Speed Indicator:

    #include


    Servo altim;


    int CodeIn;


    String Alt;


    void setup() {
    altim.attach(2);
    Serial.begin(115200);
    }


    void loop() {
    if (Serial.available() < 0) {
    CodeIn = getChar();
    if (CodeIn == '<') {LESSTHAN();}
    }
    }


    char getChar()
    {
    while(Serial.available() == 0);
    return((char)Serial.read());
    }
    void LESSTHAN() {
    CodeIn = getChar();
    if (CodeIn == 'P'){
    delay (11);
    Alt = "";
    Alt += char(Serial.read());
    Alt += char(Serial.read());
    Alt += char(Serial.read());
    int alti = Alt.toInt ();
    alti = map (alti, 0, 999, 0, 180);
    altim.write(alti);
    delay(15);
    }
    }

    But it doesn't work. I read the Command (PXXX) from link2fs and map/write it to the Servo. Whats wrong with it? Maybe one of you could help me.

    Thanks a lot =)

  2. #2
    25+ Posting Member
    Join Date
    Mar 2014
    Location
    France
    Posts
    27
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Speed Indicator (Simple Code)

    Hello Rellik_Toni
    you test your serial buffer when nothing is inside. The goal is to test if there are some characters on the serial port
    try to modify your loop() fonction with that
    if (Serial.available()) {...}
    or
    if (Serial.available() > 0) {...}

    and prefere to use
    Alt += getChar(); // waiting for a character and read it
    rather than
    Alt += char(Serial.read()); // read a character even if there is nothing on the line

    ryan

  3. #3
    New Member
    Join Date
    Jan 2015
    Location
    Germany
    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: Speed Indicator (Simple Code)

    Thank you very much. Now it works, but the servo moves only 100 and a little bit clunky.

    The Code.


    #include


    int CodeIn;


    Servo spd;


    String spe;


    void setup(){
    spd.attach(2);
    Serial.begin(115200);
    // The normal startup stuff goes here
    }


    void loop() {
    {OTHER();}// Check for "Other" things to do. (Non extraction stuff)
    if (Serial.available()) { //Check if anything there
    CodeIn = getChar(); //Get a serial read if there is.
    if (CodeIn == '=') {EQUALS();} // The first identifier is "=" ,, goto void EQUALS
    if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<" ,, goto void LESSTHAN
    if (CodeIn == '?') {QUESTION();}// The first identifier is "?" ,, goto void QUESTION
    if (CodeIn == '/') {SLASH();}// The first identifier is "/" ,, goto void SLASH (Annunciators)
    }
    }


    char getChar()//
    {
    while(Serial.available() == 0);// wait for data (Dont touch)
    return((char)Serial.read());// (Dont touch) Thanks Doug
    }


    void OTHER(){
    /* In here you would put code that uses other data that
    cant be put into an "extraction void" that references something else.
    Also in here you would put code to do something that was not
    relying on a current extraction.
    (Remember - The identifier voids only trigger when it receives that identifier)
    */
    }


    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 'W':
    //Do something
    break;

    case 'a':
    //Do something
    break;
    //etc etc etc
    // You only need the "Case" testing for the identifiers you expect to use.
    }
    }


    void LESSTHAN(){ // The first identifier was "<"
    CodeIn = getChar();
    switch(CodeIn) {
    case 'P':
    spe = "";
    spe += getChar();
    spe += getChar();
    spe += getChar();
    int vals = spe.toInt();
    vals = map(vals, 0, 200, 0, 179);
    spd.write(vals);
    delay(15);
    break;
    }
    }

  4. #4
    25+ Posting Member
    Join Date
    Mar 2014
    Location
    France
    Posts
    27
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Speed Indicator (Simple Code)

    Hi,
    I've never used servo with arduino (only with RC model) but may be you can play with optional parameters of the attach function (min and max)
    servo.attach(pin, min, max)

    see arduino reference
    http://arduino.cc/en/Reference/ServoAttach

    hope this helps you
    ryan

  5. #5
    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: Speed Indicator (Simple Code)

    This is part of code I used for the Attitude instrument. Worked quite good. Also try different settings for Cycle/Refresh on the
    "Communication Settings" tab in Link2fs. Also try to remove the "delay(15);"
    Code:
    #include "Servo.h"
    Servo RollServo;
    String Roll;
    
    int pos = 0;    // variable to store the servo position
    Code:
    void setup(){   RollServo.attach(9); // The servo pin
                    RollServo.write(90);  // set servo to mid-point
    Code:
            case 'R': //Found for reading the Vertical Speed     
       Roll = "";
       Roll += getChar();
       Roll += getChar();
       Roll += getChar();
       Roll += getChar();
          int Rolli = Roll.toInt(); 
          Rolli = map(Rolli, 100, -100, 0, 180 );   
          RollServo.write(Rolli); pos += 1;          
       break;
    Life was hard, but then came Windows 7.
    Now we can fly! --------
    --------