RGB strips change color and stop on command

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
#include "certs.h"
#include <LEDStripDriver.h>

// DIN=GPIO16, CIN=GPIO14 
LEDStripDriver led = LEDStripDriver(16, 14);
char messageBuffer[50];

WiFiClient espClient;
PubSubClient client(espClient);

unsigned long currentTime;
unsigned long startTime;
unsigned long period = 1000;

bool power = false;
int lastledcolor;
void setup_wifi()
{
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("WiFi connected to ");
  Serial.println(ssid);
  Serial.print("IP address : ");
  Serial.println(WiFi.localIP());
  delay(2000);
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop()
{
  if (!client.connected())
  {
    reconnect();
  }
  client.loop();
  if (power == true)
  {
    Rainbow();
  }
  else
  stopRainbow();
}

void reconnect()
{
  while (!client.connected())
  {
    if (client.connect("Reception"))
    {
      Serial.println("connected");
      client.subscribe("RGB/Output");
    }
    else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again");
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length)
{
  //Serial.println();
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.print("length : ");
  Serial.println(length);
  memcpy(messageBuffer, payload, length);
  messageBuffer[length] = '\0';
  Serial.print("the payload is : ");
  Serial.println(messageBuffer);
  if (strcmp(messageBuffer, "change") == 0)
  {
      Serial.print("turning ");
      Serial.println(strcmp(messageBuffer, "change"));
      digitalWrite(LED_BUILTIN, HIGH);
      power = true; //rainbow  
  }
  else if (strcmp(messageBuffer, "freeze") == 0)
  {
      Serial.print("turning ");
      Serial.println(strcmp(messageBuffer, "freeze"));
      digitalWrite(LED_BUILTIN, LOW);
      power = false;  //stop at last color
    }
  }

void stopRainbow()
{
  switch (lastledcolor) {
case 0: //red
  led.setColor(255,0,0);
case 1: //green
  led.setColor(0,255,0);
case 2: //blue
  led.setColor(0,0,255);
case 3: //yellow
  led.setColor(255,255,0);
case 4: //cyan
  led.setColor(0,255,255);
case 5: //magenta
  led.setColor(139,0,139);
case 6: //orange
  led.setColor(255,69,0);
case 7: //pink
  led.setColor(255,20,147);
}//one color
}

void Rainbow()
{
  int ledcolor = random(8); //this randomly selects a number between 0 and 7
  ledcolor=lastledcolor;
  currentTime = millis();
  if (currentTime - startTime >= period)
  {
    switch (ledcolor) {
case 0: //red
  led.setColor(255,0,0);
break;
case 1: //green
  led.setColor(0,255,0);
break;
case 2: //blue
  led.setColor(0,0,255);
break;
case 3: //yellow
  led.setColor(255,255,0);
break;
case 4: //cyan
  led.setColor(0,255,255);
break;
case 5: //magenta
  led.setColor(139,0,139);
break;
case 6: //orange
  led.setColor(255,69,0);
break;
case 7: //pink
  led.setColor(255,20,147);
break;
}//rainbow here
    startTime = currentTime;
}
}

Hello, I’m trying to get rgb strips to change colors and when given the ‘freeze’ command it will stop at one color. As of the now when I give it the ‘change’ command it executes stopRainbow() and the ‘freeze’ command executes Rainbow() which is the opposite of what I intended. Also when its changing colors, the led color is white and flickering. How do I make the changing of colors more obvious?

You need to use "Auto indent" - control-T - on that code to make it properly indented and readable.

To interrupt the Rainbow code, you need to put the switch criterion inside the Rainbow loop.

The flickering is likely due to the WiFi interaction delays.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
#include "certs.h"
#include <LEDStripDriver.h>

// DIN=GPIO16, CIN=GPIO14
LEDStripDriver led = LEDStripDriver(16, 14);
char messageBuffer[50];

WiFiClient espClient;
PubSubClient client(espClient);

unsigned long currentTime;
unsigned long startTime;
unsigned long period = 1000;

bool power = false;
int lastledcolor;
void setup_wifi()
{
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("WiFi connected to ");
  Serial.println(ssid);
  Serial.print("IP address : ");
  Serial.println(WiFi.localIP());
  delay(2000);
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop()
{
  if (!client.connected())
  {
    reconnect();
  }
  client.loop();
  if (power == true)
  {
    int ledcolor = random(8); //this randomly selects a number between 0 and 7
    ledcolor = lastledcolor;
    currentTime = millis();
    if (currentTime - startTime >= period)
    {
      switch (ledcolor) {
        case 0: //red
          led.setColor(255, 0, 0);
          break;
        case 1: //green
          led.setColor(0, 255, 0);
          break;
        case 2: //blue
          led.setColor(0, 0, 255);
          break;
        case 3: //yellow
          led.setColor(255, 255, 0);
          break;
        case 4: //cyan
          led.setColor(0, 255, 255);
          break;
        case 5: //magenta
          led.setColor(139, 0, 139);
          break;
        case 6: //orange
          led.setColor(255, 69, 0);
          break;
        case 7: //pink
          led.setColor(255, 20, 147);
          break;
      }//rainbow here
      startTime = currentTime;
    }
  }
  else
    stopRainbow();
}

void reconnect()
{
  while (!client.connected())
  {
    if (client.connect("Reception"))
    {
      Serial.println("connected");
      client.subscribe("RGB/Output");
    }
    else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again");
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length)
{
  //Serial.println();
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.print("length : ");
  Serial.println(length);
  memcpy(messageBuffer, payload, length);
  messageBuffer[length] = '\0';
  Serial.print("the payload is : ");
  Serial.println(messageBuffer);
  if (strcmp(messageBuffer, "change") == 0)
  {
    Serial.print("turning ");
    Serial.println(strcmp(messageBuffer, "change"));
    digitalWrite(LED_BUILTIN, HIGH);
    power = true; //rainbow
  }
  else if (strcmp(messageBuffer, "freeze") == 0)
  {
    Serial.print("turning ");
    Serial.println(strcmp(messageBuffer, "freeze"));
    digitalWrite(LED_BUILTIN, LOW);
    power = false;  //stop at last color
  }
}

void stopRainbow()
{
  switch (lastledcolor) {
    case 0: //red
      led.setColor(255, 0, 0);
    case 1: //green
      led.setColor(0, 255, 0);
    case 2: //blue
      led.setColor(0, 0, 255);
    case 3: //yellow
      led.setColor(255, 255, 0);
    case 4: //cyan
      led.setColor(0, 255, 255);
    case 5: //magenta
      led.setColor(139, 0, 139);
    case 6: //orange
      led.setColor(255, 69, 0);
    case 7: //pink
      led.setColor(255, 20, 147);
  }//one color
}

like this? its still flickering white

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
#include "certs.h"
#include <LEDStripDriver.h>

// DIN=GPIO16, CIN=GPIO14
LEDStripDriver led = LEDStripDriver(16, 14);
char messageBuffer[50];

WiFiClient espClient;
PubSubClient client(espClient);

unsigned long currentTime;
unsigned long startTime;

String lastledcolor;
String ledcolor;
int command;
void setup_wifi()
{
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    pause(500);
  }
  Serial.println();
  Serial.print("WiFi connected to ");
  Serial.println(ssid);
  Serial.print("IP address : ");
  Serial.println(WiFi.localIP());
  pause(2000);
}

void setup() {
  Serial.begin(9600);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  led.setColor(0, 0, 0);
}

void loop()
{
  if (!client.connected())
  {
    reconnect();
  }
  client.loop();
  if (command == 1)
  {
    Rainbow();
  }
  else if (command == 0)
  {
    lastledcolor = ledcolor;
    stopRainbow();
  }
}

void reconnect()
{
  while (!client.connected())
  {
    if (client.connect("Reception"))
    {
      Serial.println("connected");
      client.subscribe("RGB/Output");
    }
    else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again");
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length)
{
  //Serial.println();
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.print("length : ");
  Serial.println(length);
  memcpy(messageBuffer, payload, length);
  messageBuffer[length] = '\0';
  Serial.print("the payload is : ");
  Serial.println(messageBuffer);
  if (strcmp(messageBuffer, "change") == 0)
  {
    Serial.println(strcmp(messageBuffer, "change"));
    command= 1;

  }
  else if (strcmp(messageBuffer, "freeze") == 0)
  {
    Serial.println(strcmp(messageBuffer, "freeze"));
    command= 0;

  }
}

void stopRainbow()
{
  if (lastledcolor == "red")
    led.setColor(255, 0, 0);
  else if (lastledcolor == "green")
    led.setColor(0, 255, 0);
  else if (lastledcolor == "blue")
    led.setColor(0, 0, 255);
  else if (lastledcolor == "yellow")
    led.setColor(255, 255, 0);
  else if (lastledcolor == "cyan")
    led.setColor(0, 255, 255);
  else if (lastledcolor == "magenta")
    led.setColor(139, 0, 139);
  else if (lastledcolor == "orange")
    led.setColor(255, 69, 0);
  else if (lastledcolor == "pink")
    led.setColor(255, 20, 147);
}

void Rainbow()
{
  ledcolor = "red";
  led.setColor(255, 0, 0);
  pause(1000);
  ledcolor = "green";
  led.setColor(0, 255, 0);
  pause(1000);
  ledcolor = "blue";
  led.setColor(0, 0, 255);
  pause(1000);
  ledcolor = "yellow";
  led.setColor(255, 255, 0);
  pause(1000);
  ledcolor = "cyan";
  led.setColor(0, 255, 255);
  pause(1000);
  ledcolor = "magenta";
  led.setColor(139, 0, 139);
  pause(1000);
  ledcolor = "orange";
  led.setColor(255, 69, 0);
  pause(1000);
  ledcolor = "pink";
  led.setColor(255, 20, 147);
  pause(1000);
}

void pause(unsigned long period)
{
  currentTime = millis();
  if (currentTime - startTime >= period)
  {} startTime = currentTime;
}

What about this? will void pause() improve wifi communications?