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