Pid Arduino
Pid Arduino
Pid Arduino
//LiquidCrystal lcd(12, 11, 13, 10, 9, 8);//trocar o 5 por 13; o 4 por 10; o 3 por
9; o 2 por 8
antigo LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
DDRD= B11111111;//pinos para mostra a forma de onda no osciloscopio
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("______PID_______");
lcd.setCursor(0, 1);
lcd.println("controle de temp.");
delay(1);
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Equipe:_________");
lcd.setCursor(0, 1);
lcd.println("Alan;Erick;James");
delay(1);
float error = 0;
float error_percentual = 0;
float temperature;
float lastTemperature;
double
kP = 1.0,
kI = 0.0,
kD = 0.0;
double P = 0, I = 0, D = 0;
double PID;
float setPoint = 25;
int controlePwm = 0;
long lastProcess = 0;
float saida_sistema;
void loop() {
// put your main code here, to run repeatedly:
int rawTemp = analogRead(pSENSOR);
double temperature = map(rawTemp,0,1023,0,100);
//implementa��o PID
error = setPoint - temperature;
error_percentual=((setPoint-temperature)/setPoint)*100;
float deltaTime = (millis() - lastProcess)/1000.0;
lastProcess = millis();
//P
P = error*kP;
//I
I = I + error*kI;
//D
D = (lastTemperature - temperature)*kD;
// soma tudo
PID = P + I + D;
//saida do controle
analogWrite(pCONTROLE, controlePwm);//said\ pelo pino digital 5
analogWrite(A5, temperature);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("e:");
lcd.print(error);
/*
lcd.setCursor(12, 0);
lcd.print("%");
*/
lcd.setCursor(0, 1);
lcd.print("T:");
lcd.print(setPoint);
lcd.setCursor(8, 0);
lcd.print("Ts:");
lcd.print(temperature);
/*
Serial.println("Controle PWM:");
Serial.println(controlePwm);
Serial.println();
Serial.println("Temperatura do Sensor:");
Serial.println(temperature);
Serial.println();
*/
//saida_sistema=temperature - error;
/*
if(temperature>setPoint){while(temperature>setPoint)
{ saida_sistema=temperature+error;delay(10);Serial.println(saida_sistema);}}
else if(temperature<setPoint){saida_sistema=temperature+error;delay(10);}
else if(temperature==setPoint){saida_sistema=setPoint;delay(10);}
*/
//if(error>0){while (error>0)
{saida_sistema=temperature+error;Serial.println(saida_sistema);}}
if(error>0){saida_sistema=error + temperature;}
else if(error<0){saida_sistema=temperature + error;}
else if(error==0){saida_sistema=temperature + error;}
//Gs=exp(polo*tempo)
//float reset=0;
//Serial.println(reset);
for(int d=0;d<101;d++){
Serial.println("Temperatura Corrigida:");
float e=d;
float polo=-1;
saida_sistema=+setPoint-setPoint*((PID)*(exp(polo*d)/(1+PID*exp(polo*d))));
PORTD=auxx;
//Serial.println(d);
Serial.println(saida_sistema);
//Serial.println(((1))*((PID)*(1/(1+PID*1))));
lcd.setCursor(7, 1);
lcd.print(";");
lcd.print(saida_sistema,5);//o 5 � referente a quantidade de casas decimais
delay(1);
if(saida_sistema>setPoint){
analogWrite(A2, 1023);
analogWrite(A3, 0);
analogWrite(A4, 0);
}
else if(saida_sistema<setPoint){
analogWrite(A4, 1023);
analogWrite(A2, 0);
analogWrite(A3, 0);
else if(saida_sistema==setPoint){
analogWrite(A3, 1023);
analogWrite(A2, 0);
analogWrite(A4, 0);
}
/*
}
float b;
//for(int a=0;a<101;a++){b=a;Serial.println((exp((b-100)/100))*(saida_sistema*(1-
error_percentual)));}
//obs: imcrementar uma exp; valor_desejado*exp((x-100)/100), onde x varia de 0 ao
infinito;
//saida_sistema=temperature - error;
/*
int tempo=2000;
for(int c=0;c<tempo;c++){
Serial.println("Temperatura Corrigida:");
Serial.println(saida_sistema*(1-error_percentual));
Serial.println();
delay(1);
}
*/
lcd.setCursor(0, 1);
//lcd.print(millis() / 1000);
if(error<0){
analogWrite(A2, 1023);
analogWrite(A3, 0);
analogWrite(A4, 0);
}
else if(error>0){
analogWrite(A4, 1023);
analogWrite(A2, 0);
analogWrite(A3, 0);
else if(error==0){
analogWrite(A3, 1023);
analogWrite(A2, 0);
analogWrite(A4, 0);
//controlePwm=0;
/*while(error>0){saida_sistema=temperature - (error);}
while(error<0){saida_sistema=temperature + (error);}
saida_sistema=temperature;
*/
// *
**************************************_PROGRAMA_PARA_O_LCD_************************
***********************
/*
LiquidCrystal Library - Hello World
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
/*
// include the library code:
#include <LiquidCrystal.h>
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
}
*/