I am working on a simple "leaky capacitor" project for school. The code I have for it runs fine but it fails to display the result on the computer when I run it. The code I am using is at this website: http://abe-bhaleraolab.age.uiuc.edu/blog/2012/mar/08/pid-control-using-arduino/
Can anyone help me output this result?
#include <PID_v1.h>
*/
// Layout
const int tankLevelSensor = 0; // Must be AIN
const int tankFillerOutput = 3; // must be PWM
//Variables
double setPoint=204;// 204 approximately == 1V on a 5V scale
double tankLevel, tankFill;
// setPoint - the value we want our level to be.
// tankLevel - the 10 bit value of our actual tank level
// tankFill - the inflow level into the tank.
// Tuning parameters
const double Kp=10;
const double Ki=0;
const double Kd=0;
//Load a new PID controller
PID myPID(&tankLevel, &tankFill, &setPoint, Kp, Ki, Kd, DIRECT);
const long sampleRate = 15;
// How frequently to run the PID control?
// Communication setup
const long serialPing = 500;
// Serial pingback interval in milliseconds
unsigned long now = 0;
// placehodler for current timestamp
unsigned long lastMessage = 0;
// last message timestamp.
void setup()
{
Serial.begin(9600);
// Open a communication channel from the Arduino
// back to the computer over USB.
//initialize the variables we're linked to
tankLevel = analogRead(tankLevelSensor);
//turn the PID on
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(sampleRate);
Serial.println("Begin"); // Hello World!
lastMessage = millis(); // timestamp
}
void loop()
{
tankLevel = analogRead(tankLevelSensor);
myPID.Compute(); // this function updates the control action.
// It reads tankLevel, and stores the output in tankFill
analogWrite(tankFillerOutput, tankFill);
now = millis();
if(now - lastMessage > serialPing) {
// this should execute less frequently
// send a message back to the mother ship
Serial.print("Setpoint = ");
Serial.print(setPoint);
Serial.print(" Tank Level = ");
Serial.print(tankLevel);
Serial.print(" Fill Rate = ");
Serial.print(tankFill);
Serial.print("\n");
lastMessage = now;
//update the time stamp.
}
}
Moderator edit: CODE TAGS