diff --git a/application.c b/application.c index 62b4063..f1d649f 100644 --- a/application.c +++ b/application.c @@ -66,6 +66,8 @@ static uint32_t boot_time = 0; #define DRV8825_FAULT_PIN PB12 /**< pin pulled low on error (such as over-current) */ static volatile uint32_t drv8825_steps = 0; /**< incremented with each step */ static int8_t drv8825_direction = 0; /**< direction of the steps (1 = clockwise, -1 = counter-clockwise) */ +static uint32_t drv8825_goal = 0; /**< number of steps to reach */ +static bool drv8825_reached = false; /**< set when the goal is reached */ /** maximum speed (in steps/s) before the motor stalls (found empirically) * @note found empirically 300 @ 9V/180mA, 420 @ 12V/150mA */ @@ -1154,12 +1156,19 @@ void main(void) } } if (dial_steps) { // hour dial position detected - if (drv8825_steps >= DIAL_MIDNIGHT_STEPS) { // wait for dial to reach midnight - speed = 0; // stop motor - command_speed(&speed); // stop motor - dial_steps = 0; // restart position counter - puts("midnight reached\n"); - } + puts("dials homed\n"); + dial_steps = 0; // restart position counter (and clear flag) + drv8825_goal = DIAL_MIDNIGHT_STEPS; // go to midnight + drv8825_reached = false; // wait until it's reached + action = true; + } + if (drv8825_reached) { + puts("midnight reached\n"); + speed = 0; // stop motor + command_speed(&speed); // stop motor + drv8825_goal = 0; // disable goal + drv8825_reached = false; // clear flag + action = true; // redo main loop } if (radio_esp8266_received_len) { if (radio_esp8266_received_len >= 18 && 0 == memcmp((char*)radio_esp8266_received, "Art-Net", 7)) { @@ -1201,6 +1210,9 @@ void TIM_ISR(DRV8825_STEP_TIMER)(void) if (UINT32_MAX == drv8825_steps) { // underflow drv8825_steps = DIAL_CYCLE_STEPS; // use known circumference } + if (!drv8825_reached && drv8825_goal && drv8825_steps == drv8825_goal) { // we reached the set goal + drv8825_reached = true; // notify main loop + } } }