Is this soldering station code efficent and if something should be changed?
#include "Arduino.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <max6675.h>
#include <PID_v1.h>
// PID declaration
#define P_PID 40
// 40
#define I_PID 2
// 2
#define D_PID 25
// 25
// pin declaration
int buttonPin = 8;
int PIDPin = 5; // PWM?
int ktcSO = 10;
int ktcCS = 11;
int ktcCLK = 9;
int potPin = A0;
int powerPin = A1; //??
int heaterLed = 6;
int buzzPin = 5;
// button service
boolean stat = false;
boolean buttonState;
boolean lastButtonState;
unsigned long time_passed;
unsigned long button_time;
unsigned long last_button_time;
int button_wait = 50;
// temp set and readout
int read_value = 0;
int set_value = 0;
int voltage = 0;
String voltage_lcd_read_value; //??
String voltage_lcd_set_value; //??
String voltage_lcd; //??
String pwr_lcd;
int temp_max = 500;
unsigned long voltageMillis = 0;
unsigned long previousMillis = 0;
unsigned long nowMillis = 0;
int pause_time = 300;
// voltage measurement mode
int voltage_measurement = 0; //??
float voltage_read_value = 0; //??
float voltanie = 0; //??
//LCD special characters
////////////////////////
byte energy[8] = //??
{ //??
0b00010, 0b00100, 0b01000, 0b11111, 0b00010, 0b00100, 0b01000, 0b00000 //??
}; //??
byte temp[8] =
{
0b00100, 0b01010, 0b01010, 0b01110, 0b01110, 0b11111, 0b11111, 0b01110
};
byte volt[8] = { //??
B01110, B11011, B10001, B10001, B10001, B10001, B11111, B00000 //??
}; //??
///////////////////////
double Setpoint, Input, Output;
//double aggKp = 6.4, aggKi = 0.25, aggKd = 2;
//double aggKp = 4, aggKi = 0.10, aggKd = 1;
double aggKp = P_PID, aggKi = I_PID, aggKd = D_PID;
double consKp = aggKp, consKi = aggKi, consKd = aggKd;
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
String ver = "1.0.3";
Serial.begin(9600);
lcd.init();
lcd.backlight();
myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(0, 255);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(PIDPin, OUTPUT);
pinMode(buzzPin, OUTPUT);
pinMode(potPin, INPUT_PULLUP);
pinMode(heaterLed, OUTPUT);
delay(200);
lcd.createChar(2, temp);
lcd.createChar(3, energy); //??
lcd.createChar(4, volt); //??
// welcome screen
lcd.setCursor(0, 0);
lcd.print(" ArduSolder ");
lcd.setCursor(0, 1);
lcd.print(" ver ");
lcd.print(ver);
delay(1000);
lcd.clear();
}
void loop() {
printDebug();
checkLocalButton();
time_passed = millis();
int Output_led = Output;
// readout from the soldering iron
voltageMillis = millis();
if (voltageMillis - previousMillis >= pause_time)
{
read_value = ktc.readCelsius();
previousMillis = voltageMillis;
// Serial.print("Temp C = ");
// Serial.println(ktc.readCelsius());
}
// checking if soldering iron is connected
//
// if (read_value == 0) //
// {
// tone(buzzPin, 250, 20);
// lcd.setCursor(0, 0);
// lcd.print(" Podlacz kolbe ");
// lcd.setCursor(0, 1);
// lcd.print("stacja wylaczona ");
// stat = 0;
// analogWrite(PIDPin, 0);
// noTone(buzzPin);
// }
// // jest podlaczona
// else
// {
// sprawdzanie czy temp. nie przekroczyła granicznych wartości
if (read_value < temp_max)
{
if (voltage_measurement == 1)
{
voltage_read_value = analogRead(powerPin); //??
voltanie = (voltage_read_value / 1024) * 25 * 0.9715; //??
}
// read_value data from the pot
set_value = analogRead(potPin);
// counting set up temperature
voltage = (set_value / 2.58) + 60; //////// 66 i 2,55?
if (voltage > 450) voltage = 450;
if (voltage < 60) voltage = 60;
// set temperature
Setpoint = voltage;
if (stat == 0) // status OFF
{
Output = 0;
analogWrite(PIDPin, Output);
}
else // status ON
{
Input = read_value;
double gap = abs(Setpoint - Input);
if (gap < 30)
{
myPID.SetTunings(consKp, consKi, consKd);
}
else
{
myPID.SetTunings(aggKp, aggKi, aggKd);
}
myPID.Compute();
analogWrite(PIDPin, Output);
}
// format set value to LCD readout
voltage_lcd_set_value = String(voltage);
int length1 = voltage_lcd_set_value.length();
if (length1 == 2)
{
voltage_lcd_set_value = " " + voltage_lcd_set_value;
}
else if (length1 == 3) {
voltage_lcd_set_value = "" + voltage_lcd_set_value;
}
else { }
// format voltage set value to LCD readout
if (voltage_measurement == 1)
{
voltage_lcd = String(voltanie, 2);
int length4 = voltage_lcd.length();
if (length4 == 4)
{
voltage_lcd = " " + voltage_lcd;
}
else if (length4 == 5)
{
voltage_lcd = "" + voltage_lcd;
}
else
{
}
}
voltage_lcd_read_value = String(read_value);
int length2 = voltage_lcd_read_value.length();
if (length2 == 1)
{
voltage_lcd_read_value = " " + voltage_lcd_read_value;
}
else if (length2 == 2)
{
voltage_lcd_read_value = " " + voltage_lcd_read_value;
}
else if (length2 == 3)
{
voltage_lcd_read_value = "" + voltage_lcd_read_value;
}
pwr_lcd = String(Output / 2.55, 0);
int length13 = pwr_lcd.length();
if (length13 == 1)
{
pwr_lcd = " " + pwr_lcd;
}
else if (length13 == 2)
{
pwr_lcd = " " + pwr_lcd;
}
else if (length13 == 3)
{
pwr_lcd = "" + pwr_lcd;
}
else {}
// readout voltage measurement
if (voltage_measurement == 1)
{
lcd.setCursor(6, 0);
lcd.write(4);
lcd.setCursor(10, 0);
lcd.print(voltage_lcd);
lcd.setCursor(15, 0);
lcd.print("V");
}
else
{
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.write(2);
lcd.print(" = ");
lcd.setCursor(5, 1);
lcd.print(voltage_lcd_read_value);
lcd.print((char)223);
lcd.print("C ");
lcd.setCursor(12, 0);
lcd.print(pwr_lcd);
lcd.print("%");
lcd.setCursor(0, 0);
lcd.print("SET= ");
lcd.setCursor(5, 0);
lcd.print(voltage_lcd_set_value);
lcd.print((char)223);
lcd.print("C ");
if ( stat == 0)
{
lcd.setCursor(10, 0);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(" OFF ");
digitalWrite(heaterLed, LOW);
}
if ( stat == 1)
{
// lcd.setCursor(11, 0);
// lcd.write(3);
lcd.setCursor(11, 1);
lcd.print(" ON ");
analogWrite(heaterLed, Output_led / 1.5);
}
}
}
else
{
lcd.setCursor(0, 0);
lcd.clear();
lcd.print(" UWAGA !! ");
lcd.setCursor(0, 1);
lcd.print(" Temp. > ");
lcd.print(temp_max);
lcd.print((char)223);
lcd.print("C");
delay(700);
lcd.setCursor(0, 1);
lcd.print("stacja wylaczona");
stat = 0;
analogWrite(PIDPin, 0);
}
}
void printDigits(byte digits)
{
if (digits < 10)
{
lcd.print('0');
lcd.print(digits, DEC);
}
}
void checkLocalButton()
{
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState && last_button_time - button_time >= button_wait) // debouncing prevention
{
button_time = millis();
if (buttonState == LOW) // changed to pressed
{
last_button_time = millis();
stat = !stat;
}
}
lastButtonState = buttonState;
last_button_time = millis();
}
void printDebug() // just for debugging
{
// int pin = digitalRead(buttonPin);
// int heaterled = digitalRead(heaterLed);
// Serial.print("button ");
// / Serial.println(button);
// Serial.print("heaterLed ");
// Serial.println(heaterled);
Serial.print("button ");
Serial.println(buttonState);
// Serial.print("last_button_time ");
// Serial.println(last_button_time);
// Serial.print("button_time ");
// Serial.println(button_time);
// delay (200);
// Serial.println(voltage_lcd_read_value);
// Serial.println(P_PID);
}