Will millis work after a button?

Mods: sorry if this is too similar a topic. It feels like a totally different topic to me, but if it is not I'm sorry and hopefully you don't mind merging it

I'm trying to figure this one out but haven't gotten far enough to post a readable code. I'm not looking for anyone to solve my code for me, more so looking to see if my idea is even possible.

I made a light show into a function called redbluegreen. Said function uses the millis timer principle.

I want this light show to be selected after a button.

Question is: If the millis program requires multiple loops through the function to work properly, will it work behind a button that is only pushed once?

My gut feeling is no, but I thought I would check here before giving up.

Should I be looking at a state machine instead? The important part is that it is selected with a button as there eventually will be multiple light shows. It works with delay but I am hoping not to use that for the usual reasons.

Thanks.

Please reword this.


Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

The millis() function returns the time after power on in millisseconds as needed.
That is all.

Basically your code is wrong. You can do this if your code is written correctly.

Without seeing your code we can't tell you what you are getting wrong.

Perhaps I worded that wrong.. I mean if the function requires multiple loops... I'll post the code but it's not finished and there is a lot of extra stuff in there from trying to figure out how to make it work.

If you look at the case 1: redbluegreen function, it works on it's own in a loop, but does not work when placed in a setup section of the code when testing it in a test sketch. This is why I think it won't work after a button in this code.
case 2 & 3 work (not yet made into functions but I could) but it uses the delay which I am trying to get away from.

it is a LOLIN WEMOS C3 mini with RGB LED shield

/*
  LED

  This example creates a Bluetooth® Low Energy peripheral with service that contains a
  characteristic to control an LED.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

  You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or
  nRF Connect (Android), to interact with the services and characteristics
  created in this sketch.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>
#include <Adafruit_NeoPixel.h>

//#define PIXEL_PIN=ledPin
#define PIXEL_COUNT 7
const int ledPin = 6;  // pin to use for the LED

const long interval_1 = 5000;
const long interval_2 = 5000;
const long interval_3 = 5000;
const long interval_4 = 4000;
const long interval_5 = 5000;
const long interval_6 = 6000;


unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;

unsigned long currentTime = millis();
unsigned long currentTime2 = millis();


// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, ledPin, NEO_GRB + NEO_KHZ800);




BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214");  // Bluetooth® Low Energy LED Service

// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);



void setup() {
  Serial.begin(9600);
  while (!Serial)
    ;

  strip.begin();             // Initialize NeoPixel strip object (REQUIRED)
  strip.setBrightness(255);  //set LED brightness 0-255
  strip.show();              // Initialize all pixels to 'off'
  Serial.println("Initialized LEDs");


  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");

    while (1)
      ;
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("LED");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for Bluetooth® Low Energy peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        switch (switchCharacteristic.value()) {  // any value other than 0
          case 01:
            // task 1
            { redbluegreen(); }
        }
      }

      case 02:
        /*currentTime = millis();
            Serial.println("Setting current time");*/
        // task 2
        if ((currentTime - previousTime_2) >= interval_2) {
          Serial.println("Blue LED on");
          colorWipe(strip.Color(0, 0, 255), 0);  // blue
          delay(1000);
          colorWipe(strip.Color(0, 0, 0), 0);  // OFF
          delay(0);
          Serial.println("Green LED off");
          break;
          previousTime_2 = currentTime;
          Serial.println("Resetting current time");
        }
      case 03:
        Serial.println("Green LED on");
        colorWipe(strip.Color(0, 255, 0), 0);  // green
        delay(1000);
        colorWipe(strip.Color(0, 0, 0), 0);  // OFF
        delay(0);
        Serial.println("Green LED off");
        break;
      default:
        Serial.println(F("LEDs off"));
        colorWipe(strip.Color(0, 0, 0), 0);  // OFF
        delay(0);
        break;
    }
  }




  /*if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {  // any value other than 0
          Serial.println("LED on");
          colorWipe(strip.Color(0, 0, 255), 0);  // blue
          delay(1000);
          colorWipe(strip.Color(0, 204, 204), 300);  // teal
          delay(1000);
          colorWipe(strip.Color(127, 0, 255), 300);  // purple
          delay(1000);
          colorWipe(strip.Color(0, 0, 255), 300);  // blue
          delay(1000);
          colorWipe(strip.Color(255, 255, 255), 300);  // White
          delay(0);
          colorWipe(strip.Color(0, 0, 0), 0);  // OFF
          delay(0);
          Serial.println("LED lights off");
        */





  else {  // a 0 value
    Serial.println(F("LED off"));
    colorWipe(strip.Color(0, 0, 0), 0);  // OFF
    delay(0);
  }


  // when the central disconnects, print it out:
  Serial.print(F("Disconnected from central: "));
  Serial.println(central.address());
}



