Results 1 to 8 of 8
Thread: Building a system using Arduino
-
06-09-2012, 04:37 AM #1
- Join Date
- Jun 2012
- Location
- Norway
- Posts
- 5
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); } }
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")
-
Post Thanks / Like - 0 Thanks, 1 Likes, 0 Dislikes
Mickey_Techy liked this post
-
06-09-2012, 12:10 PM #2
- Join Date
- Apr 2008
- Location
- Graham, WA
- Posts
- 296
Re: Building a system using Arduino
Great job!
g.
-
06-09-2012, 12:27 PM #3
Re: Building a system using Arduino
How many switches can you connect to the arduino?
-
06-09-2012, 01:55 PM #4
- Join Date
- Oct 2009
- Location
- Juneau, AK
- Posts
- 547
Re: Building a system using Arduino
That is outstanding sir!
Thanks for the input!
Reidhttp://juneaucessnasim.blogspot.com
N58243 (virtual)- Low and Slow...
-
06-09-2012, 03:55 PM #5
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 ,, Jimwww.jimspage.co.nz/intro.htm
All this and Liz still loves me ! !
-
06-26-2012, 12:52 PM #6
- Join Date
- Oct 2009
- Location
- Juneau, AK
- Posts
- 547
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!
Reidhttp://juneaucessnasim.blogspot.com
N58243 (virtual)- Low and Slow...
-
09-08-2012, 05:26 PM #7
- Join Date
- Sep 2012
- Location
- United States
- Posts
- 9
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")
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!
-
09-10-2012, 09:31 AM #8
- Join Date
- Jun 2012
- Location
- Norway
- Posts
- 5
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
-
Post Thanks / Like - 2 Thanks, 1 Likes, 0 Dislikes
forty-2 liked this post
Hi...realize this has been a long time, but I'm heading down the path of building my own 777...
B777 Overhead Panel Design