Pages: [1]   Go Down
Author Topic: Code for displaying a result on my computer screen  (Read 286 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?


Code:
#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

« Last Edit: December 12, 2012, 08:53:37 am by AWOL » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 650
Posts: 50838
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The code I have for it runs fine but it fails to display the result on the computer when I run it.
Nothing displays in the Serial Monitor?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im fairly new to this programming thing so forgive me if I seem inexperianced. When i run the code with the circuit attached to the computer, the software compiles and thats it. Nothing else shows up. Is this somthing to do with the code or am I doing somthing else wrong?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 650
Posts: 50838
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is this somthing to do with the code or am I doing somthing else wrong?
Where are you expecting this output to appear? Are you opening the Serial Monitor?
Logged

Pages: [1]   Go Up
Jump to: