Leave the loop

Hello I would like to be able to leave the loop when i send a 0. But it doesn’t work. How do i do this ???

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "**********";
const char* password = "***********";
const char* mqtt_server = "mqtt-dashboard.com";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

#include "FastLED.h"
#define LED_PIN 4
#define NUM_LEDS 1
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
#define BRIGHTNESS  255 

CRGB leds[NUM_LEDS];

//CRGB leds[NUM_LEDS];

void setup() {    
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  }
  
uint8_t gHue = 0; 

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  payload[2]='\0';
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);}
  Serial.println();
  
switch (payload[0]) {
  
  case '1':{
    Serial.println("On");

    
    for (int i = 0; i < 2;) {
      for (int i = 0; i < 255; i++) {
      fill_solid(leds, NUM_LEDS, CHSV( gHue, 255, i));
      FastLED.show();
      delay(15);
      EVERY_N_MILLISECONDS(10) {gHue++;} 
      Serial.println(payload[0]);
      if ((char)payload[0] == '0') {
       exit;}
      }
     for (int i = 0; i < 255; i++) {
      fill_solid(leds, NUM_LEDS, CHSV( gHue, 255, 255-i));
      FastLED.show();
      delay(15);
      EVERY_N_MILLISECONDS(10) {gHue++;} 
      Serial.println(payload[0]);
      if ((char)payload[0] == '0') {
       exit;}
      }

      
      //int pos = beatsin16(5,0,192); // generating the sinwave 
      //fill_solid(leds, NUM_LEDS, CHSV( gHue, 255, pos)); // CHSV (hue, saturation, value);
      //FastLED.show();
      //EVERY_N_MILLISECONDS(10) {gHue++;} 
      //Serial.println(pos);
      // Serial.println(i);
      if ((char)payload[0] == '0') {
       i = 2;}
    }
  break;}
  
  case '0': {
    Serial.println("Out");
    leds[0] = CRGB::Black;
    FastLED.show();
  break;}
  
  }
}

void loop() {
  if (!client.connected()) {
    reconnect();}
  client.loop(); 
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      client.subscribe("PLANT/1");} 
      else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void setup_wifi() {
  delay(10);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");}
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

RGBBreathingESP8266.ino (3.14 KB)

exit;Try return instead.

"exit" by itself like that does nothing at all. (exit is a function, unlike return)

Doesn't work with return either.

TheRogueNinja:
Doesn't work with return either.

Please tell us what "work" means to you.

Use as many words as you need.

Ok so, the esp8266 turns on and the led is bright white, then I send a 1 with mqtt and the esp8266 excutes the "rgb breathing" loop, then if i send a 0 the esp8266 doesn't see the 0 that I send (because either the delay or the loop, I think) and because it doesn't see the 0 that I send the led doesn't turn off. What I would like to know is how to register the 0 that I send via mqtt while being the loop.

also thank you for your help so far.

I wouldn't put a blocking loop in the callback - just implement the LED flashing as a state machine in the main loop, controlled by a simple flag set or reset in the callback.