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 BRIGHTNESS  255 


//CRGB leds[NUM_LEDS];

void setup() {    
  client.setServer(mqtt_server, 1883);
uint8_t gHue = 0; 

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

    for (int i = 0; i < 2;) {
      for (int i = 0; i < 255; i++) {
      fill_solid(leds, NUM_LEDS, CHSV( gHue, 255, i));
      EVERY_N_MILLISECONDS(10) {gHue++;} 
      if ((char)payload[0] == '0') {
     for (int i = 0; i < 255; i++) {
      fill_solid(leds, NUM_LEDS, CHSV( gHue, 255, 255-i));
      EVERY_N_MILLISECONDS(10) {gHue++;} 
      if ((char)payload[0] == '0') {

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

void loop() {
  if (!client.connected()) {

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())) {
      else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");

void setup_wifi() {
  Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");
  Serial.println("IP address: ");

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.

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.