2012-10-01 19:03:30 +00:00
|
|
|
|
2012-10-02 20:00:49 +00:00
|
|
|
#define START_STATE 0
|
|
|
|
#define PREHEAT_STATE 1
|
|
|
|
#define RAMP_UP_STATE 2
|
|
|
|
#define TAL_FIRST_STATE 3
|
|
|
|
#define PEAK_STATE 4
|
|
|
|
#define TAL_SECOND_STATE 5
|
2012-10-03 15:58:34 +00:00
|
|
|
#define RAMP_DOWN_STATE 6
|
2012-10-02 20:00:49 +00:00
|
|
|
#define END_STATE 7
|
|
|
|
#define ERROR_STATE 8
|
|
|
|
|
|
|
|
// error conditions
|
2012-10-03 18:09:27 +00:00
|
|
|
#define E_DT_MIN 1 // temperatur dt too small
|
|
|
|
#define E_DT_MAX 2 // temperatur dt too big
|
2012-10-02 20:00:49 +00:00
|
|
|
#define E_TIME_MAX 4 // reflow process does take too long
|
2012-10-03 21:52:23 +00:00
|
|
|
#define E_TL_TOO_LONG 8 // package was roasted
|
|
|
|
#define E_TP_TOO_LONG 16 // package was roasted
|
2012-10-02 20:00:49 +00:00
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
// system time, timestamps and temperatures from sensors
|
|
|
|
__attribute__((__unused__)) static unsigned int time = 0; // profile seconds
|
|
|
|
__attribute__((__unused__)) static unsigned int temperatur = 25; // actual oven temp
|
|
|
|
__attribute__((__unused__)) static unsigned int last_temperatur = 25; // last oven temp
|
2012-10-03 21:37:25 +00:00
|
|
|
__attribute__((__unused__)) static int actual_dt = 0; // actual difference from last to actual temperatur
|
2012-10-01 19:03:30 +00:00
|
|
|
|
2012-10-03 15:58:34 +00:00
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
// profile temperatures
|
|
|
|
__attribute__((__unused__)) static unsigned int Ts_min = 150; // °C
|
|
|
|
__attribute__((__unused__)) static unsigned int Ts_max = 200; // °C
|
|
|
|
__attribute__((__unused__)) static unsigned int Tl = 217; // 217°C
|
|
|
|
__attribute__((__unused__)) static unsigned int Tp = 260; // 245-260°C
|
|
|
|
__attribute__((__unused__)) static unsigned int time_max = 480; // 8*60s max
|
2012-10-03 18:09:27 +00:00
|
|
|
|
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
// profile temp per second rates
|
|
|
|
__attribute__((__unused__)) static unsigned int ramp_up_rate_min = 0; // not used yet
|
|
|
|
__attribute__((__unused__)) static unsigned int ramp_up_rate_max = 50; // 3°C/s
|
|
|
|
__attribute__((__unused__)) static unsigned int ramp_down_max = 6; // 6°C/s max
|
|
|
|
__attribute__((__unused__)) static unsigned int ramp_down_min = 2; // 2°C/s max
|
2012-10-01 19:03:30 +00:00
|
|
|
|
2012-10-02 13:03:02 +00:00
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
// profile temp durations
|
|
|
|
__attribute__((__unused__)) static unsigned int Ts_duration = 100; // 60-180s
|
|
|
|
__attribute__((__unused__)) static unsigned int Tl_duration = 100; // 60-150s
|
|
|
|
__attribute__((__unused__)) static unsigned int Tp_duration = 30; // 20-40s
|
2012-10-03 15:58:34 +00:00
|
|
|
|
2012-10-01 19:03:30 +00:00
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
// timestamps of event beginnings/ends
|
|
|
|
__attribute__((__unused__)) static unsigned int Ts_min_time = 0;
|
|
|
|
__attribute__((__unused__)) static unsigned int Ts_max_time = 0;
|
|
|
|
__attribute__((__unused__)) static unsigned int Tl_time_start = 0;
|
|
|
|
__attribute__((__unused__)) static unsigned int Tl_time_end = 0;
|
|
|
|
__attribute__((__unused__)) static unsigned int Tp_time_start = 0;
|
|
|
|
__attribute__((__unused__)) static unsigned int Tp_time_end = 0;
|
2012-10-03 00:29:05 +00:00
|
|
|
|
2012-10-03 15:58:34 +00:00
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
// thermostat
|
|
|
|
__attribute__((__unused__)) static unsigned int set_min = 0;
|
|
|
|
__attribute__((__unused__)) static unsigned int set_max = 0;
|
|
|
|
__attribute__((__unused__)) static int set_dt_min = 0;
|
|
|
|
__attribute__((__unused__)) static int set_dt_max = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// state machine
|
|
|
|
__attribute__((__unused__)) static byte state;
|
|
|
|
__attribute__((__unused__)) static boolean is_oven_heating = false;
|
|
|
|
__attribute__((__unused__)) static byte error_condition = 0;
|
|
|
|
|
|
|
|
// ui stuff
|
|
|
|
__attribute__((__unused__)) static boolean led_on = false;
|
2012-10-03 15:58:34 +00:00
|
|
|
|
2012-10-01 19:03:30 +00:00
|
|
|
void setup() {
|
|
|
|
Serial.begin(9600);
|
2012-10-03 18:09:27 +00:00
|
|
|
delay(2000);
|
|
|
|
set_start_state();
|
2012-10-03 15:58:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-10-03 18:09:27 +00:00
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
static void control_oven() {
|
2012-10-03 18:09:27 +00:00
|
|
|
if (temperatur < set_min && (!is_oven_heating)) {
|
2012-10-03 15:58:34 +00:00
|
|
|
is_oven_heating = true;
|
|
|
|
Serial.println("Oven turned on");
|
|
|
|
}
|
2012-10-03 18:09:27 +00:00
|
|
|
else if (temperatur > set_max && 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-03 21:17:50 +00:00
|
|
|
static 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-03 21:17:50 +00:00
|
|
|
static void get_temp() {
|
2012-10-03 15:58:34 +00:00
|
|
|
last_temperatur = temperatur;
|
2012-10-03 21:17:50 +00:00
|
|
|
temperatur = int(float(analogRead(2)) * 0.2929);
|
2012-10-03 18:09:27 +00:00
|
|
|
actual_dt = temperatur - last_temperatur;
|
2012-10-02 13:03:02 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 21:33:23 +00:00
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
static void check_dt() {
|
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-03 21:17:50 +00:00
|
|
|
static void print_debug() {
|
2012-10-03 18:09:27 +00:00
|
|
|
Serial.print("Time: ");
|
|
|
|
Serial.print(time);
|
|
|
|
Serial.print(", temperatur: ");
|
|
|
|
Serial.print(temperatur);
|
|
|
|
Serial.print(", last_temperatur: ");
|
|
|
|
Serial.print(last_temperatur);
|
|
|
|
Serial.print(", state: ");
|
|
|
|
Serial.print(state);
|
|
|
|
Serial.print(", Error: ");
|
|
|
|
Serial.println(error_condition);
|
2012-10-02 20:00:49 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 21:52:23 +00:00
|
|
|
boolean check_max_duration() {
|
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;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean check_Tl_duration() {
|
|
|
|
if (time - Tl_time_start > Tp_duration) {
|
|
|
|
error_condition |= E_TL_TOO_LONG;
|
2012-10-02 20:00:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-03 21:52:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean check_Tp_duration() {
|
|
|
|
if (time - Tp_time_start > Tp_duration) {
|
|
|
|
error_condition |= E_TP_TOO_LONG;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-10-03 18:09:27 +00:00
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
static void set_start_state() {
|
|
|
|
state = START_STATE;
|
2012-10-03 18:09:27 +00:00
|
|
|
get_temp();
|
|
|
|
last_temperatur = temperatur;
|
2012-10-03 21:17:50 +00:00
|
|
|
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-03 21:17:50 +00:00
|
|
|
static 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-03 21:17:50 +00:00
|
|
|
static 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-03 21:17:50 +00:00
|
|
|
static 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-03 21:17:50 +00:00
|
|
|
static void set_peak_state() {
|
2012-10-03 18:09:27 +00:00
|
|
|
Serial.println("Changed state to PEAK_STATE");
|
2012-10-03 21:17:50 +00:00
|
|
|
state++;
|
2012-10-03 18:09:27 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 21:33:23 +00:00
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
static 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-03 21:17:50 +00:00
|
|
|
static 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-03 21:17:50 +00:00
|
|
|
static 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-03 18:09:27 +00:00
|
|
|
void set_error_state() {
|
|
|
|
if (state != ERROR_STATE) {
|
2012-10-03 21:17:50 +00:00
|
|
|
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-03 21:17:50 +00:00
|
|
|
static void handle_start_state() {
|
2012-10-03 15:58:34 +00:00
|
|
|
if (temperatur > Ts_min) {
|
|
|
|
Ts_min_time = time;
|
2012-10-03 21:17:50 +00:00
|
|
|
set_preheat_state();
|
2012-10-03 15:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
static void handle_preheat_state() {
|
2012-10-03 18:09:27 +00:00
|
|
|
if (temperatur > Ts_max) {
|
|
|
|
Ts_max_time = time;
|
2012-10-03 21:17:50 +00:00
|
|
|
set_ramp_up_state();
|
2012-10-03 18:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 21:33:23 +00:00
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
static void handle_ramp_up_state() {
|
2012-10-03 18:09:27 +00:00
|
|
|
if (temperatur > Tl) {
|
|
|
|
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-03 21:17:50 +00:00
|
|
|
static void handle_tal_first_state() {
|
2012-10-03 21:52:23 +00:00
|
|
|
check_Tl_duration();
|
2012-10-03 18:09:27 +00:00
|
|
|
if (temperatur > Tp - 5) {
|
|
|
|
Tp_time_start = time;
|
|
|
|
set_peak_state();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 15:58:34 +00:00
|
|
|
|
2012-10-03 21:33:23 +00:00
|
|
|
static void handle_peak_state() {
|
2012-10-03 21:52:23 +00:00
|
|
|
check_Tl_duration();
|
|
|
|
check_Tp_duration();
|
2012-10-03 21:17:50 +00:00
|
|
|
if (time - Tp_time_start > Tp_duration) {
|
2012-10-03 15:58:34 +00:00
|
|
|
Tp_time_end = time;
|
2012-10-03 18:09:27 +00:00
|
|
|
set_tal_second_state();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
static void handle_tal_second_state() {
|
2012-10-03 21:52:23 +00:00
|
|
|
check_Tl_duration();
|
2012-10-03 18:09:27 +00:00
|
|
|
if (temperatur < Tl) {
|
|
|
|
set_ramp_down_state();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
static void handle_ramp_down_state() {
|
2012-10-03 18:09:27 +00:00
|
|
|
if (temperatur < Ts_min) {
|
2012-10-03 21:17:50 +00:00
|
|
|
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-03 21:33:23 +00:00
|
|
|
static void handle_end_state() {
|
|
|
|
while(true) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-03 21:17:50 +00:00
|
|
|
static 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");
|
|
|
|
if (error_condition & E_TL_TOO_LONG)
|
|
|
|
Serial.println("Error: temperatur above liquidus was too long");
|
|
|
|
if (error_condition & E_TP_TOO_LONG)
|
|
|
|
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-03 15:58:34 +00:00
|
|
|
time = millis() / 1000;
|
|
|
|
get_temp();
|
2012-10-03 18:09:27 +00:00
|
|
|
check_dt();
|
2012-10-03 21:52:23 +00:00
|
|
|
check_max_duration();
|
2012-10-03 18:09:27 +00:00
|
|
|
|
|
|
|
if (error_condition) {
|
|
|
|
set_error_state();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print_debug();
|
|
|
|
}
|
|
|
|
|
2012-10-03 15:58:34 +00:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case START_STATE:
|
|
|
|
handle_start_state();
|
|
|
|
break;
|
|
|
|
case PREHEAT_STATE:
|
2012-10-03 18:09:27 +00:00
|
|
|
handle_preheat_state();
|
2012-10-03 15:58:34 +00:00
|
|
|
break;
|
|
|
|
case RAMP_UP_STATE:
|
2012-10-03 21:17:50 +00:00
|
|
|
handle_ramp_up_state();
|
2012-10-03 15:58:34 +00:00
|
|
|
break;
|
|
|
|
case TAL_FIRST_STATE:
|
2012-10-03 18:09:27 +00:00
|
|
|
handle_tal_first_state();
|
2012-10-03 15:58:34 +00:00
|
|
|
break;
|
|
|
|
case PEAK_STATE:
|
|
|
|
handle_peak_state();
|
|
|
|
break;
|
|
|
|
case TAL_SECOND_STATE:
|
2012-10-03 18:09:27 +00:00
|
|
|
Tl_time_end = time;
|
|
|
|
handle_tal_second_state();
|
2012-10-03 15:58:34 +00:00
|
|
|
break;
|
|
|
|
case RAMP_DOWN_STATE:
|
2012-10-03 18:09:27 +00:00
|
|
|
handle_ramp_down_state();
|
2012-10-03 15:58:34 +00:00
|
|
|
break;
|
|
|
|
case END_STATE:
|
2012-10-03 21:33:23 +00:00
|
|
|
handle_end_state();
|
2012-10-03 18:09:27 +00:00
|
|
|
break;
|
|
|
|
case ERROR_STATE:
|
|
|
|
handle_error_state();
|
|
|
|
break;
|
2012-10-03 15:58:34 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
control_oven();
|
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
|
|
|
|