#ifndef McKenzie_h
#define McKenzie_h

#include "Arduino.h"


class PIDd {

  /*  **************************************
 *         Dependent PID Loop Routine
 *  **************************************
 * 
 */
 
  public:
    PIDd (float Kp, float Ti, float Td, float t, float PVmin, float PVmax, float CVmin, float CVmax); 
    float Kp;
    float Ti;
    float Td;
    float t;
    float PVmin;
    float PVmax;
    float CVmin;
    float CVmax;
    float Error;
    void Reset();
    float Output (float Error);
    float CO; //Controller Output (0 - 100)
    float CV; //Scaled Controller Output (CVmin - CVmax)
  
  private:
    float PVspan; //Total Process Value Span
    float En; //Current Error in fractional % of span (0-1)
    float Pterm; //Proportional Term
    float Iterm; //Integral Term
    float Dterm; //Derivative Term
    bool antiWind; //Anti-windup is active
    unsigned long Tc; //Current Time (microsenconds since program start)
    unsigned long Tn1; //Time from last call (microsenconds since program start)
    float deltaT; //Change in time since last scan (seconds)
    float En1; //E(n-1) Previous Error in %span
    float En2; //E(n-2) Error from 2 loop updates prior in %span
    float deltaE; //Change in Errror
    float COn1; //Previous Controller Output in %span
    int scancount; //Number of iterations to hold derivative term until there are more than 2

};

class Timer_rlm {
  // Class to create and control timers to be used instead of the delay() function

  public:
    Timer_rlm (float preset);
    void start();
    void stop();
    //void restart();
    void reset();
    void check();
    long acc; //accumulated time value (in milliseconds)
    bool dn; //timer has reached preset timer value
    bool en; //timer is enabled
    bool tt; //timer is running
    long preset; //timer setting.  When timer acc reaches preset the timer stops timing and done bit is set

  private:
    long tc; //Current time value (in microseconds since the program started)
    long t0; //Starting time value
    long tn1; //Last update time value (in microseconds since the program started)

};

/*
class TargetDetection {
    
    *******************************
    * Infra Red Target Detection  *
    *******************************
  

  public:
    TargetDetection (int inPin, int hitLevel);
    bool check();
    bool pulseDetect;

  private:
    int targetLevel;
    int lastTargetLevel;
    long onTimes[400];
    long offTimes[400];
    int onLevel;
    long onInterval;
    long offInterval;
    int detectIndex=0;
    int detectCount;
    Timer detectReset(1000);
    Timer detectTime(1000);
    bool oneShot1;
    bool oneShot2;

};
*/

#endif
