From 425ae4584c8b9500ae78da06bdfafd0f28431a80 Mon Sep 17 00:00:00 2001 From: Aran Vink Date: Sun, 28 Mar 2021 00:32:57 +0100 Subject: [PATCH] Remove unneeded delays, rewrite main loop in example --- AMBX.cpp | 2 -- examples/ambx/AMBX.ino | 52 +++++++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/AMBX.cpp b/AMBX.cpp index 4df787d3..6c31021a 100644 --- a/AMBX.cpp +++ b/AMBX.cpp @@ -218,8 +218,6 @@ void AMBX::Light_Command(uint8_t *data, uint16_t nbytes) { Notify(PSTR("\r\nLight command "), 0x80); #endif pUsb->outTransfer(bAddress, epInfo[ AMBX_OUTPUT_PIPE ].epAddr, nbytes, data); - //Do really short delay, I've noticed otherwise the controller will receive all command at once, and might not process all of them. - delay(1); } void AMBX::setLight(uint8_t ambx_light, uint8_t r, uint8_t g, uint8_t b) { diff --git a/examples/ambx/AMBX.ino b/examples/ambx/AMBX.ino index f7280e2c..e3e53087 100644 --- a/examples/ambx/AMBX.ino +++ b/examples/ambx/AMBX.ino @@ -13,8 +13,9 @@ USB Usb; AMBX AMBX(&Usb); // This will just create the instance -bool printAngle; uint8_t state = 0; +const long interval = 1000; +unsigned long previousMillis = 0; void setup() { Serial.begin(115200); @@ -27,29 +28,34 @@ void setup() { } Serial.print(F("\r\nAMBX USB Library Started")); } + void loop() { Usb.Task(); - - if (AMBX.AMBXConnected) { - if (state == 0) { - - } else if (state == 1) { - AMBX.setAllLights(Red); - } else if (state == 2) { - AMBX.setAllLights(Green); - } else if (state == 3) { - AMBX.setAllLights(Blue); - } else if (state == 4) { - AMBX.setAllLights(White); + + if (AMBX.AMBXConnected) { + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + + if (state > 4) { + state = 0; + } + if (state == 0) { + Serial.print(F("\r\nRed")); + AMBX.setAllLights(Red); + } else if (state == 1) { + Serial.print(F("\r\nGreen")); + AMBX.setAllLights(Green); + } else if (state == 2) { + Serial.print(F("\r\nBlue")); + AMBX.setAllLights(Blue); + } else if (state == 3) { + Serial.print(F("\r\nWhite")); + AMBX.setAllLights(White); + } + state++; + } } - - //Example using single light: - //AMBX.setLight(Wallwasher_center, White); - - state++; - if (state > 4) - state = 0; - delay(1000); - } - delay(10); + //Example using single light: + //AMBX.setLight(Wallwasher_center, White); }