help with "error was not declared in this scope"

hi, i'm trying to implement a PID algorithm and i got into trouble while compiling, i'll leave you my code and the error i get below it:

void setup()
  {
   analogReference(DEFAULT);
   pinMode (3, OUTPUT);
   pinMode (5, OUTPUT);
   Serial.begin(9600);
  }

void loop()
  { 
   actuate(PID(calculateError(getSetpoint(), getFeedback());
  }
  
  
//setpoint data acquisition

float getSetpoint()
  {
   int setpointPin = A1;
   float setpoint = analogRead(setpointPin);
   return setpointPin;
  }


//feedback data acquisition

float getFeedback()
  {
   int feedbackPin = A5;
   float feedback = analogRead(sensePin);
   return feedback;

  }

//calculate error
float calculateError(float setpoint, float feedback)
  {
   float error;
   error = setpoint - feedback;
   return error;
  }
//PID function===>returns output
float PID(error)                
  {
   float pGain = 0;
   float IGain = 0;
   float DGain = 0;
   float output;               
   
   output = proportional(error) + integral(error) + differential(error); 
  }

  //proportional function
float proportional( float pError)
  {
   pOutput = pError * pGain;    //calculate de proportional part of the output
   return pOutput;   
  }
  
  //integral function
float integral(float Error)
  {
   float iState = 0;    //sum of the past errors
   float iMax = 0;      //max value of iState
   float iMin = 0;      //min value of iState
   
   iState += Error;    //update iState
   iState = constrain(iState, iMin, iMax);  //constrain iState to avoid windup
   
    iOutput = iState * iGain;    //calculate the integral output
    return iOutput;
  }
  
  //differential function
float differential(float Error)
  {
   float lastError;
   errorDifference = Error - lastError;  //error variation
   dOutput = dGain * errorDifference;    //calculate the differential output
   lastError = Error;       //save the current error to reuse it next time
   return dOutput;
   
  }

//actuate
void actuate(output)
  {
    output = constrain(output, 0, 1023);
    map(output, 0, 1023, 0, 255);
    analogWrite(3, output);
    analogWrite(5, output);
  }

'error' was not declared in this scope

control_PID:6: error: 'error' was not declared in this scope
control_PID:10: error: variable or field 'actuate' declared void
control_PID:10: error: 'output' was not declared in this scope
control_PID.cpp: In function 'void loop()':
control_PID:21: error: expected `)' before ';' token
control_PID.cpp: In function 'float getFeedback()':
control_PID:40: error: 'sensePin' was not declared in this scope
control_PID.cpp: At global scope:
control_PID:53: error: redefinition of 'float PID'
control_PID:6: error: 'float PID' previously defined here
control_PID:53: error: 'error' was not declared in this scope
control_PID.cpp: In function 'float proportional(float)':
control_PID:66: error: 'pOutput' was not declared in this scope
control_PID:66: error: 'pGain' was not declared in this scope
control_PID.cpp: In function 'float integral(float)':
control_PID:80: error: 'iOutput' was not declared in this scope
control_PID:80: error: 'iGain' was not declared in this scope
control_PID.cpp: In function 'float differential(float)':
control_PID:88: error: 'errorDifference' was not declared in this scope
control_PID:89: error: 'dOutput' was not declared in this scope
control_PID:89: error: 'dGain' was not declared in this scope
control_PID.cpp: At global scope:
control_PID:96: error: variable or field 'actuate' declared void
control_PID:96: error: 'output' was not declared in this scope

that's all, thanks in advance for your help

float getSetpoint()
  {
   int setpointPin = A1;
   float setpoint = analogRead(setpointPin);
   return setpointPin;
  }

Is that what you want?
Cos that's what you'll get.

float PID(error)

Type?

void actuate(output)

Type?

What a complete mess that code was.
Most of the errors were:-

  1. Not defining a variable that you use in a function. Should some of the variables be global. If a variable is defined in a function it is independent of any variable of the same name declared in another function.

  2. When defining a function you need to declare the variable type you are passing into it.

  3. Not enough closing braces in the only line in the loop() function - you needed an extra 3.

I have defined variables inside the functions that you have missed but you will probably have to define them where you need them with the value you need.

int sensePin = 0;
void setup()
  {
   analogReference(DEFAULT);
   pinMode (3, OUTPUT);
   pinMode (5, OUTPUT);
   Serial.begin(9600);
  }

void loop()
  { 
   actuate(PID(calculateError(getSetpoint(), getFeedback())));
  }
  
  
//setpoint data acquisition

float getSetpoint()
  {
   int setpointPin = A1;
   float setpoint = analogRead(setpointPin);
   return setpointPin;
  }


//feedback data acquisition

float getFeedback()
  {
   int feedbackPin = A5;
   float feedback = analogRead(sensePin);
   return feedback;

  }

//calculate error
float calculateError(float setpoint, float feedback)
  {
   float error;
   error = setpoint - feedback;
   return error;
  }
//PID function===>returns output
float PID(float error)                
  {
   float pGain = 0;
   float IGain = 0;
   float DGain = 0;
   float output;               
   
   output = proportional(error) + integral(error) + differential(error); 
  }

  //proportional function
float proportional( float pError)
  {
      float pGain = 0;
   float pOutput = pError * pGain;    //calculate de proportional part of the output
   return pOutput;   
  }
  
  //integral function
float integral(float Error)
  {
   float iState = 0;    //sum of the past errors
   float iMax = 0;      //max value of iState
   float iMin = 0;      //min value of iState
   
   iState += Error;    //update iState
   iState = constrain(iState, iMin, iMax);  //constrain iState to avoid windup
   float iGain = 0;
   float iOutput = iState * iGain;    //calculate the integral output
    return iOutput;
  }
  
  //differential function
float differential(float Error)
  {
   float lastError;
   float dGain = 0;
   float errorDifference = Error - lastError;  //error variation
   float dOutput = dGain * errorDifference;    //calculate the differential output
   lastError = Error;       //save the current error to reuse it next time
   return dOutput;
   
  }

//actuate
void actuate(float output)
  {
    output = constrain(output, 0, 1023);
    map(output, 0, 1023, 0, 255);
    analogWrite(3, output);
    analogWrite(5, output);
  }

thanks a lot for your help, i've got to admit that this is my first time with non-void functions, and also first time passing data to them. so i'm a complete newbie about it

and my idea is variating the setpoint with a pot so i can see how the system behaves

my idea is variating the setpoint with a pot so i can see how the system behaves

But if all you return is the identity of the pin the pot is attached to, the system is going to behave the same all the time!

return setpointPin;


Rob

oh hadn't noted it, anyway already solved it.
the code is already working and really well thanks a lot for all your help