Cockpitbuilders.com

sign up .

August 13, 2020, 12:57:41 am

Login with username, password and session length
29 Guests, 1 User
Members
  • Total Members: 3961
  • Latest: avvial
Stats
  • Total Posts: 55573
  • Total Topics: 7418
  • Online Today: 36
  • Online Ever: 582
  • (January 22, 2020, 08:44:01 am)
Users Online
Users: 1
Guests: 29
Total: 30

COUNTDOWN TO WF2020


WORLDFLIGHT TEAM USA

Will Depart in...

Recent

Welcome

Arduino Code Help!

Started by Trevor Hale, November 11, 2014, 08:16:45 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Trevor Hale

November 11, 2014, 08:16:45 am Last Edit: November 11, 2014, 08:18:11 am by Trevor Hale
Hi guys,

Have the following code that compiles....  Wondering if someone can answer a quick question for me.

the line
Quoteuint8_t servonum = 0;          // servo (or analog device) is connected at position 0 on the PCA9685


would be my trim gauge connected at position 0

so if I have the Flap connected at Postion 1, it wont allow me to add the line. below it.

Quoteuint8_t servonum = 0;          // servo (or analog device) is connected at position 0 on the PCA9685
uint8_t servonum = 1;          // servo (or analog device) is connected at position 0 on the PCA9685


How can I tell it the flap is at position #1, and Trim at Pos #0


Quote
// ***********************BEGIN DECLARATIONS********************************************
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);  //Base address for PCA9685
uint8_t servonum = 0;          // servo (or analog device) is connected at position 0 on the PCA9685.
int trimFSX;
int old_trimFSX;
int trimPWM;
int flapFSX;
int old_flapFSX;
int flapPWM;

String data;
int const a=(41); //(replace 1 with your number for a)
int const b=(74); //(replace 1 with your number for b)
int const c=(41); //(replace 1 with your number for c)
int const d=(74); //(replace 1 with your number for d)

int CodeIn;
//**********************************************************************
// ***********************BEGIN VOID SETUP*******************************
void setup()
{

Wire.begin();               //Join the bus as master
Serial.begin(115200);
trimFSX=0; //Just to put an initial number for starters
flapFSX=0; //Just to put an initial number for starters
// --------Increase PWM refresh rate to 240Hz
// ---------------------------------------
pwm.begin();           //Starts communication with PCA9685
pwm.setPWMFreq(240);
}
//end void setup
//****************************************************************************
// **************BEGIN VOID LOOP********************************************************
void loop()
{
old_trimFSX=trimFSX;
old_flapFSX=flapFSX;


//**** Check for data from L2FSX ****
if (Serial.available() > 0) {  //if there is a charactor in the serial receive buffer then ,,,,
   CodeIn = getChar();// read it via the "char getChar" routine
   if (CodeIn == '<') {
     LESSTHAN();   }// The first identifier is "<" goto LESSTHAN void
}//end of serial.available
if (trimFSX!=old_trimFSX) {
  trimPWM=(a*(trimFSX) +b);
  pwm.setPWM(servonum, 0, trimPWM);
  old_trimFSX=trimFSX;
}//end of if
if (flapFSX!=old_flapFSX) {
  flapPWM=(c*(trimFSX) +d);
  pwm.setPWM(servonum, 1, flapPWM);
  old_flapFSX=flapFSX;
 
}//end of if
}//end void loop
//--------------- Subroutine getChar ------------------------------
char getChar()// The serial buffer routine to get a character
{
while(Serial.available() == 0);// wait for data
return((char)Serial.read());
}// end of getchar Routine.

//--------------- Subroutine LESSTHAN ------------------------------
void LESSTHAN() {//the first identifier was a '<'
CodeIn = getChar();//get another charactor from serial port
if (CodeIn == 'H'){//found the identifier "H"
   delay (11);    // It seems to need a delay here
   data = ""; // Empty data string
   data += char(Serial.read());    //Read the first data charactor sent
   data += char(Serial.read());   //Read the second data charactor sent and add it to the first
   data += char(Serial.read());   //Read the third data charactor sent and add it to the other two.)   
   trimFSX = data.toInt();
} //end of "found the identifier "H"
if (CodeIn == 'G'){//found the identifier "G"
   delay (11);    // It seems to need a delay here
   data = ""; // Empty data string
   data += char(Serial.read());    //Read the first data charactor sent
   data += char(Serial.read());   //Read the second data charactor sent and add it to the first
   data += char(Serial.read());   //Read the third data charactor sent and add it to the other two.)   
   flapFSX = data.toInt();
} //end of "found the identifier "G"
}  // end of LESSTHAN loop

// ************** END *********************************************************


Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

MLeavy737

November 11, 2014, 09:39:28 am #1 Last Edit: November 11, 2014, 09:48:09 am by MLeavy737
Hey Trevor :)

