//
//
// Soldering Iron Tip temperature controller
//
// 2016/9/17,
//
#include "Wire.h"
#include "Adafruit_LEDBackpack.h"
#include "max6675.h"
#include "MsTimer2.h"
#define TMR2_INT 500
MAX6675 TC1(A3, A2, A1); // = (CLK,CS,DO) , define pins for thermocouple1
Adafruit_7segment matrix = Adafruit_7segment();
#define SSR_PIN 3
#define VR_PIN A0
bool isTimer2Int = false;
int cnt8 = 0;
// on-level 0, 1, 2, 3, 4, 5, 6, 7, 8
// 0% 12.5% 25% 37.5% 50% 62.5% 75% 87.5% 100%
int strength[9] = {0x00, 0x80, 0x88, 0x89, 0xaa, 0xab, 0xee, 0xfe, 0xff};
void timer2ISR()
{
isTimer2Int = true; // ISR for Timer2
if (++cnt8 > 7) cnt8 = 0; // for power control by 1/8 resolution
}
/****************************************
Setup
*****************************************/
void setup()
{
Serial.begin(115200); // 115200 baud
matrix.begin(0x70);
matrix.setBrightness(8);
matrix.clear();
digitalWrite(SSR_PIN, LOW);
pinMode(SSR_PIN, OUTPUT);
MsTimer2::set(TMR2_INT, timer2ISR);
MsTimer2::start();
}
/**************************************************
Main Loop
**************************************************/
void loop()
{
double temp1;
double t_max, t_min, t_delta;
int vr;
int temp2;
int p_level;
if (isTimer2Int) {
isTimer2Int = false;
temp1 = TC1.readCelsius();
vr = analogRead(VR_PIN);
temp2 = (int)(temp1 * 10.0);
t_max = vr * 150.0 / 750.0 + 200.0; //max 400 degree when ADC = 1000
// Serial.print(t_max, 1);
// Serial.print(",");
Serial.println(temp1, 1);
t_delta = t_max - temp1; // if still need to up
if (vr < 32) {
digitalWrite(SSR_PIN, LOW); // power-off regardless to the temperature
display_off(temp1);
} else {
if (t_delta > 0) {
if (t_delta > 90.0) p_level = strength[8]; //100%
else if (t_delta > 60.0) p_level = strength[5]; //87.5%
else if (t_delta > 30.0) p_level = strength[4]; //50%
else if (t_delta > 20.0) p_level = strength[3]; //37.5%
else if (t_delta > 10.0) p_level = strength[2]; //25%
else if (t_delta > 5.0) p_level = strength[1]; //12.5%
else p_level = strength[0]; //0% stop heating when it reaches to 5 degree below the target
if ((p_level >> cnt8) & 0x01) {
digitalWrite(SSR_PIN, HIGH);
matrix.drawColon(true);
} else {
digitalWrite(SSR_PIN, LOW);
matrix.drawColon(false);
}
} else {
digitalWrite(SSR_PIN, LOW); // if temp1 is higher than target then off,
matrix.drawColon(false);
}
matrix.writeDigitNum(0, (temp2 / 1000), false);
matrix.writeDigitNum(1, (temp2 / 100) % 10, false);
matrix.writeDigitNum(3, (temp2 / 10) % 10, true);
matrix.writeDigitNum(4, temp2 % 10, false);
matrix.writeDisplay();
}
}
}
//
// display "OFF" on the LED display
//
void display_off(double t)
{
matrix.setBrightness(2);
matrix.drawColon(false);
matrix.writeDigitRaw(0, 0); //blank
matrix.writeDigitRaw(1, 0); //blank
matrix.writeDigitRaw(3, 0); //blank
if (t > 60) matrix.writeDigitRaw(0, 0x80); //blank with dot.
if (t > 50) matrix.writeDigitRaw(1, 0x80); //blank with dot.
if (t > 40) matrix.writeDigitRaw(3, 0x80); //blank with dot.
matrix.writeDigitRaw(4, 0x80); //blank with dot.
matrix.writeDisplay();
}