From 4fb41ef464d46f0eef6d34228630a53664615a68 Mon Sep 17 00:00:00 2001 From: JbLb Date: Sat, 8 Feb 2020 23:28:47 +0100 Subject: [PATCH] non blocking led animation --- hoverserial.ino | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/hoverserial.ino b/hoverserial.ino index c9d18f4..c2e9a6f 100644 --- a/hoverserial.ino +++ b/hoverserial.ino @@ -28,7 +28,7 @@ Nunchuk nchuk(Wire2); // ########################## DEFINES ########################## #define LED_BUILTIN PC13 -#define TIME_SEND 20 // [ms] poolling time interval +#define TIME_SEND 100 // [ms] time interval #define HOVER_SERIAL_BAUD 38400 // [-] Baud rate for HoverSerial (used to communicate with the hoverboard) #define START_FRAME 0xAAAA // [-] Start frme definition for reliable serial communication #define SCREEN_WIDTH 128 // OLED display width, in pixels @@ -107,9 +107,10 @@ void setup() Serial.begin(115200); Serial1.begin(HOVER_SERIAL_BAUD); - + strip.begin(); // INITIALIZE NeoPixel strip object - strip.show(); // Turn OFF all pixels ASAP + strip.clear(); + strip.show(); // Turn OFF all pixels ASAP strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255) while (!Serial) @@ -280,13 +281,29 @@ void Nunchuk_control() // ########################## LOOP ########################## unsigned long iTimeSend = 0; +int idx_led = 0; void loop() { unsigned long timeNow = millis(); + // Blink the LED digitalWrite(LED_BUILTIN, (timeNow % 2000) < 1000); Receive(); Nunchuk_control(); - Serial.println(timeNow); + + if (iTimeSend > timeNow) + return; + iTimeSend = timeNow + TIME_SEND; + strip.clear(); + strip.setPixelColor(idx_led, strip.Color(128, 0, 0)); + if (idx_led < strip.numPixels() -1 ) + { + idx_led++; + } + else + { + idx_led = 0; + } + strip.show(); }