void colorWipe(uint32_t color, int wait) {
  for (int i = 0; i < strip.numPixels(); i++) {  // For each pixel in strip...
    strip.setPixelColor(i, color);               //  Set pixel's color (in RAM)
    strip.show();                                //  Update strip to match
    delay(wait);                                 //  Pause for a moment
  }
}


void redbluegreen() {
  currentTime = millis();
  Serial.println("Setting current time");
  if ((currentTime - previousTime_1) >= interval_1)
    Serial.println("checking interval");
  {
    colorWipe(strip.Color(255, 0, 0), 0);  // red
    Serial.println("Red LED on");
    previousTime_1 = currentTime;
    Serial.println("resetting current time");
  }
  if ((currentTime - previousTime_1) >= interval_2)
    Serial.println("checking interval");
  {
    colorWipe(strip.Color(0, 0, 255), 0);  // blue
    Serial.println("Blue LED on");
    previousTime_1 = currentTime;
    Serial.println("resetting current time");
  }
  if ((currentTime - previousTime_1) >= interval_3)
    Serial.println("checking interval");
  {
    colorWipe(strip.Color(0, 255, 0), 0);  // green
    Serial.println("Green LED on");
    previousTime_1 = currentTime;
    Serial.println("resetting current time");
  }
}

Updated code. Off to work now but I'll check back this evening.

Case 1: LED goes blue for about 500 millisec each
Case 2 & 3: work as expected

thanks

/*
  LED

  This example creates a Bluetooth® Low Energy peripheral with service that contains a
  characteristic to control an LED.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

  You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or
  nRF Connect (Android), to interact with the services and characteristics
  created in this sketch.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>
#include <Adafruit_NeoPixel.h>

//#define PIXEL_PIN=ledPin
#define PIXEL_COUNT 7
const int ledPin = 6;  // pin to use for the LED

const long interval_1 = 5000;
const long interval_2 = 5000;
const long interval_3 = 5000;
const long interval_4 = 4000;
const long interval_5 = 5000;
const long interval_6 = 6000;


unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;

unsigned long currentTime = millis();
unsigned long currentTime2 = millis();


// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, ledPin, NEO_GRB + NEO_KHZ800);




BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214");  // Bluetooth® Low Energy LED Service

// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);



void setup() {
  Serial.begin(9600);
  while (!Serial)
    ;

  strip.begin();             // Initialize NeoPixel strip object (REQUIRED)
  strip.setBrightness(255);  //set LED brightness 0-255
  strip.show();              // Initialize all pixels to 'off'
  Serial.println("Initialized LEDs");


  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");

    while (1)
      ;
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("LED");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for Bluetooth® Low Energy peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        switch (switchCharacteristic.value()) {  // any value other than 0
          case 01:
            // task 1
            redbluegreen();



          case 02:
            /*currentTime = millis();
            Serial.println("Setting current time");*/
            // task 2
            if ((currentTime - previousTime_2) >= interval_2) {
              Serial.println("Blue LED on");
              colorWipe(strip.Color(0, 0, 255), 0);  // blue
              delay(1000);
              colorWipe(strip.Color(0, 0, 0), 0);  // OFF
              delay(0);
              Serial.println("Blue LED off");
              break;
              previousTime_2 = currentTime;
              Serial.println("Resetting current time");
            }
          case 03:
            Serial.println("Green LED on");
            colorWipe(strip.Color(0, 255, 0), 0);  // green
            delay(1000);
            colorWipe(strip.Color(0, 0, 0), 0);  // OFF
            delay(0);
            Serial.println("Green LED off");
            break;
          default:
            Serial.println(F("LEDs off"));
            colorWipe(strip.Color(0, 0, 0), 0);  // OFF
            delay(0);
            break;
        }
      }
    }
  }




  /*if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {  // any value other than 0
          Serial.println("LED on");
          colorWipe(strip.Color(0, 0, 255), 0);  // blue
          delay(1000);
          colorWipe(strip.Color(0, 204, 204), 300);  // teal
          delay(1000);
          colorWipe(strip.Color(127, 0, 255), 300);  // purple
          delay(1000);
          colorWipe(strip.Color(0, 0, 255), 300);  // blue
          delay(1000);
          colorWipe(strip.Color(255, 255, 255), 300);  // White
          delay(0);
          colorWipe(strip.Color(0, 0, 0), 0);  // OFF
          delay(0);
          Serial.println("LED lights off");
        */





  else {  // a 0 value
    Serial.println(F("LED off"));
    colorWipe(strip.Color(0, 0, 0), 0);  // OFF
    delay(0);
  }


  // when the central disconnects, print it out:
  Serial.print(F("Disconnected from central: "));
  Serial.println(central.address());
}



