2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: PID_basic.ino
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// VERSION: 0.1.0
|
|
|
|
// PURPOSE: demo
|
|
|
|
// DATE: 2020-12-15
|
|
|
|
// (c) : MIT
|
|
|
|
//
|
|
|
|
|
|
|
|
// connect an LED to the PWM pin
|
2021-11-12 07:52:09 -05:00
|
|
|
// connect a potentiometer to A0
|
2021-01-29 06:31:58 -05:00
|
|
|
// play :)
|
|
|
|
|
2021-05-28 07:41:04 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#include "PID_RT.h"
|
|
|
|
|
|
|
|
PID_RT PID;
|
|
|
|
|
|
|
|
const int PWM_PIN = 3; // UNO PWM pin
|
|
|
|
|
|
|
|
int op = 0;;
|
|
|
|
float input = 0;
|
|
|
|
|
2021-05-28 07:41:04 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Serial.begin(115200);
|
|
|
|
Serial.println(__FILE__);
|
|
|
|
|
|
|
|
PID.setPoint(125);
|
|
|
|
PID.setOutputRange(0, 255); // PWM range
|
|
|
|
PID.setInterval(50);
|
|
|
|
PID.setK(2, 5, 1);
|
|
|
|
PID.start();
|
|
|
|
|
|
|
|
op = analogRead(A0);
|
|
|
|
}
|
|
|
|
|
2021-05-28 07:41:04 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
input = analogRead(A0);
|
|
|
|
if (PID.compute(input))
|
|
|
|
{
|
|
|
|
op = PID.getOutput();
|
|
|
|
analogWrite(PWM_PIN, op);
|
|
|
|
|
|
|
|
Serial.print(PID.getInput());
|
|
|
|
Serial.print('\t');
|
|
|
|
Serial.println(op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 07:41:04 -04:00
|
|
|
|
|
|
|
// -- END OF FILE --
|