Κώδικας:
// include the library code:
#include <LiquidCrystal.h>
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <PID_v1.h>
//#define LCD_I2C_ADDRESS 0x27
#define ROWS 2
#define COLUMNS 16
#define DELAY_MEASURE 2
#define STANDBY_TEMP_C 150
#define ADC_TO_TEMP_GAIN 0.415//1.8//0.99//2.50//0.53 //0.415
#define ADC_TO_TEMP_OFFSET_C 25.0
#define MAX_PWM_LOW 50//180
#define MAX_PWM_HI 255//210//240
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//PID parameters
double Setpoint, Input, Output;
double aggKp=8.00, aggKi=0.10, aggKd=4.00;
double consKp=4.00, consKi=0.05, consKd=2.00;
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//LiquidCrystal_I2C lcd(LCD_I2C_ADDRESS, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#define TEMPin A0
int actual_temperature, will_temperarture = STANDBY_TEMP_C;
const int numReadings = 30;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
float adcValue;
int pwm = 0; //pwm Out Val 0.. 255
int will_temp_tmp;
int MAX_PWM;
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
const int btnMode = 6; // the number of the pushbutton pin
const int btnUp = 7; // the number of the pushbutton pin
const int btnDown = 8; // the number of the pushbutton pin
const int PWMpin = 10; // the number of the PWM pin
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void setup()
{
lcd.begin(COLUMNS, ROWS); // set up the LCD's number of columns and rows:
pinMode(TEMPin, INPUT);
digitalWrite(TEMPin, LOW);
pinMode(PWMpin, OUTPUT);
pinMode(btnMode, INPUT_PULLUP);
pinMode(btnUp, INPUT_PULLUP);
pinMode(btnDown, INPUT_PULLUP);
Serial.begin(9600);
Setpoint = will_temperarture;
myPID.SetMode(AUTOMATIC);
splashScreen();
RefreshDisplay() ;
}
//===================================================================
void RefreshDisplay()
{
lcd.setCursor(0,0);
lcd.print("Set:");
lcd.setCursor(0,1);
lcd.print("Temp:");
lcd.setCursor(9,0);
lcd.print("PWM:");
}
//===================================================================
void SerialSend()
{
/*
Serial.print(fTime,3);
Serial.print(", ");
Serial.print(fTSet,1);
Serial.print(", ");
Serial.print(fTemp,1);
Serial.print(", ");
Serial.print(fEk,1);
Serial.print(", ");
Serial.print(fVoltage,3);
Serial.print(", ");
Serial.print(fTimeSampling,4);
Serial.print(", ");
Serial.print(fEk,3);
Serial.print(", ");
Serial.print(fIntegralTerm,3);
Serial.print(", ");
Serial.print(fPWM,2);
Serial.print(", ");
Serial.println(fIntegralTerm,2);
*/
}
//===================================================================
void Buttons()
{
if ((digitalRead(btnUp) == HIGH) && (digitalRead(btnDown) ==LOW ))
{
will_temperarture = will_temperarture + 5.0 ;
if (will_temperarture > 380.0)
{
will_temperarture = 380.0 ;
}
}
if ((digitalRead(btnUp) == LOW) && (digitalRead(btnDown) ==HIGH )) {
will_temperarture = will_temperarture - 5.0 ;
if (will_temperarture < 0.0)
{
will_temperarture = 0.0 ;
}
}
delay(200);
}
//===================================================================
void loop()
{
Buttons(); //read the buttons
Input = getTemperatureCelsius();
Setpoint = will_temperarture;
double gap = abs(Setpoint-Input);
if(gap < 10)
{
myPID.SetTunings(consKp, consKi, consKd);
}
else
{
myPID.SetTunings(aggKp, aggKi, aggKd);
}
myPID.Compute();
will_temp_tmp = will_temperarture;
actual_temperature = getTemperatureCelsius();
pwm = Output;
MAX_PWM = actual_temperature <= STANDBY_TEMP_C ? MAX_PWM_LOW : MAX_PWM_HI;
pwm = pwm > MAX_PWM ? pwm = MAX_PWM : pwm < 0 ? pwm = 0 : pwm;
analogWrite(PWMpin, pwm);
writeHEATING(will_temperarture, Input, pwm);
RefreshDisplay() ;
}
//===================================================================
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
int getTemperatureCelsius()
{
analogWrite(PWMpin, 0); //switch off heater
delay(DELAY_MEASURE); //wait for some time (to get low pass filter in steady state)
total = total - readings[readIndex];
readings[readIndex] = analogRead(TEMPin);
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings)
{
readIndex = 0;
}
adcValue = total / numReadings;
analogWrite(PWMpin, pwm); //switch heater back to last value
return round(((float) adcValue)*ADC_TO_TEMP_GAIN+ADC_TO_TEMP_OFFSET_C); //apply linear conversion to actual temperature
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void writeHEATING(int tempWILL, int tempVAL, int pwmVAL)
{
static int d_tempWILL = 1;//2
static int tempWILL_OLD = 1;//10
static int tempVAL_OLD = 1;//10
static int pwmVAL_OLD = 1;//10
pwmVAL = map(pwmVAL, 0, 255, 0, 99);
if (tempVAL_OLD != tempVAL)
{
lcd.setCursor(5,1);
if ((tempVAL_OLD/100) != (tempVAL/100))
{
lcd.print(tempVAL_OLD/100);
lcd.write(223);
}
else
lcd.print(" ");
if (((tempVAL_OLD/10)%10) != ((tempVAL/10)%10))
{
lcd.print((tempVAL_OLD/10)%10);
lcd.write(223);
}
else
lcd.print(" ");
if ((tempVAL_OLD%10) != (tempVAL%10))
{
lcd.print(tempVAL_OLD%10 );
lcd.write(223);
}
lcd.setCursor(5,1);
if (tempVAL < 100)
lcd.print(" ");
if (tempVAL <10)
lcd.print(" ");
lcd.print(tempVAL);
lcd.write(223);
lcd.print(" ");
tempVAL_OLD = tempVAL;
}
if ((tempWILL_OLD+d_tempWILL < tempWILL) || (tempWILL_OLD-d_tempWILL > tempWILL))
{
lcd.setCursor(4,0);
if ((tempWILL_OLD/100) != (tempWILL/100))
{
lcd.print(tempWILL_OLD/100);
lcd.write(223);
}
else
lcd.print(" ");
if (((tempWILL_OLD/10)%10) != ((tempWILL/10)%10))
{
lcd.print((tempWILL_OLD/10)%10 );
lcd.write(223);
}
else
lcd.print(" ");
if ((tempWILL_OLD%10) != (tempWILL%10))
{
lcd.print(tempWILL_OLD%10 );
lcd.write(223);
lcd.print(" ");
}
lcd.setCursor(4,0);
if (tempWILL < 100)
lcd.print(" ");
if (tempWILL <10)
lcd.print(" ");
lcd.print(tempWILL);
lcd.write(223);
lcd.print(" ");
tempWILL_OLD = tempWILL;
}
if (pwmVAL_OLD != pwmVAL)
{
lcd.setCursor(12,0);
if ((pwmVAL_OLD/100) != (pwmVAL/100))
{
lcd.print(pwmVAL_OLD/100);
}
else
lcd.print(" ");
if (((pwmVAL_OLD/10)%10) != ((pwmVAL/10)%10))
lcd.print((pwmVAL_OLD/10)%10 );
else
lcd.print(" ");
if ((pwmVAL_OLD%10) != (pwmVAL%10))
lcd.print(pwmVAL_OLD%10 );
lcd.setCursor(12,0);
if (pwmVAL < 100)
lcd.print(" ");
if (pwmVAL <10)
lcd.print(" ");
lcd.print(pwmVAL);
lcd.print("%");
pwmVAL_OLD = pwmVAL;
}
}
//===================================================================
void splashScreen()
{
lcd.setCursor(0,0);
lcd.print(F(" T. MAROUFIDIS "));
lcd.setCursor(0,1);
lcd.print(F("SOLDERING MODULE"));
delay(5000);
lcd.clear();
}
Πριν γίνει αυτό, βάλε τις βιβλιοθήκες που είναι επισυναπτόμενες, στο φάκελο libraries του περιβάλλοντος Arduino που έχεις.