I don't know much about Arduino code but it looks like it uses the C programming language.  The one thing i can tell you about why you can't add that second line here..

uint8_t servonum = 0;          // servo (or analog device) is connected at position 0 on the PCA9685
uint8_t servonum = 1;          // servo (or analog device) is connected at position 0 on the PCA9685

that code is trying to redeclare the variable it just created which it won't let you do. If you just want to assign in the second line just take out the uint8_t. 

Like i said, not sure exactly how the code relates to Arduino but maybe that will at least compile now.

Mike L
The 737 800/900... Fastest airplane with the gear down!

Trevor Hale

November 11, 2014, 10:31:01 am #2 Last Edit: November 11, 2014, 10:32:34 am by Trevor Hale
Thats exactly what it is saying.

From the sample code I have seen (Only had the one Servo/PWM) I gather that just identifies the device on port 0
I am just assuming I will have to declare a device on port 1 as well, but maybe I don;t have to.

I should have the PWM card in a few days and I will see how it works, I was able to compile it by removing the servnum=1 altogether LOL.

And it is C...

Glad someone understands it LOL.  Thanks Bud

Trev
Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

KyleH

To be helpful, need to know what the definitions of this function are:
pwm.setPWM(servonum, 0, trimPWM);

does the servonum define that its a servo, or the port it is on? And what does the '0' mean?

I would think that the servonum variable defines the port the servo is connected to. That means that the second one for the flaps would need to be a different number.
My approach would be as follows:

QuoteAfter the include statements:

#define    TRIM_SERVO    0
#define    FLAPS_SERVO   1


Then in the function calls:

if (trimFSX!=old_trimFSX) {
  trimPWM=(a*(trimFSX) +b);
  pwm.setPWM(TRIM_SERVO, 0, trimPWM);
  old_trimFSX=trimFSX;
}//end of if

if (flapFSX!=old_flapFSX) {
  flapPWM=(c*(trimFSX) +d);
  pwm.setPWM(FLAPS_SERVO, 1, flapPWM);
  old_flapFSX=flapFSX



Now that is making some assumptions, so it could be completely wrong.


Kyle

Chief Pilot
Worldflight Team USA
http://www.worldflightusa.com

Trevor Hale

That is the way I understand it, except for the function "uint8_t" <-- Not really sure what this function is.

It is my understanding that

pwm.setPWM(servonum, 0, trimPWM); <-- The 0 in this is the port number and Servonum is the function and trimPWM is the value to set.I could be wrong though.

I am hoping to have the other card today and I can test and report back.

Thanks Guys.
Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

KyleH

I don't know the Ardunio compiler, but the unit8_t is a variable type declaration like int

I know for microchip int8 is an 8 bit integer, so my guess is it is an unsigned 8 bit  integer, and don't know what the _t means. Maybe a volatile one?

Kyle

Chief Pilot
Worldflight Team USA
http://www.worldflightusa.com

Trevor Hale

You Rock!  Thats probably exactly what it is,

With that being said, I likely could go...

uint8_t TRIM_SERVO = 0
uint8_t FLAP_SERVO = 1

perhaps... 

Thanks a bunch, I will check that out!
Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

KyleH

Do you have any documentation on what pwm.setPWM(servonum, 0, trimPWM);  function does, or even the function code itself?
Kyle

Chief Pilot
Worldflight Team USA
http://www.worldflightusa.com

Trevor Hale

I had a guy write this sample for me, and I added the Flap.. When I have it working I should be able to play with it.  From what I can figure out, that is where the PWM Value gets output to the analog gauge.

But now, I don;t have any documentation, I only have other examples, and those examples seem to be for servos not pwm voltage outputs LOL.

I will catch on and learn sooner or later...  And I will then be able to help people here.  I know for a Fact GeneB is good at this stuff and likely could explain things to us better.  LOL

Until then I will keep researching :)


Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

727737Nut

Quote from: KyleH on November 12, 2014, 04:19:17 am
Do you have any documentation on what pwm.setPWM(servonum, 0, trimPWM);  function does, or even the function code itself?

(servonum, 0, trimPWM)

servonum is the where you physically have it plugged in on the expansion card 0-15 are available

0 is the starting point, if using servo's it is ALWAYS 0  If using PWM it can vary depending on what you are trying to PWM, example, a couple of my gauges need 50 to start. 

trimPWM is the high value.  anywhere from your first setting up to 4095

Here is a sample I have using a servo
(1, 0, 3997) // that is saying servo hooked to position 1 goes from 0 (off) to 3997 (max)

Hope that makes sense.  I am using the Adafruit I2C 16ch 12bit servo/pwm card with a Teensy 3.1

Rob
737 Junkie

Trevor Hale