void colorWipe(uint32_t color, int wait) {
  for (int i = 0; i < strip.numPixels(); i++) {  // For each pixel in strip...
    strip.setPixelColor(i, color);               //  Set pixel's color (in RAM)
    strip.show();                                //  Update strip to match
    delay(wait);                                 //  Pause for a moment
  }
}


void redbluegreen() {
  currentTime = millis();
  Serial.println("Setting current time");
  if ((currentTime - previousTime_1) >= interval_1)
    Serial.println("checking interval");
  {
    colorWipe(strip.Color(255, 0, 0), 0);  // red
    Serial.println("Red LED on");
    previousTime_1 = currentTime;
    Serial.println("resetting current time");
  }
  if ((currentTime - previousTime_1) >= interval_2)
    Serial.println("checking interval");
  {
    colorWipe(strip.Color(0, 0, 255), 0);  // blue
    Serial.println("Blue LED on");
    previousTime_1 = currentTime;
    Serial.println("resetting current time");
  }
  if ((currentTime - previousTime_1) >= interval_3)
    Serial.println("checking interval");
  {
    colorWipe(strip.Color(0, 255, 0), 0);  // green
    Serial.println("Green LED on");
    previousTime_1 = currentTime;
    Serial.println("resetting current time");
  }
}

consider

case 02:
            /*currentTime = millis();
            Serial.println("Setting current time");*/
            // task 2
            if ((currentTime - previousTime_2) >= interval_2) {
              Serial.println("Blue LED on");
              colorWipe(strip.Color(0, 0, 255), 0);  // blue
              delay(1000);
              colorWipe(strip.Color(0, 0, 0), 0);  // OFF
              delay(0);
              Serial.println("Blue LED off");
              break;
              previousTime_2 = currentTime;
              Serial.println("Resetting current time");
            }

How does this

previousTime_2 = currentTime;
              Serial.println("Resetting current time");

run after the break statement?

Consider your break statement is enclosed in a If statement. If the If is not satisfied then the break does not run so case3 is ran.

Where is

the break;?

switch (expression)  {
    case constant1:
        // code to be executed if 
        // expression is equal to constant1;
        break;

    case constant2:
        // code to be executed if
        // expression is equal to constant2;
        break;
        .
        .
        .
    default:
        // code to be executed if
        // expression doesn't match any constant
}

After fixing your code post the fixed code in a new post so we can get to the other issues.

Basically you have not "got" the way to use millis, in any way shape or form.

I think you need to read some tutorials.

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0

The basic idea, it to eliminate ALL delays, and have all for and while loops "unwrapped". That means that you only do one literation of a for loop before returning.

At the start of the loop function you check what tasks need doing with the current value you get from the millis() counter. You then go and do that task. The task can even control the next time it gets called by setting up the length of time needed before the task gets called again. This is the way you "replace" the delay call.

