Go Down

Topic: Code for displaying a result on my computer screen (Read 354 times) previous topic - next topic

erich590

Dec 12, 2012, 02:50 pm Last Edit: Dec 12, 2012, 02:53 pm by AWOL Reason: 1
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: [Select]
#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


PaulS

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?

erich590

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?

PaulS

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?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy