Results 1 to 8 of 8
  1. #1
    Our new friend needs to reach 10 posts to get to the next flight level
    Join Date
    Jun 2012
    Location
    Norway
    Posts
    8
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Building a system using Arduino

    Hi guys,

    I just made my first hardware switch control and events in the flight simulator (P3D) using arduino and lua. I have connected a simple switch to the arduino board which sends commands over a serial link (through USB) to a lua script. The script subscribes to the parking brake event.

    Every time I flicked the switch it turns the parking brake off and on, and whenever an internal simulation events (such as pressing the brake pedal) tries to turn the parking brake off, this information is sent to the board which can verify if this is in sync with the hardware switch. If the hardware switch says that the parking brake should still be on, it re-sends the parking brake command.

    I plan to expand this to build my own panels with switches, displays and rotary knobs to take care of the most common FSX functions.

    This is a picture of the setup. Impressive, huh?
    12 - 1.jpg

    The arduino sketch is here:
    Code:
    
    #define STRLEN 20
    
    char buffer[STRLEN];
    int  bufferIndex = 0;
    int parking_break = 100;
    int parking_break_pin = 4;
    int digital_read_value = 100;
    long last_debounce=0;
    long debounce_delay=100;
    
    
    void setup()
    {
      Serial.begin(115200);
      Serial.flush();
      pinMode(parking_break_pin, INPUT);
      digitalWrite(parking_break_pin, HIGH);
    }
    
    void loop()
    {
      if( Serial.available())
      {
        char ch = Serial.read();
        if( ch == '\n')  // is this the terminating carriage return
        {
          buffer[ bufferIndex ] = 0; // terminate the strin
          bufferIndex = 0;  // reset the index ready for another string
          parse_string(buffer);
          // do something with the string
        }
        else
          buffer[ bufferIndex++ ] = ch; // add the character into the buffer
      }
      check_input();
    } 
    
    void parse_string (char* buffer)
    {
      char *name = NULL, *value = NULL;
      //Serial.write(buffer);
      name =strtok (buffer, ":");
      value =strtok( NULL, ":");
      if (name && value) {
        handle_command (name, value);
      }
    }
    
    void handle_command (char* name,char* value)
    {
      if (strcmp( name, "parkingb") == 0)
      {
        if(parking_break !=atoi( value))
        {
          send_value ("parkingb", parking_break);
        }
      }
    }
    
    void send_value (char* name,int value)
    {
      char buffer [20];
      sprintf (buffer, "%s:%d\0", name, value);
      Serial.write (buffer);
    }
    
    void check_input ()
    {
      digital_read_value = digitalRead(parking_break_pin);
      if (digital_read_value!= parking_break) {
        parking_break = digital_read_value;
        last_debounce= millis();
      }
      if (last_debounce!=0 && (millis() - last_debounce) > debounce_delay) {
        //Serial.write("changed value");
    
        last_debounce=0;
        send_value ("parkingb", parking_break);
      }
    }
    And the lua script is here:
    Code:
    speed = 115200
    handshake = 0
    
    dev = com.open("COM8", speed, handshake)
    if dev == 0 then
        ipc.display("Could not open VRIdevice port")
        ipc.exit()
    end
    
    
    
    function handlecom(handle, str)
        ipc.log("com=" .. str)
        if string.match(str, "parkingb:(%d)") then
        	state = tonumber(string.match(str, "parkingb:(%d)"))
            if state == 1 then
                ipc.writeUD(0x0BC8, 32767)
            else
                ipc.writeUD(0x0BC8, 0)
    	end
        end
    end
    
    -- ----------------------------- Callbacks -------------------
    function parking_break (offset, value)
        if value == 0 then
            com.write(dev, "parkingb:0\n")
        else
            com.write(dev, "parkingb:1\n")
        end
    end
    
    
    ipc.sleep(500)
    event.com(dev, 20, 5, 0, "handlecom")
    event.offset(0x0BC8, "UW", "parking_break")

  2. Likes Mickey_Techy liked this post
  3. #2
    300+ Forum Addict
    Join Date
    Apr 2008
    Location
    Graham, WA
    Posts
    323
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Building a system using Arduino

    Great job!

    g.

  4. #3
    10+ Posting Member Simbuilder's Avatar
    Join Date
    May 2012
    Location
    Sweden
    Posts
    14
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Building a system using Arduino

    How many switches can you connect to the arduino?

  5. #4
    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: Building a system using Arduino

    That is outstanding sir!

    Thanks for the input!

    Reid
    http://juneaucessnasim.blogspot.com
    N58243 (virtual)- Low and Slow...

  6. #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: Building a system using Arduino

    Ditto what Reid said ,, great we have another way to get the Arduino into the cockpit.

    Neat stuff and thanks ,, Jim
    www.jimspage.co.nz/intro.htm
    All this and Liz still loves me ! !

  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: Building a system using Arduino

    Kolaf,

    I would love to be able to just read an FSUIPC offset and send to serial. Do you have an idea how your code could be modified to do so?

    It seems like it should be simple enough, but LUA eludes me.

    If I could do this, it would be easier to create all of my gauges, without asking Jim to provide extractions for all of them.

    Any help you can give would be appreciated!

    Reid
    http://juneaucessnasim.blogspot.com
    N58243 (virtual)- Low and Slow...

  8. #7
    10+ Posting Member
    Join Date
    Sep 2012
    Location
    United States
    Posts
    10
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Building a system using Arduino

    Mongo -

    here's a somewhat simplified snippit from my ardiuno radio project (to be posted soon!)

    Code:
    --set up serial port
    speed = 57600
    handshake = 0
    arduinoPort = "COM3"
    Serial_In = 0
    Com1S = 0
    
    --this is the function called when the offset changes
    --could be prettier, but its a work in progress.
    function com1aSend(offset, value)
        buf = string.format("%X", ipc.readStruct(0x034E, "UW"))  --assigns offset to variable
        buf = string.format("%s", buf)                                     --makes it a string - redundant?
        com.write(dev, string.format("%s\n", buf))                    --sends sends via serial w/ newline
    end
    
    --this calls the above function whenever the offset changes
    event.offset(0x034E, "UW", "com1aSend")
    This just spits the value out to the serial port as a string. If you're planning on sending out more then one value, you'll want to prepend each with an identifying character that the arduino uses to determine which value you've sent.

    Thank you Kolaf & Jim for your excellent work and posts here. I've found your posts a number of times while researching my own project, and they've always steered me in the right direction!

  9. #8
    Our new friend needs to reach 10 posts to get to the next flight level
    Join Date
    Jun 2012
    Location
    Norway
    Posts
    8
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Building a system using Arduino

    I have recently completed my prototype, and the current version of my software can be found here, together with a demo video https://code.google.com/p/arduino-fs/ .

    The source code is by no means elegant, but it works

  10. Thanks Mickey_Techy, forty-2 thanked for this post
    Likes forty-2 liked this post