【发布时间】:2019-11-04 06:23:26
【问题描述】:
我使用 Neopixels(64 个 LED),并且我有一个名为 level_up 的函数,每次都会获得不同的 led_num。一般是水平条; level[1] 将按从 0 到 28 的顺序点亮所有 LED,level[2] 将点亮从 29 到 48 的所有 LED,依此类推。 我附加的功能工作正常,但我需要将延迟更改为 millis() 并且不知道如何。有什么想法吗?
uint8_t level[] = {0, 28, 48, 60, 64}; //levels 0 to 4
void level_up(uint8_t wait, uint8_t led_num) {
uint8_t start_point;
if (led_num == level[1]) start_point = 0; //up from level 0 to 1
if (led_num == level[2]) start_point = 28; //up from level 1 to 2
if (led_num == level[3]) start_point = 48; //up from level 2 to 3
if (led_num == level[4]) start_point = 60; //...
for (uint8_t i = start_point; i < led_num; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 255));
strip.show();
delay(wait); //TODO: change it to timer
}
}
void loop() {
if (plus_btn.pressed()) {
score++;
if (score >= 4) {
score = 4;
}
}
if (minus_btn.pressed()) {
score--;
if (score <= 0) {
score = 0;
}
}
switch (score) {
case 0:
if (last_score == 1) level_down(50, level[0]);
last_score = 0;
break;
case 1:
// if last_score was 0 make the blue effect because level is up
if (last_score == 0) level_up(50, level[1]);
// if last_score was 2 make the red effect because level is down
if (last_score == 2) level_down(50, level[1]);
last_score = 1;
break;
case 2:
if (last_score == 1) level_up(50, level[2]);
if (last_score == 3) level_down(50, level[2]);
last_score = 2;
break;
case 3:
if (last_score == 2) level_up(50, level[3]);
if (last_score == 4) level_down(50, level[3]);
last_score = 3;
break;
case 4:
winning_timer.start();
winning();
digitalWrite(WINNING_SENSOR_PIN, HIGH);
break;
}
Serial.println(score);
}
【问题讨论】:
-
delay()中的参数是毫秒。 -
... 和
delay()s 参数是unsigned long。您的wait变量仅允许255 ms长时间延迟。 -
我知道;我不需要更多:) 我的问题是关于更改此函数以使用 millis()。
-
millis()仅报告自您开始以来的毫秒数。你想用它做什么? -
我需要这个函数与 Arduino 执行的其他操作一起进行多任务处理。延迟会停止处理器,而 millis() 或计时器则不会。