From ebd47ee2c58ace75896a4e64942dbbb654d55187 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Mallo Date: Mon, 13 Oct 2025 21:09:06 +0200 Subject: [PATCH] Use average temperature over 16 samples --- barista/barista.ino | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/barista/barista.ino b/barista/barista.ino index 553c34e..1d0c416 100644 --- a/barista/barista.ino +++ b/barista/barista.ino @@ -63,6 +63,8 @@ struct input { enum logic btn[MAX_BTN]; } g_in; +#define MAX_SAMPLES 16 + struct state { enum machine_state mstate; unsigned long brewing_t0; @@ -70,8 +72,11 @@ struct state { unsigned long heating_t0; unsigned long hot_t0; + int ntc_i; /* Next available place */ float ntc_R; - float ntc_T; + float ntc_last_T; + float ntc_array_T[MAX_SAMPLES]; + float ntc_T; /* average */ struct btn btn[MAX_BTN]; enum buzz_state buzz_state; @@ -114,7 +119,16 @@ void do_input(struct input *input) void proc_ntc(struct state *state, const struct input *input) { state->ntc_R = ntc_resistance(input->ntc_V); - state->ntc_T = ntc_temp(state->ntc_R); + state->ntc_last_T = ntc_temp(state->ntc_R); + state->ntc_array_T[state->ntc_i++] = state->ntc_last_T; + if (state->ntc_i >= MAX_SAMPLES) + state->ntc_i = 0; + + float avg = 0; + for (int i = 0; i < MAX_SAMPLES; i++) + avg += state->ntc_array_T[i]; + + state->ntc_T = avg / MAX_SAMPLES; } void proc_buttons(struct state *state, const struct input *input)