reflow/reflowctl/reflowctl.ino

503 lines
10 KiB
Arduino
Raw Normal View History

2012-10-20 13:02:25 +00:00
// include the library code:
#include <LiquidCrystal.h>
#include <DFR_Key.h>
2012-10-01 19:03:30 +00:00
2012-10-04 15:59:45 +00:00
// states
2012-10-23 17:23:57 +00:00
#define CONFIG_STATE 0
#define START_STATE 1
#define PREHEAT_STATE 2
#define RAMP_UP_STATE 3
#define TAL_FIRST_STATE 4
#define PEAK_STATE 5
#define TAL_SECOND_STATE 6
#define RAMP_DOWN_STATE 7
#define END_STATE 8
#define ERROR_STATE 9
2012-10-02 20:00:49 +00:00
// error conditions
#define E_DT_MIN 1 // temperature dt too small
#define E_DT_MAX 2 // temperature dt too big
2012-10-02 20:00:49 +00:00
#define E_TIME_MAX 4 // reflow process does take too long
2012-10-04 15:59:45 +00:00
#define E_TS_TOO_SHORT 8 // Ts duration too short
#define E_TS_TOO_LONG 16 // Ts duration too long
#define E_TL_TOO_SHORT 32 // Tl duration too short
#define E_TL_TOO_LONG 64 // Tl duration too long
#define E_TP_TOO_SHORT 128 // Tp duration too short
#define E_TP_TOO_LONG 256 // Tp duration too long
2012-10-02 20:00:49 +00:00
// system time, timestamps and temperatures from sensors
2012-10-20 13:02:25 +00:00
unsigned int time = 0; // profile seconds
unsigned int temperature = 25; // actual oven temp
unsigned int last_temperature = 25; // last oven temp
int actual_dt = 0; // actual difference from last to actual temperatur
2012-10-01 19:03:30 +00:00
2012-10-23 17:23:57 +00:00
typedef struct {
// profile temperatures
unsigned int Ts_min;
unsigned int Ts_max;
unsigned int Tl;
unsigned int Tp;
unsigned int time_max;
// profile temp per second rates
int ramp_up_rate_min;
int ramp_up_rate_max;
int ramp_down_max;
int ramp_down_min;
// profile temp durations
unsigned int Ts_duration_min;
unsigned int Ts_duration_max;
unsigned int Tl_duration_min;
unsigned int Tl_duration_max;
unsigned int Tp_duration_min;
unsigned int Tp_duration_max;
} Profile;
struct Profile profile {150, // °C
unsigned int Ts_max = 200; // °C
unsigned int Tl = 217; // 217°C
unsigned int Tp = 260; // 245-260°C
unsigned int time_max = 480; // 8*60s max
// profile temp per second rates
int ramp_up_rate_min = 0; // not used yet
unsigned int ramp_up_rate_max = 50; // 3°C/s
unsigned int ramp_down_max = 6; // 6°C/s max
unsigned int ramp_down_min = 2; // 2°C/s max
// profile temp durations
unsigned int Ts_duration_min = 60; // s
unsigned int Ts_duration_max = 180; // s
unsigned int Tl_duration_min = 60; // 60-150s
unsigned int Tl_duration_max = 150; // 60-150s
unsigned int Tp_duration_min = 20; // 20-40s
unsigned int Tp_duration_max = 40; // 20-40s
} Profile;
2012-10-03 15:58:34 +00:00
// timestamps of event beginnings/ends
2012-10-04 15:59:45 +00:00
unsigned int Ts_time_start = 0;
unsigned int Ts_time_end = 0;
unsigned int Tl_time_start = 0;
unsigned int Tl_time_end = 0;
unsigned int Tp_time_start = 0;
unsigned int Tp_time_end = 0;
2012-10-03 00:29:05 +00:00
// thermostat
2012-10-23 17:23:57 +00:00
static unsigned int set_min = 0;
static unsigned int set_max = 0;
static int set_dt_min = 0;
static int set_dt_max = 0;
// state machine
2012-10-23 17:23:57 +00:00
static unsigned int error_condition = 0;
static byte state = 0;
static boolean is_oven_heating = false;
// ui stuff
2012-10-23 17:23:57 +00:00
static boolean led_on = false;
static boolean disable_checks = true;
2012-10-03 15:58:34 +00:00
2012-10-20 13:02:25 +00:00
//Pin assignments for SainSmart LCD Keypad Shield
2012-10-23 17:23:57 +00:00
static LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
static DFR_Key keypad;
2012-10-20 13:02:25 +00:00
2012-10-01 19:03:30 +00:00
void setup() {
Serial.begin(9600);
2012-10-21 11:47:44 +00:00
delay(300);
2012-10-04 15:59:45 +00:00
pinMode(13, OUTPUT);
2012-10-21 11:47:44 +00:00
pinMode(2, INPUT);
2012-10-03 18:09:27 +00:00
set_start_state();
2012-10-23 17:23:57 +00:00
2012-10-20 13:02:25 +00:00
lcd.begin(16, 2);
lcd.clear();
keypad.setRate(10);
2012-10-03 15:58:34 +00:00
}
2012-10-03 18:09:27 +00:00
2012-10-21 11:47:44 +00:00
void print_profile_state() {
2012-10-23 17:23:57 +00:00
String tmp("Profile: ");
2012-10-21 11:47:44 +00:00
tmp += state;
tmp += "/";
2012-10-23 17:23:57 +00:00
tmp += END_STATE;
2012-10-21 11:47:44 +00:00
lcd.setCursor(0, 1);
lcd.print(tmp);
}
void print_status() {
2012-10-23 17:23:57 +00:00
String tmp("T: ");
if (time < 10)
tmp += "00";
else if (time < 100)
tmp += "0";
2012-10-21 11:47:44 +00:00
tmp += time;
2012-10-23 17:23:57 +00:00
tmp += " Tmp: ";
if (temperature < 10)
tmp += "00";
else if (temperature < 100)
tmp += "0";
2012-10-21 11:47:44 +00:00
tmp += temperature;
lcd.setCursor(0, 0);
lcd.print(tmp);
}
2012-10-23 17:23:57 +00:00
void print_config_state() {
for (;;) {
}
}
2012-10-21 11:47:44 +00:00
/* led edit mode procedures
* global menu (up/down) -> select
* start
* edit profile
* select value (up/down) -> select | left
* value (up/down) -> select | left
*
*/
2012-10-23 17:23:57 +00:00
void get_time() {
if (state>0)
time++;
2012-10-21 11:47:44 +00:00
}
2012-10-20 13:02:25 +00:00
void control_oven() {
2012-10-04 15:59:45 +00:00
if (temperature < set_min && !is_oven_heating) {
2012-10-03 15:58:34 +00:00
is_oven_heating = true;
Serial.println("Oven turned on");
}
2012-10-04 15:59:45 +00:00
else if (temperature > set_min && is_oven_heating) {
2012-10-03 15:58:34 +00:00
is_oven_heating = false;
Serial.println("Oven turned off");
}
2012-10-01 19:03:30 +00:00
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void set_temp(int min, int max, int dt_min, int dt_max) {
2012-10-03 18:09:27 +00:00
set_min = min;
set_max = max;
set_dt_min = dt_min;
set_dt_max = dt_max;
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void get_temp() {
last_temperature = temperature;
temperature = int(float(analogRead(2)) * 0.2929);
actual_dt = temperature - last_temperature;
2012-10-02 13:03:02 +00:00
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void check_dt() {
2012-10-04 15:59:45 +00:00
if (disable_checks)
return;
2012-10-03 18:09:27 +00:00
if (actual_dt > set_dt_max) {
error_condition |= E_DT_MAX;
2012-10-02 20:00:49 +00:00
}
2012-10-03 18:09:27 +00:00
if (actual_dt < set_dt_min) {
error_condition |= E_DT_MIN;
2012-10-02 20:00:49 +00:00
}
2012-10-01 19:03:30 +00:00
}
2012-10-20 13:02:25 +00:00
void print_debug() {
2012-10-03 18:09:27 +00:00
Serial.print("Time: ");
Serial.print(time);
Serial.print(", temperatur: ");
Serial.print(temperature);
2012-10-03 18:09:27 +00:00
Serial.print(", last_temperatur: ");
Serial.print(last_temperature);
2012-10-03 18:09:27 +00:00
Serial.print(", state: ");
Serial.print(state);
Serial.print(", Error: ");
Serial.println(error_condition);
2012-10-02 20:00:49 +00:00
}
2012-10-20 13:02:25 +00:00
void check_max_duration() {
2012-10-04 15:59:45 +00:00
if (disable_checks)
return;
Serial.println(time);
2012-10-02 20:00:49 +00:00
if (time > time_max) {
2012-10-03 21:52:23 +00:00
error_condition |= E_TIME_MAX;
}
2012-10-04 15:59:45 +00:00
Serial.println(time);
}
2012-10-20 13:02:25 +00:00
void check_Ts_duration_min() {
2012-10-04 15:59:45 +00:00
if (disable_checks)
return;
Tl_time_end = time;
if (time - Tl_time_start < Tl_duration_min) {
error_condition |= E_TL_TOO_SHORT;
}
}
2012-10-20 13:02:25 +00:00
void check_Ts_duration_max() {
2012-10-04 15:59:45 +00:00
if (disable_checks)
return;
if (time - Ts_time_start > Tl_duration_max) {
error_condition |= E_TS_TOO_LONG;
}
2012-10-03 21:52:23 +00:00
}
2012-10-04 15:59:45 +00:00
2012-10-20 13:02:25 +00:00
void check_Tl_duration_min() {
2012-10-04 15:59:45 +00:00
if (disable_checks)
return;
Tl_time_end = time;
if (time - Tl_time_start < Tl_duration_min) {
error_condition |= E_TL_TOO_SHORT;
}
}
2012-10-20 13:02:25 +00:00
void check_Tl_duration_max() {
2012-10-04 15:59:45 +00:00
if (disable_checks)
return;
if (time - Tl_time_start > Tl_duration_max) {
2012-10-03 21:52:23 +00:00
error_condition |= E_TL_TOO_LONG;
2012-10-02 20:00:49 +00:00
}
2012-10-03 21:52:23 +00:00
}
2012-10-20 13:02:25 +00:00
void check_Tp_duration_min() {
2012-10-04 15:59:45 +00:00
Tp_time_end = time;
if (time - Tp_time_start < Tp_duration_min) {
error_condition |= E_TP_TOO_SHORT;
}
}
2012-10-20 13:02:25 +00:00
void check_Tp_duration_max() {
2012-10-04 15:59:45 +00:00
if (disable_checks)
return;
if (time - Tp_time_start > Tp_duration_max) {
2012-10-03 21:52:23 +00:00
error_condition |= E_TP_TOO_LONG;
}
}
2012-10-03 18:09:27 +00:00
2012-10-20 13:02:25 +00:00
void set_start_state() {
led_on = false;
digitalWrite(13, LOW);
error_condition = 0;
state = START_STATE;
2012-10-03 18:09:27 +00:00
get_temp();
last_temperature = temperature;
actual_dt = temperature - last_temperature;
set_temp(Tp-5, Tp, 0, ramp_up_rate_max);
2012-10-02 20:00:49 +00:00
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void set_preheat_state() {
Serial.println("Changing state to PREHEAT_STATE");
state++;
2012-10-03 18:09:27 +00:00
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void set_ramp_up_state() {
Serial.println("Changed state to RAMP_UP_STATE");
state++;
2012-10-03 18:09:27 +00:00
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void set_tal_first_state() {
Serial.println("Changed state to TAL_FIRST_STATE");
state++;
2012-10-03 18:09:27 +00:00
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void set_peak_state() {
2012-10-03 18:09:27 +00:00
Serial.println("Changed state to PEAK_STATE");
state++;
2012-10-03 18:09:27 +00:00
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void set_tal_second_state() {
2012-10-03 18:09:27 +00:00
Serial.println("Changed state to TAL_SECOND_STATE");
set_temp(25, 25, -3, -6);
state++;
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void set_ramp_down_state() {
2012-10-03 18:09:27 +00:00
Serial.println("Changed state to RAMP_DOWN_STATE");
state++;
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void set_end_state() {
Serial.println("Changed state to END_STATE");
state++;
2012-10-03 18:09:27 +00:00
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void set_error_state() {
2012-10-03 18:09:27 +00:00
if (state != ERROR_STATE) {
Serial.println("Changed state to ERROR_STATE");
2012-10-03 18:09:27 +00:00
set_temp(0, 0, 0, 0);
state = ERROR_STATE;
}
}
2012-10-01 19:03:30 +00:00
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void handle_start_state() {
2012-10-04 15:59:45 +00:00
check_max_duration();
Serial.println(time);
if (temperature > Ts_min) {
Ts_time_start = time;
set_preheat_state();
2012-10-03 15:58:34 +00:00
}
}
2012-10-20 13:02:25 +00:00
void handle_preheat_state() {
2012-10-04 15:59:45 +00:00
check_Ts_duration_max();
check_max_duration();
if (temperature > Ts_max) {
2012-10-04 15:59:45 +00:00
check_Ts_duration_min();
set_ramp_up_state();
2012-10-03 18:09:27 +00:00
}
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void handle_ramp_up_state() {
2012-10-04 15:59:45 +00:00
check_max_duration();
if (temperature > Tl) {
2012-10-03 18:09:27 +00:00
Tl_time_start = time;
2012-10-03 21:26:38 +00:00
set_tal_first_state();
2012-10-03 18:09:27 +00:00
}
}
2012-10-03 21:33:23 +00:00
2012-10-20 13:02:25 +00:00
void handle_tal_first_state() {
2012-10-04 15:59:45 +00:00
check_max_duration();
check_Tl_duration_max();
if (temperature > Tp - 5) {
2012-10-03 18:09:27 +00:00
Tp_time_start = time;
set_peak_state();
}
}
2012-10-03 15:58:34 +00:00
2012-10-20 13:02:25 +00:00
void handle_peak_state() {
2012-10-04 15:59:45 +00:00
check_Tl_duration_max();
check_Tp_duration_max();
if (time - Tp_time_start > Tp_duration_max) {
check_Tp_duration_min();
2012-10-03 18:09:27 +00:00
set_tal_second_state();
}
}
2012-10-20 13:02:25 +00:00
void handle_tal_second_state() {
2012-10-04 15:59:45 +00:00
check_Tl_duration_max();
if (temperature < Tl) {
2012-10-04 15:59:45 +00:00
check_Tl_duration_min();
2012-10-03 18:09:27 +00:00
set_ramp_down_state();
}
}
2012-10-20 13:02:25 +00:00
void handle_ramp_down_state() {
if (temperature < Ts_min) {
set_end_state();
2012-10-03 00:29:05 +00:00
}
}
2012-10-02 20:00:49 +00:00
2012-10-02 15:46:12 +00:00
2012-10-20 13:02:25 +00:00
void handle_end_state() {
2012-10-04 15:59:45 +00:00
// while(true) {
// continue;
// }
2012-10-03 21:33:23 +00:00
}
2012-10-20 13:02:25 +00:00
void handle_error_state() {
2012-10-03 18:09:27 +00:00
if (led_on) {
digitalWrite(13, LOW);
led_on = false;
}
else {
digitalWrite(13, HIGH);
led_on = true;
}
if (error_condition & E_DT_MIN)
2012-10-03 21:52:23 +00:00
Serial.println("Error: delta °K/second too low");
2012-10-03 18:09:27 +00:00
if (error_condition & E_DT_MAX)
2012-10-03 21:52:23 +00:00
Serial.println("Error: delta °K/second too big");
if (error_condition & E_TIME_MAX)
Serial.println("Error: reflow process does take too long");
2012-10-04 15:59:45 +00:00
if (error_condition & E_TS_TOO_SHORT)
Serial.println("Error: heatup duration was too short");
if (error_condition & E_TS_TOO_LONG)
Serial.println("Error: heatup duration was too long");
if (error_condition & E_TL_TOO_SHORT)
Serial.println("Error: temperature above liquidus duration was too short");
2012-10-03 21:52:23 +00:00
if (error_condition & E_TL_TOO_LONG)
2012-10-04 15:59:45 +00:00
Serial.println("Error: temperature above liquidus duration was too long");
2012-10-03 21:52:23 +00:00
if (error_condition & E_TP_TOO_LONG)
2012-10-04 15:59:45 +00:00
Serial.println("Error: peak temperature duration was too short");
if (error_condition & E_TP_TOO_SHORT)
2012-10-03 21:52:23 +00:00
Serial.println("Error: peak temperature duration was too long");
2012-10-03 18:09:27 +00:00
}
2012-10-03 00:29:05 +00:00
void loop() {
2012-10-23 17:23:57 +00:00
get_time();
2012-10-03 15:58:34 +00:00
get_temp();
2012-10-03 18:09:27 +00:00
check_dt();
2012-10-23 17:23:57 +00:00
if (!error_condition) {
print_debug();
}
else {
set_error_state();
}
switch (state) {
case CONFIG_STATE:
handle_config_state();
print_config_state()
break;
case START_STATE:
handle_start_state();
break;
case PREHEAT_STATE:
handle_preheat_state();
break;
case RAMP_UP_STATE:
handle_ramp_up_state();
break;
case TAL_FIRST_STATE:
handle_tal_first_state();
break;
case PEAK_STATE:
handle_peak_state();
break;
case TAL_SECOND_STATE:
Tl_time_end = time;
handle_tal_second_state();
break;
case RAMP_DOWN_STATE:
handle_ramp_down_state();
break;
case END_STATE:
handle_end_state();
break;
case ERROR_STATE:
handle_error_state();
break;
default:
break;
}
control_oven();
// lcd.clear();
2012-10-21 11:47:44 +00:00
print_debug();
print_status();
print_profile_state();
2012-10-03 00:29:05 +00:00
delay(1000);
2012-10-01 19:03:30 +00:00
}
2012-10-03 15:58:34 +00:00