Using arduino's PID library in a class....

Discussion in 'Programmer's Corner' started by Gibson486, Mar 12, 2018.

  1. Gibson486

    Thread Starter Active Member

    Jul 20, 2012
    264
    17
    I am trying to use Arduino's PID library in a class I made. Does this look it would work? Every time I look this up, they say to extend and use a virtual class, but that seems more complex than it needs to be.

    Would this not work?

    Code (Text):
    1. class Control{
    2.    
    3.     private:
    4.    
    5.      //private variables
    6.    
    7.     public:
    8.     Control()
    9.     {
    10.         double Setpoint, Input, Output;
    11.        
    12.         PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
    13.     }
    14.  
    15.    //rest of my public functions to access private variables
    16. };
     
  2. Gibson486

    Thread Starter Active Member

    Jul 20, 2012
    264
    17
    Got it to compile. Fingers crossed it will work as intended when I upload it!

    Code (Text):
    1. class Control{
    2.  
    3.     private:
    4.  
    5.      //private variables
    6.      PID* myPID;
    7.  
    8.     public:
    9.     Control()
    10.     {
    11.         //all private variables defined to something
    12.      
    13.         myPID = new PID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
    14.     }
    15.    //rest of my public functions to access private variables
    16. };
     
Loading...