diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -28,6 +28,7 @@ enum state { STATE_SETP, STATE_SETI, STATE_SETD, + STATE_SETWINDUP, STATE_PREHEAT_BREW, STATE_MAINTAIN_BREW, @@ -202,8 +203,9 @@ int32_t setpoint = 0; uint16_t k_p = 1; uint16_t k_i = 1; uint16_t k_d = 1; +uint16_t windup_guard = 1; int16_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD - +uint8_t pid_enabled = 0; // Process things void process() @@ -221,10 +223,17 @@ void process() // Every 200ms, set the SSR on unless output is 0 if((ticks - last_ssr_on > SSR_PERIOD)) { - // Get ssr output for next time - int16_t power_percent = update_pid(k_p, k_i, k_d, temp, temp_frac, setpoint); - //power-percent is 0-1000 - ssr_output = power_percent; //(((uint32_t)SSR_PERIOD * (uint32_t)10 * (uint32_t)100) * power_percent) / (uint32_t)1000000; + if(pid_enabled) + { + // Get ssr output for next time + int16_t power_percent = update_pid(k_p, k_i, k_d, temp, temp_frac, setpoint); + //power-percent is 0-1000 + ssr_output = power_percent; //(((uint32_t)SSR_PERIOD * (uint32_t)10 * (uint32_t)100) * power_percent) / (uint32_t)1000000; + } + else + { + ssr_output = 0; + } // Only support heating (ssr_output > 0) right now if(ssr_output > 0) { @@ -292,11 +301,12 @@ void machine() // Write text to OLED // [ therm :: idle ] ssd1306_DrawString("therm :: idle ", 0, 40); + pid_enabled = 0; char tempstr[6]; itoa_fp(temp, temp_frac, tempstr); ssd1306_DrawString("Temp: ", 3, 40); - ssd1306_DrawString(" ", 3, 70); + ssd1306_DrawString(" ", 3, 72); ssd1306_DrawString(tempstr, 3, 72); ssd1306_drawlogo(); @@ -309,12 +319,12 @@ void machine() case 1: { - ssd1306_DrawString("-> set P/I/D", 1, 40); + ssd1306_DrawString("-> setup ", 1, 40); } break; case 0: { - ssd1306_DrawString("-> setup ", 1, 40); + ssd1306_DrawString("-> reset ", 1, 40); } break; } @@ -328,7 +338,7 @@ void machine() state = STATE_SETP; break; case 0: - state = STATE_SETP; + state = STATE_IDLE; break; default: state = STATE_PREHEAT_BREW; @@ -430,7 +440,7 @@ void machine() // Button handler if(SW_BTN_PRESSED) { - state = STATE_IDLE; + state = STATE_SETWINDUP; } else if(!GPIO_ReadInputDataBit(SW_UP)) { k_d++; @@ -444,6 +454,41 @@ void machine() } break; + case STATE_SETWINDUP: + { + // Write text to OLED + // [ therm :: set windup ] + // [ g = 12 ] + ssd1306_DrawString("Windup Guard", 0, 40); + ssd1306_drawlogo(); + + char tempstr[6]; + itoa(windup_guard, tempstr); + ssd1306_DrawString("G=", 1, 45); + ssd1306_DrawString(" ", 1, 57); + ssd1306_DrawString(tempstr, 1, 57); + + ssd1306_DrawString("Press to accept", 3, 40); + + // Button handler + if(SW_BTN_PRESSED) { + state = STATE_IDLE; + } + else if(!GPIO_ReadInputDataBit(SW_UP)) { + windup_guard++; + } + else if(!GPIO_ReadInputDataBit(SW_DOWN) && windup_guard > 0) { + windup_guard--; + } + + // Event Handler + // N/A + + } break; + + + + case STATE_PREHEAT_BREW: { // Write text to OLED @@ -452,6 +497,7 @@ void machine() ssd1306_DrawString("Preheating...", 0, 40); ssd1306_drawlogo(); draw_setpoint(); + pid_enabled = 1; // Button handler if(SW_BTN_PRESSED) { @@ -480,6 +526,7 @@ void machine() ssd1306_DrawString("Ready to Brew!", 0, 40); ssd1306_drawlogo(); draw_setpoint(); + pid_enabled = 1; // Button handler if(SW_BTN_PRESSED) { @@ -506,6 +553,7 @@ void machine() ssd1306_DrawString("Preheating...", 0, 40); ssd1306_drawlogo(); draw_setpoint(); + pid_enabled = 1; // Button handler if(SW_BTN_PRESSED) { @@ -534,6 +582,7 @@ void machine() ssd1306_DrawString("Ready to Steam!", 0, 40); ssd1306_drawlogo(); draw_setpoint(); + pid_enabled = 1; // Button handler if(SW_BTN_PRESSED) { @@ -556,6 +605,7 @@ void machine() default: { state = STATE_IDLE; + pid_enabled = 0; } break;