It is a whole new way of programming and you can't just drop in a millis() time anywhere, you have to think about it.

Most of the simple examples will involve blinking lights, but those are just a simple task that you can build on.

Forget Bluetooth for the moment, just concentrate in getting the tasks working correctly.

Thanks @Idahowalker and @Grumpy_Mike ,

The only case I was trying to use millis with was the redbluegreen function under case 1. I know that you don't use delay in it at all. Case 2 and 3 were leftovers from messing around before. That's why I didn't post the code originally because I knew it looked wrong. I am able to use millis successfully when I run the redbluegreen function in its own sketch. The problem comes when putting it after a button in a case situation.

I seem to be getting conflicting answers... whether or not it will work behind a button without having to push the button more than once. It wouldn't matter if the button is bluetooth or hardware.

Back to my original question (and here is where I can see you saying I'm not understanding millis which I would agree with).... when placed behind a button, will millis run multiple steps if each step has a significant difference in time?
ie:
red 5 mins
blue 5 mins after red starts
green 5 mins after blue starts

My understanding (and here is where I may not understand) is that to do this, the arduino would have to do multiple loops of the code in order to detect when the 5 minutes has passed. However, a button would not allow it to do that, as the button press will only activate the code 1 time, not in the loop that is needed to detect the long changes in time.

Therefore I'm wondering if a statemachine would be a smarter choice? I have successfully done one of these (in a sketch saved somewhere), I think I would need to find a way to simplify the code to make it reasonable to change the code relatively often for different lights.

Updated code:

/*
  LED

  This example creates a Bluetooth® Low Energy peripheral with service that contains a
  characteristic to control an LED.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

  You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or
  nRF Connect (Android), to interact with the services and characteristics
  created in this sketch.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>
#include <Adafruit_NeoPixel.h>

//#define PIXEL_PIN=ledPin
#define PIXEL_COUNT 7
const int ledPin = 6;  // pin to use for the LED

const long interval_1 = 5000;
const long interval_2 = 5000;
const long interval_3 = 5000;
const long interval_4 = 4000;
const long interval_5 = 5000;
const long interval_6 = 6000;


unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;

unsigned long currentTime = millis();
unsigned long currentTime2 = millis();


// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, ledPin, NEO_GRB + NEO_KHZ800);




BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214");  // Bluetooth® Low Energy LED Service

// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);



void setup() {
  Serial.begin(9600);
  while (!Serial)
    ;

  strip.begin();             // Initialize NeoPixel strip object (REQUIRED)
  strip.setBrightness(255);  //set LED brightness 0-255
  strip.show();              // Initialize all pixels to 'off'
  Serial.println("Initialized LEDs");


  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");

    while (1)
      ;
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("LED");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for Bluetooth® Low Energy peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        switch (switchCharacteristic.value()) {  // any value other than 0
          case 01:
            // task 1
            redbluegreen();
            break;


          case 02:  //this is not meant to be a millis code
            /*currentTime = millis();
            Serial.println("Setting current time");*/
            // task 2
            if ((currentTime - previousTime_2) >= interval_2) {
              Serial.println("Blue LED on");
              colorWipe(strip.Color(0, 0, 255), 0);  // blue
              delay(1000);
              colorWipe(strip.Color(0, 0, 0), 0);  // OFF
              delay(0);
              Serial.println("Blue LED off");
              //break;
              previousTime_2 = currentTime;
              Serial.println("Resetting current time");
            }
            break;
          case 03:   //this is not meant to be a millis code
            Serial.println("Green LED on");
            colorWipe(strip.Color(0, 255, 0), 0);  // green
            delay(1000);
            colorWipe(strip.Color(0, 0, 0), 0);  // OFF
            delay(0);
            Serial.println("Green LED off");
            break;
          default:
            Serial.println(F("LEDs off"));
            colorWipe(strip.Color(0, 0, 0), 0);  // OFF
            delay(0);
            break;
        }
      }
    }
  }




  /*if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {  // any value other than 0
          Serial.println("LED on");
          colorWipe(strip.Color(0, 0, 255), 0);  // blue
          delay(1000);
          colorWipe(strip.Color(0, 204, 204), 300);  // teal
          delay(1000);
          colorWipe(strip.Color(127, 0, 255), 300);  // purple
          delay(1000);
          colorWipe(strip.Color(0, 0, 255), 300);  // blue
          delay(1000);
          colorWipe(strip.Color(255, 255, 255), 300);  // White
          delay(0);
          colorWipe(strip.Color(0, 0, 0), 0);  // OFF
          delay(0);
          Serial.println("LED lights off");
        */





  else {  // a 0 value
    Serial.println(F("LED off"));
    colorWipe(strip.Color(0, 0, 0), 0);  // OFF
    delay(0);
  }


  // when the central disconnects, print it out:
  Serial.print(F("Disconnected from central: "));
  Serial.println(central.address());
}



