private void task1_run() { while (true) { // if(serial_port.IsOpen) serial_port.Write("read\r\n"); T_rang_temp = T_rang; Thread.Sleep(100); while (task1_is_running) { Thread.Sleep(100); try { switch (state_task1) { case 1: update_status("START", true); TimerSampleSetEnable(true); CONTROL(PIN.BEEP, ON); CONTROL(PIN.MOTO1_PWM, 100); CONTROL(PIN.FAN1_PWM, 100); if (!task2_is_running) { CONTROL(PIN.MOTO2, OFF); CONTROL(PIN.FAN2, OFF); } CONTROL(PIN.VAN, ON); CONTROL(PIN.FIRE, ON); CONTROL(PIN.BEEP, OFF); btnStartSetEnable(false); btnStopSetEnable(true); btnStopSetLabel("kiểm tra đổ cafe"); state_task1 = 10; counter1s_task1 = 0; serial_port.Write("sync\r\n"); break; case 10: //Check nhiệt độ update_status("Kiểm tra gas và nhiệt độ...", true); if (cur_temperature >= N_rang) { CONTROL(PIN.BEEP, ON); counter1s_task1 = 0; update_status("Xin đổ cafe vào!", true); task2_is_maintain = true; if (MessageBox.Show("XIN ĐỔ CAFFE VÀO!\n\nNhấn [Cancel] để hủy bỏ chu trình", "Continue", MessageBoxButtons.OKCancel) == DialogResult.OK) { CONTROL(PIN.BEEP, OFF); state_task1 = 15; task2_is_maintain = false; break; } else { CONTROL(PIN.BEEP, OFF); MessageBoxEx.Show("Dừng chu trình!","Thông báo",1000); show_time = false; TimerSampleSetEnable(false); btnStartSetEnable(true); update_status("IDLE", true); TrackBarSetEnable(false); btnStopSetEnable(false); CONTROL(PIN.BEEP, ON); CONTROL(PIN.MOTO1_PWM, 0); CONTROL(PIN.FAN1_PWM, 0); CONTROL(PIN.MOTO2, OFF); CONTROL(PIN.FAN2, OFF); CONTROL(PIN.VAN, OFF); CONTROL(PIN.FIRE, OFF); CONTROL(PIN.BEEP, OFF); state_task1 = 1; task1_is_running = false; if (task2_is_running) state_task2 = 20; task2_is_maintain = false; break; } } /* else if (counter1s_task1 >= 10 && cur_temperature < 100 && gas_detection) //nếu trong 10s không đạt 100 độ thì TERMINATE { CONTROL(PIN.BEEP, ON); CONTROL(PIN.MOTO1_PWM, 0); CONTROL(PIN.FAN1_PWM, 0); CONTROL(PIN.MOTO2, OFF); CONTROL(PIN.FAN2, OFF); CONTROL(PIN.VAN, OFF); CONTROL(PIN.FIRE, OFF); MessageBox.Show("Gas chưa cháy!"); CONTROL(PIN.BEEP, OFF); state_task1 = 49; } */ break; case 15: counter1s_task1 = 0; update_status("Đang sấy...", true); btnStopSetLabel("sấy"); state_task1 = 16; break; case 16: // Duy trì nhiệt độ sấy trong 4 phút if (counter1s_task1 > T_say) { CONTROL(PIN.BEEP, ON); state_task1 = 19; break; } maintain(N_say); break; case 19: counter1s_task1 = 0; CONTROL(PIN.BEEP, OFF); update_status("Đang rang...", true); btnStopSetLabel("rang"); state_task1 = 20; break; case 20: //Rang if (counter1s_task1 >= T_rang_temp) { CONTROL(PIN.BEEP, ON); Timer1sSetEnable(false); timer1s.Stop(); state_task1 = 30; break; } maintain(N_rang); break; case 30://Confirm rang tiếp task2_is_maintain = true; using (ConfirmContinue form = new ConfirmContinue()) { form.ShowDialog(); string strRet = form.MyString; if (strRet != "0") { T_rang_temp += Convert.ToUInt32(strRet); state_task1 = 20; Timer1sSetEnable(true); } else { counter1s_task1 = 0; state_task1 = 39; Timer1sSetEnable(true); } CONTROL(PIN.BEEP, OFF); } task2_is_maintain = false; break; case 39:// Chạy task làm nguội task2_is_running = true; BTNNguoiSetEnable(true); CONTROL(PIN.BEEP, ON); CONTROL(PIN.VAN, OFF); CONTROL(PIN.FIRE, OFF); MessageBoxEx.Show("Rang hoàn tất!","Thông báo",600); update_status("Đang làm nguội...", true); CONTROL(PIN.BEEP, OFF); counter1s_task1 = 0; state_task1 = 49; break; /*OLD version case 40://Làm nguội if (counter1s_task1 >= T_nguoi)// thời gian làm nguội state_task1 = 41; break; case 41: Timer1sSetEnable(false); CONTROL(PIN.BEEP, ON); CONTROL(PIN.MOTO1_PWM, 0); CONTROL(PIN.FAN1_PWM, 0); CONTROL(PIN.MOTO2, OFF); CONTROL(PIN.FAN2, OFF); MessageBox.Show("Làm nguội hoàn tất!"); state_task1 = 49; break; */ case 49: //TERMINATING: //show_time = false; btnStartSetEnable(true); update_status("IDLE", true); TrackBarSetEnable(false); btnStopSetEnable(false); CONTROL(PIN.BEEP, ON); CONTROL(PIN.MOTO1_PWM, 0); CONTROL(PIN.FAN1_PWM, 0); CONTROL(PIN.VAN, OFF); CONTROL(PIN.FIRE, OFF); CONTROL(PIN.BEEP, OFF); state_task1 = 50; break; case 50: //TERMINATED counter1s_task1 = 0; state_task1 = 51; update_status("Đang đổ cafe ra...", true); CONTROL(PIN.BEEP, OFF); break; case 51: //đợi đổ hết cafe ra: 10 giây if (counter1s_task1 > 10) { state_task1 = 1; CONTROL(PIN.BEEP, ON); // AutoClosingMessageBox.Show("Text", "Caption", 1000); MessageBoxEx.Show("Bắt đầu quá trình mới", "Thông báo", 2000); } break; default: break; } } catch (Exception) { } } } }
private void run() { while (true) { // if(serial_port.IsOpen) serial_port.Write("read\r\n"); Tsay = T0; Thread.Sleep(100); while (loop) { Thread.Sleep(100); try { switch (state) { case 1: update_status("START", true); CONTROL(PIN.BEEP, ON); CONTROL(PIN.MOTO1_PWM, 100); CONTROL(PIN.FAN1_PWM, 100); CONTROL(PIN.VAN, ON); CONTROL(PIN.FIRE, ON); CONTROL(PIN.MOTO2, OFF); CONTROL(PIN.FAN2, OFF); CONTROL(PIN.BEEP, OFF); btnStartSetEnable(false); btnStopSetEnable(true); btnStopSetLabel("check"); state = 10; counter1s = 0; serial_port.Write("sync\r\n"); break; case 10: //Check nhiệt độ update_status("Kiểm tra gas và nhiệt độ...", true); if (cur_temperature >= Nmin) { CONTROL(PIN.BEEP, ON); counter1s = 0; update_status("Xin đổ cafe vào!", true); if (MessageBox.Show("Xin đổ cafe vào!", "Continue", MessageBoxButtons.OKCancel) == DialogResult.OK) { CONTROL(PIN.BEEP, OFF); state = 19; break; } else { CONTROL(PIN.BEEP, OFF); MessageBox.Show("Terminated!"); state = 49; break; } } else if (counter1s >= 10 && cur_temperature < 100 && gas_detection) //nếu trong 10s không đạt 100 độ thì TERMINATE { CONTROL(PIN.BEEP, ON); CONTROL(PIN.MOTO1_PWM, 0); CONTROL(PIN.FAN1_PWM, 0); CONTROL(PIN.MOTO2, OFF); CONTROL(PIN.FAN2, OFF); CONTROL(PIN.VAN, OFF); CONTROL(PIN.FIRE, OFF); MessageBox.Show("Gas chưa cháy!"); CONTROL(PIN.BEEP, OFF); state = 49; } break; case 19: counter1s = 0; update_status("Đang sấy...", true); btnStopSetLabel("sấy"); state = 20; break; case 20: //Sấy if (counter1s >= Tsay) { CONTROL(PIN.BEEP, ON); Timer1sSetEnable(false); timer1s.Stop(); state = 30; break; } drying(); break; case 30://Confirm sấy tiếp using (ConfirmContinue form = new ConfirmContinue()) { form.ShowDialog(); string strRet = form.MyString; if (strRet != "0") { Tsay += Convert.ToUInt32(strRet); state = 20; Timer1sSetEnable(true); } else { counter1s = 0; state = 39; Timer1sSetEnable(true); } CONTROL(PIN.BEEP, OFF); } break; case 39: Timer1sSetEnable(false); CONTROL(PIN.BEEP, ON); CONTROL(PIN.MOTO2, ON); CONTROL(PIN.FAN2, ON); CONTROL(PIN.VAN, OFF); CONTROL(PIN.FIRE, OFF); update_status("Đang làm nguội...", true); MessageBox.Show("Sấy hoàn tất!"); btnStopSetLabel("làm nguội"); CONTROL(PIN.BEEP, OFF); Timer1sSetEnable(true); counter1s = 0; state = 40; break; case 40://Làm nguội if (counter1s >= T1)// thời gian làm nguội state = 41; break; case 41: Timer1sSetEnable(false); CONTROL(PIN.BEEP, ON); CONTROL(PIN.MOTO1_PWM, 0); CONTROL(PIN.FAN1_PWM, 0); CONTROL(PIN.MOTO2, OFF); CONTROL(PIN.FAN2, OFF); MessageBox.Show("Làm nguội hoàn tất!"); state = 49; break; case 49: //TERMINATING: Timer1sSetEnable(false); btnStopSetEnable(false); btnStartSetEnable(true); update_status("IDLE", true); TrackBarSetEnable(false); CONTROL(PIN.BEEP, ON); CONTROL(PIN.MOTO1_PWM, 0); CONTROL(PIN.FAN1_PWM, 0); CONTROL(PIN.VAN, OFF); CONTROL(PIN.FIRE, OFF); CONTROL(PIN.MOTO2, OFF); CONTROL(PIN.FAN2, OFF); CONTROL(PIN.BEEP, OFF); state = 50; break; case 50: //TERMINATED //Do nothing CONTROL(PIN.BEEP, OFF); state = 1; loop = false; break; default: break; } } catch (Exception) { } } } }