Skip to content

Commit e7d0fac

Browse files
committed
Fix: Execute state code every 100 ms, instead of a 100 ms delay, which delays by 100 ms PLUS code execution time.
1 parent 671909c commit e7d0fac

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

examples/Braccio_Learn_and_Repeat/Braccio_Learn_and_Repeat.ino

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -148,47 +148,53 @@ void setup() {
148148

149149
void loop()
150150
{
151-
if (state == RECORD)
151+
/* Every 100 ms, which is the system sample rate, do ... */
152+
static auto prev = millis();
153+
auto const now = millis();
154+
if ((now - prev) >= 100)
152155
{
153-
/* Check if we still have space for samples. */
154-
if (sample_cnt >= MAX_SAMPLES) {
155-
state = ZERO_POSITION;
156-
Serial.println("ZERO_POSITION");
157-
Braccio.lvgl_lock();
158-
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
159-
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
160-
Braccio.lvgl_unlock();
156+
prev = now;
157+
158+
if (state == RECORD)
159+
{
160+
/* Check if we still have space for samples. */
161+
if (sample_cnt >= MAX_SAMPLES) {
162+
state = ZERO_POSITION;
163+
Serial.println("ZERO_POSITION");
164+
Braccio.lvgl_lock();
165+
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
166+
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
167+
Braccio.lvgl_unlock();
168+
}
169+
else
170+
{
171+
/* Capture those samples. */
172+
Braccio.positions(idx);
173+
idx += 6;
174+
sample_cnt += 6;
175+
}
161176
}
162-
else
177+
178+
if (state == REPLAY)
163179
{
164-
/* Capture those samples. */
165-
Braccio.positions(idx);
180+
Braccio.moveTo(idx[0], idx[1], idx[2], idx[3], idx[4], idx[5]);
166181
idx += 6;
167182
sample_cnt += 6;
183+
if (idx >= final_idx) {
184+
Serial.println("REPLAY done");
185+
state = ZERO_POSITION;
186+
Braccio.lvgl_lock();
187+
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
188+
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
189+
Braccio.lvgl_unlock();
190+
}
168191
}
169-
}
170192

171-
if (state == REPLAY)
172-
{
173-
Braccio.moveTo(idx[0], idx[1], idx[2], idx[3], idx[4], idx[5]);
174-
idx += 6;
175-
sample_cnt += 6;
176-
if (idx >= final_idx) {
177-
Serial.println("REPLAY done");
178-
state = ZERO_POSITION;
193+
if (state != ZERO_POSITION)
194+
{
179195
Braccio.lvgl_lock();
180-
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
181-
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
196+
lv_label_set_text_fmt(counter, "Counter: %d" , (sample_cnt / 6));
182197
Braccio.lvgl_unlock();
183198
}
184199
}
185-
186-
delay(100);
187-
188-
if (state != ZERO_POSITION)
189-
{
190-
Braccio.lvgl_lock();
191-
lv_label_set_text_fmt(counter, "Counter: %d" , (sample_cnt / 6));
192-
Braccio.lvgl_unlock();
193-
}
194200
}

0 commit comments

Comments
 (0)