Ok that makes sense, therefore I will need to alter my code, (Beating my head against the wall now) LOL
Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

Trevor Hale

November 13, 2014, 04:14:37 am #11 Last Edit: November 13, 2014, 04:22:37 am by Trevor Hale
Think this should work..  Rob, Any thoughts?  Changed the servonum uint8_t to say flapgauge and trimgauge respectively.


Quote
// ***********************BEGIN DECLARATIONS********************************************
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);  //Base address for PCA9685
uint8_t trimgauge = 0;          // servo (or analog device) is connected at position 0 on the PCA9685.
uint8_t flapgauge = 1;          // servo (or analog device) is connected at position 1 on the PCA9685.

int trimFSX;
int old_trimFSX;
int trimPWM;
int flapFSX;
int old_flapFSX;
int flapPWM;

String data;
int const a=(1); //(replace 1 with your number for a)
int const b=(1); //(replace 1 with your number for b)
int const c=(1); //(replace 1 with your number for c)
int const d=(1); //(replace 1 with your number for d)

int CodeIn;
//**********************************************************************
// ***********************BEGIN VOID SETUP*******************************
void setup()
{

Wire.begin();               //Join the bus as master
Serial.begin(115200);
trimFSX=0; //Just to put an initial number for starters
flapFSX=0; //Just to put an initial number for starters
// --------Increase PWM refresh rate to 240Hz
// ---------------------------------------
pwm.begin();           //Starts communication with PCA9685
pwm.setPWMFreq(240);
}
//end void setup
//****************************************************************************
// **************BEGIN VOID LOOP********************************************************
void loop()
{
old_trimFSX=trimFSX;
old_flapFSX=flapFSX;


//**** Check for data from L2FSX ****
if (Serial.available() > 0) {  //if there is a charactor in the serial receive buffer then ,,,,
   CodeIn = getChar();// read it via the "char getChar" routine
   if (CodeIn == '<') {
     LESSTHAN();   }// The first identifier is "<" goto LESSTHAN void
}//end of serial.available
if (trimFSX!=old_trimFSX) {
  trimPWM=(a*(trimFSX) +b);
  pwm.setPWM(trimgauge, 0, trimPWM);
  old_trimFSX=trimFSX;
}//end of if
if (flapFSX!=old_flapFSX) {
  flapPWM=(c*(trimFSX) +d);
  pwm.setPWM(flapgauge, 0, flapPWM);
  old_flapFSX=flapFSX;
 
}//end of if
}//end void loop
//--------------- Subroutine getChar ------------------------------
char getChar()// The serial buffer routine to get a character
{
while(Serial.available() == 0);// wait for data
return((char)Serial.read());
}// end of getchar Routine.

//--------------- Subroutine LESSTHAN ------------------------------
void LESSTHAN() {//the first identifier was a '<'
CodeIn = getChar();//get another charactor from serial port
if (CodeIn == 'H'){//found the identifier "H"
   delay (11);    // It seems to need a delay here
   data = ""; // Empty data string
   data += char(Serial.read());    //Read the first data charactor sent
   data += char(Serial.read());   //Read the second data charactor sent and add it to the first
   data += char(Serial.read());   //Read the third data charactor sent and add it to the other two.)   
   trimFSX = data.toInt();
} //end of "found the identifier "H"
if (CodeIn == 'G'){//found the identifier "G"
   delay (11);    // It seems to need a delay here
   data = ""; // Empty data string
   data += char(Serial.read());    //Read the first data charactor sent
   data += char(Serial.read());   //Read the second data charactor sent and add it to the first
   data += char(Serial.read());   //Read the third data charactor sent and add it to the other two.)   
   flapFSX = data.toInt();
} //end of "found the identifier "G"
}  // end of LESSTHAN loop

// ************** END *********************************************************
Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

727737Nut

Not sure Trevor as I don't use FSX or link2fs   With the x-plane and Teensy you only need about 6 lines of code. :)
737 Junkie

Trevor Hale

Quote from: 727737Nut on November 13, 2014, 06:14:04 am
Not sure Trevor as I don't use FSX or link2fs   With the x-plane and Teensy you only need about 6 lines of code. :)


I agree, but the scenery for me just isn't there with x-plane LOL.
Ok, well thanks for the explanation of the codeset anyway, I think I am on the right track either way, I will know more if my pwm card ever shows up!

Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

Trevor Hale

December 10, 2014, 06:52:02 am #14 Last Edit: December 11, 2014, 05:11:47 am by Trevor Hale
Hi Guys,

Thought I would post my Code that works for my Arduino Trim gauge, and the code I am working on for my flap gauge.

Quote
// ***********************BEGIN DECLARATIONS********************************************
#include <wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);  //Base address for PCA9685

uint8_t trimgauge = 15;          // servo (or analog device) is connected at position 0 on the PCA9685.
uint8_t flapgauge = 1;

