Hello all,
This is my first post on any forum, ever. Please excuse any ignorance on my part!
I've been programming Arduino boards for several years now, and am finally getting past the beginner "if blank, then digitalWrite HIGH stuff.
I'm building a scaled down model of a motion flight simulator platform to get it working before I start the big project of the full scale version. I'm going to be using Microsoft flight simulator FSX and sending data to my Arduino board using Link2fs (jimspage.co.nz). Using an MPU6050 gyro and accelerometer to measure the difference between the sim's angle, and FSX pitch and roll data.
My problem is this:
The aircraft pitch data is sent to my Arduino via serial, and sent in this format: <Q-002.6
the first two letters identify what kind of data (in this case, pitch (<Q)) followed by the neg or pos pitch angle in degrees. I've tried to change the incoming string to an int by using the toInt() function which works great! Only problem is that I can't get the expected values to print to the serial monitor screen.
For example, the aircraft is pitched down 2 degrees in FSX, Link2fs is sending my Arduino the string <Q+002.0 (Positive is down). I'm unable to get the Arduino to distinguish the positive or the negative angles I'm seeing on the serial monitor. (I'd like to invert them so positive is up). I'm ALWAYS getting positive numbers when the aircraft is pitched up or down. Here's the code I'm trying (an example I've changed that came with the Link2fs program to do what I want):
/*
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.
This sets the complete Arduino card for "keys" input except pin 13.
Attach a switch to any pin (except 13) and program that pin in "Multi"
Pin 13 is used for the servo to indicate flaps.
For this servo code to work you must tick "<G" (Flap position) in Multi.
Everything to do with "Keys" starts with a "K" in this code.
*/
int CodeIn;// used on all serial reads
int KpinNo;
int Koutpin;
String flaps;
String pitch;
String pitchPosOrNeg = "";
String rollPosOrNeg = "";
bool pitchUp = false;
bool rollRight = false;
String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;
void setup()
{
Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
for (int KoutPin = 2; KoutPin < 70; KoutPin++)// Get all the pins ready for "Keys"
{
pinMode(KoutPin, INPUT);
digitalWrite(KoutPin, HIGH);
}
Serial.begin(115200);
pinMode(13, OUTPUT);// For the servo.
}
void loop() {
{KEYS();} //Check the "keys" section
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;
}
}
void LESSTHAN(){ // The first identifier was "<"
CodeIn = getChar(); // Get another character
switch(CodeIn) {// Now lets find what to do with it
case 'Q'://Found the second identifier
pitch = "";
pitchPosOrNeg = "";
pitchPosOrNeg += getChar();
if(pitchPosOrNeg = "+"){
pitchUp = false;
}
else{
pitchUp = true;
}
pitch += getChar();
pitch += getChar();
pitch += getChar();
getChar();
getChar();
int pitchDeg;
pitchDeg = pitch.toInt();
Serial.print("Pitch: ");
if(pitchUp = false){
pitchDeg *= -1;
Serial.println(pitchDeg);
}
else{
Serial.println(pitchDeg);
}
break;
case 'B':
//Do something
break;
case 'G'://Found the second identifier ("G" Flaps position)
break;
}
}
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;
}
}
void SLASH(){ // The first identifier was "/" (Annunciator)
//Do something
}
void KEYS()
{
Kstringnewstate = "";
for (int KpinNo = 2; KpinNo < 70; KpinNo++){
KpinStateSTR = String(digitalRead(KpinNo));
KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 2));
if (KpinStateSTR != KoldpinStateSTR)
{
if (KpinNo != 13){
//Serial.print ("D");
//if (KpinNo < 10) Serial.print ("0");
//Serial.print (KpinNo);
//Serial.println (KpinStateSTR);
}
}
Kstringnewstate += KpinStateSTR;
}
Kstringoldstate = Kstringnewstate;
}
I've written the "Void lessthan, case Q" section.
Any help would be great! I tried setting a bool operator to determine if it's up or down, but I'm scratching my head.
Thanks in advance!