Adding ON/OFF control & temp display to PID SCRIPT

Hey all! I am currently playing with some of the PID scripts and my seeeduino.

I’ve got it hooked up to two things, a heater to heat to the set temp and a thermocouple to take the readings and adjust accordingly.

My questions are about the script…

I’ve got the following script uploaded to my board:

/********************************************************

  • PID Simple Example
  • Reading analog input 0 to control analog PWM output 3
    ********************************************************/

#include <PID_Beta6.h>

//Define Variables we’ll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 2,5,1);

void setup()
{
//initialize the variables we’re linked to
Input = analogRead(0);
Setpoint = 150;

//turn the PID on
myPID.SetMode(AUTO);
myPID.SetSampleTime(250);
myPID.SetTunings(2,3,1);

}

void loop()
{
Input = analogRead(0);
myPID.Compute();
analogWrite(3,Output);
}

Now, when the board is turned on, it starts heating to the setpoint and does a magnificent job of keeping it there.

What I am trying to achieve is adding a on/off button to trigger the board to start heating the element ONLY when the signal from the button is received Also, I would like to add two buttons that adjust the temperature UP or DOWN, and a LCD readout of the Setpoint, and the ACTUAL temperature VIA a single serial wire.

Can anyone point me to the direction where I might find an example of this? Or simply throw the code down in a post? I would imagine the first two things I am trying to achieve are relatively simple in terms of coding. The simple on/off thing and temp up/down in response to the buttons.

Thanks in advance for all your responses!

the on/off switch and the up and down temp buttons seems pretty easy as they are basically the same things. wire each button to a power lane and a digital input on the arduino and read the value in the loop. if its high, or 5v (if you hooked it to the 5v) then you can do whatever that button is assigned to do, like the actual code that is currently in your loop, or change the Setpoint either up or down one depending on what button you push.

as for the lcd read out, it seems like it would depend a little bit on the display you have. some have libraries already in place to output information, its just a matter of telling it what to send.

Okay… So i’ve got 3 SPST pushbutton switches (Normally Open) hooked up to +5v and the outputs of each going to digital pins 4— (Trigger or operation button) 5—(Increase temp by 1 degree) and 6----(Decrease temp)

As far as the On/off operation goes…

void setup()
{
//initialize the variables we’re linked to
Input = analogRead(0);
Input = digitalRead(4);
Input = digitalRead(5);
Input = digitalRead(6);
Setpoint = 150;

//turn the PID on
myPID.SetMode(AUTO);
myPID.SetSampleTime(250);
myPID.SetTunings(2,3,1);
myPID.SetOutputLimits(0, 178.5);

}

void loop()
{
Input = analogRead(0);
myPID.Compute();

if(digitalRead(4),HIGH);
analogWrite(3,Output);
}else{
I dont know what to put here??
}

Am I getting close?

it would seem so. does that not work?

you only need it to run code if its on, so you dont really need the else on there.
also, you could move the

Input = analogRead(0); myPID.Compute();
in there as well since thats the on and off.

the temp up and down buttons will be similar. but you will have to keep an eye on the previous state of them so that it doesnt just keep adding up for every cycle that the switch is thrown.

for those, add a boolean for each button and check the state each time. if the state changes from whatever the last state was, then you can adjust the other variables accordingly.

doing it this way should be able to handle having both switches thrown (which would effectively equal out and have no change) without effecting the actual temp too much.

You could also try using interrupts, though the seeeduino will only support 2.

I guess all I’d need is one… to interrupt while the button is in the OFF position.

Okay… So I have done a bit of tinkering and moving the code around. I’ve taken small portions of code from the “button library” to see if I can get this going…

It complied Okay in the arduino program, but I am wondering if anyone see’s anything that is wrong right off the bat.

I’ve put my goals to hook up the LCD untill later when I get the buttons working and doing what I want.

I think I have the on/off thing going… I just need someone to give me some pointers on how to make “buttonPin2” and “buttonPin3” to increase and decrease the setpoint by 1 degree.

Here’s what I’ve got so far.

/********************************************************

  • PID Simple Example
  • Reading analog input 0 to control analog PWM output 3
    ********************************************************/

#include <PID_Beta6.h>

//Define Variables we’ll be connecting to
double Setpoint, Input, Output;

const int buttonPin1 = 1; // the number of the RUNpushbutton pin
const int buttonPin2 = 2; // the number of the UPpushbutton pin
const int buttonPin3 = 3; // the number of the DOWNpushbutton pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 2,5,1);

void setup()
{

// initialize the pushbuttons as inputs:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);

//initialize the variables we’re linked to
Input = analogRead(0);
Setpoint = 100;

//set initial tunings

myPID.SetSampleTime(250);
myPID.SetTunings(2,3,1);
myPID.SetOutputLimits(0, 220);

}

void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin1);

// check if the RUNpushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// Perform action:
Input = analogRead(0);
myPID.Compute();
analogWrite(3,Output);
}
else {
// Turn PID OFF
myPID.SetMode(MANUAL);
}

}

How does that look so far?

sorry, I had to move the topic, it was bugging me that it was in the open source projects area.

as far as other buttons go, you should have a button state for each button so you know what state it was last cycle. since it will be near impossible to only have it pressed for one cycle without stopping the process.

just read the states and then do the actions accordingly. here is some code i have in my rainbowduino. its for 2 buttons that control the scroll speed.

void CheckAndChangeSpeed() { int tmp = digitalRead(upPin); if(tmp != prevUpState) { prevUpState = tmp; if(tmp == HIGH) { scrollSpeed+=1; Serial.print("up "); Serial.println(scrollSpeed); } } tmp = digitalRead(downPin); if(tmp != prevDownState) { prevDownState = tmp; if(tmp == HIGH) { scrollSpeed-=1; if(scrollSpeed<0)scrollSpeed = 0; Serial.print("down "); Serial.println(scrollSpeed); } } }