void colorWipe(uint32_t color, int wait) {
  for (int i = 0; i < strip.numPixels(); i++) {  // For each pixel in strip...
    strip.setPixelColor(i, color);               //  Set pixel's color (in RAM)
    strip.show();                                //  Update strip to match
    delay(wait);                                 //  Pause for a moment
  }
}


void redbluegreen() {
  currentTime = millis();
  Serial.println("Setting current time");
  if ((currentTime - previousTime_1) >= interval_1)
    Serial.println("checking interval");
  {
    colorWipe(strip.Color(255, 0, 0), 0);  // red
    Serial.println("Red LED on");
    previousTime_1 = currentTime;
    Serial.println("resetting current time");
  }
  if ((currentTime - previousTime_1) >= interval_2)
    Serial.println("checking interval");
  {
    colorWipe(strip.Color(0, 0, 255), 0);  // blue
    Serial.println("Blue LED on");
    previousTime_1 = currentTime;
    Serial.println("resetting current time");
  }
  if ((currentTime - previousTime_1) >= interval_3)
    Serial.println("checking interval");
  {
    colorWipe(strip.Color(0, 255, 0), 0);  // green
    Serial.println("Green LED on");
    previousTime_1 = currentTime;
    Serial.println("resetting current time");
  }
}

thanks

@Grumpy_Mike I am reading your tutorials now

I think what you are missing up is that ALL code must be non blocking.

So in your example you are trying to access a function, colourWipe which itself is blocking. There is no point in writing this top level function in a none blocking way, if you only go and call a blocking function. I think this goes to the heart of your confusion.

This is blocking code that itself needs to be written as none blocking code, before you try and put other code above it.

I have posted none blocking code for all the standard example effects quite a few times in the past. On my iPad at the moment so I don't have access to that code but search the forum for it.

Like I said get rid of all the Bluetooth stuff until you are ready for it.

thanks. That actually makes a lot more sense. If the function itself is blocking then of course the whole code will become blocking!! :face_with_monocle:
My brain needs a rest tonight but hopefully I can look at this again tomorrow. I've read through your first example and I was working my way through your second when my brain said enough.
Thanks

You made this mistake several times: in the above, the only line that is dependant on the if condition is that first Serial.println(). The code lines inside the { } will be executed every time, they don't depend on the if condition.

@EQL
I have just created a tutorial using a technique that will help you get what you want without having to rewrite all the pattern stuff as a state machine.

Check this out
Commutable Delay

wicked! Thank you :smiley: I'll check it out tonight.

Hi @Grumpy_Mike I had a second to look at the tutorial. It looks great! Now I haven't read it thoroughly as I would like (I have to go to work soon) nor tried it yet, but will it work when the delay is like an hour? For example, a sleep trainer that is 6 hrs green then 1 hr yellow then ends on purple? If it explains in the tutorial please don't feel obligated to re-explain. If you could just say something like yes - read the tutorial that would be fantastic!
thanks again for the tutorial. I think MANY people will find that helpful!

Yes it should as the delay is an unsigned integer which maxes out at a number of 4294967295.

So that 4294967295 / 1000 ms in 1 second / 60 seconds in a min / 60 seconds in an hour / 24 hours in a day means the maximum delay is 49.7 days, which is the maximum the millis counter works to.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.