reflow/reflowctl/reflowctl.ino

230 lines
5.0 KiB
Arduino
Raw Normal View History

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
#define E_RAMPUP 1 // oven heats up too fast
#define E_RAMPDOWN_TOO_FAST 2 // oven cools down too fast
#define E_RAMPDOWN_TOO_SLOW 3 // oven cools down too slow
#define E_TIME_MAX 4 // reflow process does take too long
2012-10-03 00:29:05 +00:00
#define E_PEAK_TOO_LONG 5 // package was roasted
2012-10-02 20:00:49 +00:00
2012-10-01 19:03:30 +00:00
unsigned int time = 0; // profile seconds
2012-10-02 13:03:02 +00:00
unsigned int temperatur = 25; // actual oven temp
unsigned int last_temperatur = 25;
2012-10-01 19:03:30 +00:00
2012-10-03 15:58:34 +00:00
// profile stuff
2012-10-01 19:03:30 +00:00
unsigned int Ts_max = 200; // °C
unsigned int Ts_min = 150; // °C
unsigned int Tp = 260; // 245-260°C
2012-10-03 15:58:34 +00:00
unsigned int rampup_rate = 50; // 3°C/s
2012-10-01 19:03:30 +00:00
unsigned int preheat_duration = 100; // 60-180s
2012-10-03 15:58:34 +00:00
unsigned int Tl = 217; // 217°C
unsigned int Tl_duration = 100; // 60-150s
2012-10-01 19:03:30 +00:00
unsigned int peak_duration = 30; // 20-40s
unsigned int rampdown_max = 6; // 6°C/s max
2012-10-02 20:00:49 +00:00
unsigned int rampdown_min = 2; // 2°C/s max
2012-10-01 19:03:30 +00:00
unsigned int time_max = 480; // 8*60s max
2012-10-02 13:03:02 +00:00
unsigned int Ts_min_time = 0;
unsigned int Ts_max_time = 0;
2012-10-03 15:58:34 +00:00
unsigned int Tl_time_start = 0;
2012-10-02 20:00:49 +00:00
unsigned int Tl_time_end = 0;
2012-10-03 15:58:34 +00:00
unsigned int Tp_time_start = 0;
2012-10-03 00:29:05 +00:00
unsigned int Tp_time_end = 0;
2012-10-02 13:03:02 +00:00
unsigned int error_condition = 0;
2012-10-03 15:58:34 +00:00
boolean is_oven_heating = false;
2012-10-02 20:00:49 +00:00
byte state = START_STATE;
2012-10-01 19:03:30 +00:00
2012-10-03 00:29:05 +00:00
int analogPin = 2;
2012-10-03 15:58:34 +00:00
int hysteresis = 0;
int set_min = 0;
int set_max = 0;
2012-10-01 19:03:30 +00:00
void setup() {
Serial.begin(9600);
2012-10-03 15:58:34 +00:00
get_temp();
last_temperatur = temperatur;
control_oven(Tp, Tp);
}
void set_temp(int min, int max) {
set_min = min;
set_max = max;
}
void control_oven() {
if (temperatur < set_min) {
is_oven_heating = true;
Serial.println("Oven turned on");
}
else if (temperatur < set_max) {
is_oven_heating = false;
Serial.println("Oven turned off");
}
2012-10-01 19:03:30 +00:00
}
void get_temp() {
2012-10-02 13:03:02 +00:00
// simulating an +1K/s rampup oven
2012-10-03 15:58:34 +00:00
last_temperatur = temperatur;
temperatur = int(float(analogRead(analogPin)) * 0.2929);
2012-10-02 13:03:02 +00:00
}
2012-10-03 15:58:34 +00:00
void check_rampup_rate() {
2012-10-02 13:03:02 +00:00
if (temperatur - last_temperatur > rampup_rate) {
2012-10-02 20:00:49 +00:00
error_condition = E_RAMPUP;
2012-10-03 15:58:34 +00:00
control_oven(false);
2012-10-02 20:00:49 +00:00
}
2012-10-03 15:58:34 +00:00
else
control_oven(true);
2012-10-02 20:00:49 +00:00
}
boolean check_rampdown_rate() {
unsigned int dt = temperatur - last_temperatur;
if (dt > rampdown_max) {
error_condition = E_RAMPDOWN_TOO_FAST;
return false;
}
if (dt < rampdown_min) {
error_condition = E_RAMPDOWN_TOO_SLOW;
2012-10-02 13:03:02 +00:00
return false;
}
return true;
2012-10-01 19:03:30 +00:00
}
2012-10-03 15:58:34 +00:00
boolean check_max_duration() {
2012-10-02 20:00:49 +00:00
if (time > time_max) {
error_condition = E_TIME_MAX;
return false;
}
}
2012-10-03 15:58:34 +00:00
boolean check_Tl_duration() {
2012-10-02 20:00:49 +00:00
if (time > time_max) {
error_condition = E_TIME_MAX;
return false;
}
}
2012-10-01 19:03:30 +00:00
2012-10-03 15:58:34 +00:00
void handle_start_state() {
if (temperatur > Ts_min) {
Serial.println("Changing state to PREHEAT_STATE");
Ts_min_time = time;
state++;
}
}
void handle_peak_state() {
Serial.println("PEAK_STATE");
if (temperatur > Tp)
control_oven(false);
else
control_oven(true);
if (time - Tp_time_start > peak_duration) {
Serial.println("Changed state to TAL_SECOND_STATE");
Tp_time_end = time;
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 00:29:05 +00:00
void loop() {
2012-10-03 15:58:34 +00:00
time = millis() / 1000;
get_temp();
Serial.print(time);
Serial.print(" ");
Serial.print(temperatur);
Serial.print(" ");
Serial.print(last_temperatur);
Serial.print(" ");
Serial.println(state);
switch (state) {
case START_STATE:
Serial.println("START_STATE");
// going from room temp to preheat, nothing to check here
handle_start_state();
break;
case PREHEAT_STATE:
Serial.println("PREHEAT_STATE");
check_rampup_rate();
if (temperatur > Ts_max) {
Serial.println("Changed state to RAMP_UP_STATE");
Ts_max_time = time;
state++;
}
break;
case RAMP_UP_STATE:
Serial.println("RAMP_UP_STATE");
check_rampup_rate();
if (temperatur > Tl) {
Serial.println("Changed state to TAL_FIRST_STATE");
Tl_time_start = time;
state++;
}
break;
case TAL_FIRST_STATE:
Serial.println("TAL_FIRST_STATE");
check_rampup_rate();
if (temperatur > Tp - 5) {
Serial.println("Changed state to PEAK_STATE");
Tp_time_start = time;
state++;
}
break;
case PEAK_STATE:
handle_peak_state();
break;
case TAL_SECOND_STATE:
Serial.println("TAL_SECOND_STATE");
if (temperatur < Tl) {
Serial.println("Changed state to RAMP_DOWN_STATE");
Tl_time_end = time;
state++;
}
break;
case RAMP_DOWN_STATE:
Serial.println("RAMP_DOWN_STATE");
if (temperatur < Ts_min) {
Serial.println("Changed state to END_STATE");
state++;
}
break;
case END_STATE:
Serial.println("END_STATE");
default:
break;
}
control_oven();
2012-10-03 00:29:05 +00:00
delay(1000);
2012-10-03 15:58:34 +00:00
return;
error:
state = END_STATE;
Serial.print("Error: ");
Serial.println(error_condition);
2012-10-01 19:03:30 +00:00
}
2012-10-03 15:58:34 +00:00