int trimFSX;
int old_trimFSX;
int trimPWM;
int flapFSX;
int old_flapFSX;
int flapPWM;

String data;
String flaps;

int const a=(100); //(replace 1 with your number for a)
int const b=(2000); //(replace 1 with your number for b)
int const c=(120);
int const d=(800);

int CodeIn;

//**********************************************************************
// ***********************BEGIN VOID SETUP*******************************

void setup()
{

Wire.begin();               //Join the bus as master
Serial.begin(115200);
trimFSX=0; //Just to put an initial number for starters
flapFSX=0;

// --------Increase PWM refresh rate to 240Hz
// ---------------------------------------
pwm.begin();           //Starts communication with PCA9685
pwm.setPWMFreq(240);
}
//end void setup

//****************************************************************************
// **************BEGIN VOID LOOP********************************************************

void loop()
{
old_trimFSX=trimFSX;
old_flapFSX=flapFSX;


//**** Check for data from L2FSX ****
if (Serial.available() > 0) {  //if there is a charactor in the serial receive buffer then ,,,,
   CodeIn = getChar();// read it via the "char getChar" routine
   if (CodeIn == '<') {
     LESSTHAN();   }// The first identifier is "<" goto LESSTHAN void
}//end of serial.available

if (trimFSX!=old_trimFSX) {
  trimPWM=(a*(trimFSX) +b);
  pwm.setPWM(trimgauge, 0, trimPWM);
  old_trimFSX=trimFSX;
}//end of if

if (flapFSX!=old_flapFSX) {
  flapPWM=((c*flapFSX)/10)+d;
  pwm.setPWM(flapgauge, 0, flapPWM);
old_flapFSX=flapFSX;
}//end of if

}//end void loop


//--------------- Subroutine getChar ------------------------------
char getChar()// The serial buffer routine to get a character
{
while(Serial.available() == 0);// wait for data
return((char)Serial.read());
}// end of getchar Routine.

//--------------- Subroutine LESSTHAN ------------------------------
void LESSTHAN() {//the first identifier was a '<'
CodeIn = getChar();//get another charactor from serial port

if (CodeIn == 'H'){//found the identifier "H" LINK2FS
   delay (11);    // It seems to need a delay here
   data = ""; // Empty data string
   data += char(Serial.read());    //Read the first data charactor sent
   data += char(Serial.read());   //Read the second data charactor sent and add it to the first
   data += char(Serial.read());   //Read the third data charactor sent and add it to the other two.)   
   trimFSX = data.toInt();
} //end of "found the identifier "H"


if (CodeIn == 'G'){ //found the identifier "G" LINK2FS
   delay (11);    // It seems to need a delay here
data = ""; // Empty data string
   data += char(Serial.read());    //Read the first data charactor sent
   data += char(Serial.read());   //Read the second data charactor sent and add it to the first
   data += char(Serial.read());   //Read the third data charactor sent and add it to the other two.)   
      flapFSX = data.toInt(); // convert it to an integer (Thanks Phill)
           
         
} //end of "found the identifier "G"
}  // end of LESSTHAN loop

// ************** END *********************************************************



From what I am told by a guy named Steve, (FROM ANOTHER WEBSITE: Reposted here for us:)

QuoteYou need to scale the output so that the pulse duration is within the servo's operating limits. Servos generally respond over the range of .7 to 2.1 ms. Anything higher or lower than these values will send the servo to the end of its travel, It's also possible to damage the servo by driving it hard against either end stop.

If you are running the 9685 at 240Hz, each PWM cycle is 1/240 second (=4.17ms). A 12bit resolution yields 4096 units, so each output PWM unit represents 4.17ms/4096 = 1.02┬Ás.

.7ms will need a PWM output of 686; 2.1ms will need a PWM output of 2058, so you generally will need to restrain your PWM to these limits. The 686 and 2058 are only approximate outputs, in practice you will most likely need to modify this range because:

    a) not all servos have the same stop points, and
    b) the device you are driving (gauge display) will not be able to utilize the full range
        of servo motion available.

The best way to determine what your actual limits are is to start with a fixed output in the middle of the range, around PWM=1400 and then slowly increase or decrease the PWM signal until you get to 0% or 100% of gauge scale. You now know what the minimum and maximum PWM range is for your specific gauge.

Now, all you need to do is to scale the fsx output to this range and your gauge will work.
Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

Trevor Hale

December 11, 2014, 05:02:14 am #15 Last Edit: December 11, 2014, 05:07:47 am by Trevor Hale
All Works Perfectly, except the servo goes backwards... 

Changing the flapFSX=0; to

flapFSX=(100-flapFSX);

Trev

Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

Like the Website ?
Support Cockpitbuilders.com and Click Below to Donate