Arduino Servo Control Flight Simulator

All things Arduino!
Post Reply
Eric
Posts: 164
Joined: Fri Feb 21, 2014 4:52 pm

Arduino Servo Control Flight Simulator

Post by Eric » Tue Apr 15, 2014 8:09 am

A viewer asked me to post the information/code I used to interface a servo to my flight simulator. Here is the video:

https://www.youtube.com/watch?v=pdeHEoy ... 0c5xocnw0k

So here is the Arduino code I used for the slip/skid indicators. There is no real need for the variable speed servo library anymore so it can be removed. Enjoy!


/* 
    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

*/
#include <VarSpeedServo.h> //Different Servo Library which offers variable speed
VarSpeedServo myServo; //define myservo (NOTE- it has different case letters than normal servo library)

int CodeIn;// used on all serial reads
int KpinNo;
int Koutpin;
String tc;
int tc1;
int val;


String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;

void setup()

myServo.attach(9); // attaches the servo on pin 9 to the servo object
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 'N'://Found the second identifier Turn coordinator
       
    {
    tc = "";
    tc += getChar();
    tc += getChar();
    tc += getChar();
    //tc += getChar();
   // tc += getChar();
    //tc += getChar();
    int tc1 = tc.toInt(); // convert it to an integer (Thanks Phill)
       
  val = tc1;            // reads the value of the data from FSX turn coordinator
  val = map(val, 0, 256, 0, 179); // scale it to use it with the servo ( between 0-256 for FSX turn coordinator,between 0 and 180 for servo)
  myServo.slowmove(val,45); // sets the servo position according to the scaled value (second number is servo speed 0=slowest, 255=fastest)
  Serial.println (val); //added for troubleshooting
   
    }
    break;
     
    case 'B':
       //Do something
    break;
     
    case 'C':
       //Do something
    break;
       
         //etc etc etc
     }
}

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
}
I make videos and content on all things electronics, 3D prining and "Maker"
http://www.mkme.org
https://www.youtube.com/mkmeorg

Davidino
Posts: 3
Joined: Fri Aug 01, 2014 6:49 pm

Re: Arduino Servo Control Flight Simulator

Post by Davidino » Fri Aug 01, 2014 7:24 pm

Hi,

i've tried your code to comand a servo for autothrottle.

I changed the 'N' to 'V' case for the reading values.

But the servo won't move :cry:

Maybe I forgot something in the sketch... Please take a look

------------------------------------START------------------------------------------

/*
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

*/
#include <Servo.h>
#include <VarSpeedServo.h> //Different Servo Library which offers variable speed
Servo myservo; // Names the servo


int CodeIn;// used on all serial reads
int KpinNo;
int Koutpin;
String tc;
int tc1;
int val;

String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
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 'V'://Found the second identifier Turn coordinator

{
tc = "";
tc += getChar();
tc += getChar();
tc += getChar();
//tc += getChar();
// tc += getChar();
//tc += getChar();
int tc1 = tc.toInt(); // convert it to an integer (Thanks Phill)

val = tc1; // reads the value of the data from FSX turn coordinator
val = map(val, 0, 256, 0, 179); // scale it to use it with the servo ( between 0-256 for FSX turn coordinator,between 0 and 180 for servo)
Serial.println (val); //added for troubleshooting

}
break;

case 'B':
//Do something
break;

case 'C':
//Do something
break;

//etc etc etc
}
}

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
}
-------------------------------------FINISH---------------------------------------

Davidino
Posts: 3
Joined: Fri Aug 01, 2014 6:49 pm

Re: Arduino Servo Control Flight Simulator

Post by Davidino » Sat Aug 02, 2014 6:57 am

Not working for me... Tha data arrives to the Arduino card, 'cause it flashes, but my servo won't move :cry:

admin
Site Admin
Posts: 21
Joined: Thu Feb 20, 2014 3:01 am

Re: Arduino Servo Control Flight Simulator

Post by admin » Mon Aug 11, 2014 9:00 pm

I don't see anything obvious sticking out but I don't know what the V identifier is off the top of my head either.

Did you make sure the servo can actually move with a basic servo sketch?

If you haven't you may want to spend some time with the individual components first- makes interfaces much easier if you know the hardware actually works.

Davidino
Posts: 3
Joined: Fri Aug 01, 2014 6:49 pm

Re: Arduino Servo Control Flight Simulator

Post by Davidino » Wed Aug 13, 2014 3:53 pm

Solved with this simple sketch...
Apparently, if you keep the Delay of servo to 0, it won't move, if you change the value to 25 or 50 (mseconds problably) it works perfectly, It works like a sort of data-gate. 0 means closed... I think :geek:

--------------------------------------------------------------------------start------------------------------------------------
// FSX WORKING - Davide F. 2014//
// Enjoy :)//

#include <Servo.h>
Servo autotServo;
int Data;
String AT;
int pos = 0; // variable to store the servo position



void setup() {
autotServo.attach(9); //attaching servo to pin 9
Serial.begin(115200);
Serial.flush();
Serial.println("V"); // so I can keep track of what is loaded
}

void loop() {
if (Serial.available() > 0) {
Data = Serial.read();
if (Data == 'V'){
delay (25); // KEEP THIS ABOVE 0
AT = "";
AT += char(Serial.read());
AT += char(Serial.read());
AT += char(Serial.read());
AT += char(Serial.read());
int ATInt = AT.toInt(); // convert it to an integer
ATInt = (ATInt + 0); // Manual correction to suit servo zero.
ATInt = map(ATInt, +0, +100, 50, 130);//map the integer
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
autotServo.write(ATInt);
}//end of found "V"
}//end of serial read
}//

---------------------------------------Successfully Finished :P -----------------------------------------

admin
Site Admin
Posts: 21
Joined: Thu Feb 20, 2014 3:01 am

Re: Arduino Servo Control Flight Simulator

Post by admin » Sat Aug 16, 2014 12:48 pm

Cool. Not sure why you would need that small delay there just before retrieving/assembling the value.

Did you try changing the update frequency in Link2FS faster or slower? Either way glad it worked.

Cheers

Post Reply