Loop stops running

Hi,

I'm kind of new with Arduino. For a school project I have to make a led run a "breathing" loop on a ESP32. This loop shoot be activated by google assistant. With some YouTube tutorials I managed to turn a LED on with google assistend (with the use of IFTT and Adafruit toggle button). I also found the code to let a led "breath" (fade in and out). Individually both the codes work on the ESP32. But when I tried to combine the codes I run into a few problems. In the VOID loop I replaced the LED going on part (from the original code) with the Breathing fade loop. When I now try to activate this loop with google assistend, the loop does run but only ones. After one fade in and out it just stays still. When I then turn the led OFF and ON again (with google assistent) it does the same thing.

I realized that it's kind of obvious that it runs only one time because the signal coming form Adafruit (activated by google assistent) is a toggle button so not a constant ON signal. So then i tried to change the IF statement in the code to a While statement. With this change the loop did work correct but I couldn't turn it off again. In the serial Moniter I also can not seem to register incoming OFF signal if I Toggle it off in Adafruit. So my question is how can I make the loop work but also let it stop correctly.

I hope it all makes sense and that somebody can help me.
I will put the code in the attachment.

Thanks!

#include <WiFi.h>

#include "Adafruit_MQTT.h"

#include "Adafruit_MQTT_Client.h"

#define WLAN_SSID       "--"

#define WLAN_PASS       "--"

#define AIO_SERVER      "io.adafruit.com"

#define AIO_SERVERPORT  1883                  

#define AIO_USERNAME  "--"

#define AIO_KEY       "--"

int i = 0;

const int ledPin = 16;  // 16 corresponds to GPIO16

const int freq = 5000;

const int ledChannel = 0;

const int resolution = 8;

WiFiClient client;     // Create an ESP8266 WiFiClient class to connect to the MQTT server.

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);        // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.

Adafruit_MQTT_Subscribe LED_Control = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/LED_Control");

void MQTT_connect();

void setup() {

  Serial.begin(115200);

  delay(10);

pinMode(2,OUTPUT);

 // Connect to WiFi access point.

  Serial.println(); Serial.println();

  Serial.print("Connecting to ");

  Serial.println(WLAN_SSID);

 

  WiFi.begin(WLAN_SSID, WLAN_PASS);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

  Serial.println();

 Serial.println("WiFi connected");

  Serial.println("IP address: "); Serial.println(WiFi.localIP());

  mqtt.subscribe(&LED_Control);

   // configure LED PWM functionalitites
  ledcSetup(ledChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(ledPin, ledChannel);

}

uint32_t x=0;

void loop() {

   MQTT_connect();

Adafruit_MQTT_Subscribe *subscription;

  while ((subscription = mqtt.readSubscription(5000))) {

    if (subscription == &LED_Control) {

      Serial.print(F("Got: "));

      Serial.println((char *)LED_Control.lastread);

       if (!strcmp((char*) LED_Control.lastread, "ON"))

      {
       
{
        for(i = 15 ; i <= 255; i+=1)
  { 
    ledcWrite(ledChannel, i);
    if (i > 150) {
      delay(4);
    }
    if ((i > 125) && (i < 151)) {
      delay(5);
    }
    if (( i > 100) && (i < 126)) {
      delay(7);
    }
    if (( i > 75) && (i < 101)) {
      delay(10);
    }
    if (( i > 50) && (i < 76)) {
      delay(14);
    }
    if (( i > 25) && (i < 51)) {
      delay(18);
    }
    if (( i > 1) && (i < 26)) {
      delay(19);
    }
  }
  for(i = 255; i >=15; i-=1)
  {
     ledcWrite(ledChannel, i);
    if (i > 150) {
      delay(4);
    }
    if ((i > 125) && (i < 151)) {
      delay(5);
    }
    if (( i > 100) && (i < 126)) {
      delay(7);
    }
    if (( i > 75) && (i < 101)) {
      delay(10);
    }
    if (( i > 50) && (i < 76)) {
      delay(14);
    }
    if (( i > 25) && (i < 51)) {
      delay(18);
    }
    if (( i > 1) && (i < 26)) {
      delay(19);
    }
  }
  delay(970);
}

      }      else

      {

         ledcWrite(ledChannel, 0);

      }

    }

  }

 

}


void MQTT_connect() {

  int8_t ret;

  // Stop if already connected.

  if (mqtt.connected()) {

    return;

  }

 Serial.print("Connecting to MQTT... ");

uint8_t retries = 3;

  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected

       Serial.println(mqtt.connectErrorString(ret));

       Serial.println("Retrying MQTT connection in 5 seconds...");

       mqtt.disconnect();

       delay(5000);  // wait 5 seconds

       retries--;

       if (retries == 0) {

         // basically die and wait for WDT to reset me

         while (1);

       }

  }

  Serial.println("MQTT Connected!");

}

V_B_prototype.ino (3.5 KB)

Please put the code in code tags.

#include <WiFi.h>


#include "Adafruit_MQTT.h"


#include "Adafruit_MQTT_Client.h"


#define WLAN_SSID       "Pindakaas"


#define WLAN_PASS       "Pindakaasmet18stukjeshagelslag"


#define AIO_SERVER      "io.adafruit.com"


#define AIO_SERVERPORT  1883


#define AIO_USERNAME  "rvslageren"


#define AIO_KEY       "aio_gqdu63rVjEpsXDIaGvxsZkxG8Dai"


int i = 0;


const int ledPin = 16;  // 16 corresponds to GPIO16


const int freq = 5000;


const int ledChannel = 0;


const int resolution = 8;


WiFiClient client;     // Create an ESP8266 WiFiClient class to connect to the MQTT server.


Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);        // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.


Adafruit_MQTT_Subscribe LED_Control = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/LED_Control");


void MQTT_connect();


void setup()
{
  Serial.begin(115200);


  delay(10);


  pinMode(2, OUTPUT);


  // Connect to WiFi access point.


  Serial.println(); Serial.println();


  Serial.print("Connecting to ");


  Serial.println(WLAN_SSID);


  WiFi.begin(WLAN_SSID, WLAN_PASS);


  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }


  Serial.println();


  Serial.println("WiFi connected");


  Serial.println("IP address: "); Serial.println(WiFi.localIP());


  mqtt.subscribe(&LED_Control);


  // configure LED PWM functionalitites
  ledcSetup(ledChannel, freq, resolution);


  // attach the channel to the GPIO to be controlled
  ledcAttachPin(ledPin, ledChannel);
}


uint32_t x = 0;


void loop()
{
  MQTT_connect();


  Adafruit_MQTT_Subscribe *subscription;


  while ((subscription = mqtt.readSubscription(5000)))
  {
    if (subscription == &LED_Control)
    {
      Serial.print(F("Got: "));


      Serial.println((char *)LED_Control.lastread);


      if (!strcmp((char*) LED_Control.lastread, "ON"))
      {
        {
          for (i = 15 ; i <= 255; i += 1)
          {
            ledcWrite(ledChannel, i);
            if (i > 150)
            {
              delay(4);
            }
            if ((i > 125) && (i < 151))
            {
              delay(5);
            }
            if (( i > 100) && (i < 126))
            {
              delay(7);
            }
            if (( i > 75) && (i < 101))
            {
              delay(10);
            }
            if (( i > 50) && (i < 76))
            {
              delay(14);
            }
            if (( i > 25) && (i < 51))
            {
              delay(18);
            }
            if (( i > 1) && (i < 26))
            {
              delay(19);
            }
          }
          for (i = 255; i >= 15; i -= 1)
          {
            ledcWrite(ledChannel, i);
            if (i > 150)
            {
              delay(4);
            }
            if ((i > 125) && (i < 151))
            {
              delay(5);
            }
            if (( i > 100) && (i < 126))
            {
              delay(7);
            }
            if (( i > 75) && (i < 101))
            {
              delay(10);
            }
            if (( i > 50) && (i < 76))
            {
              delay(14);
            }
            if (( i > 25) && (i < 51))
            {
              delay(18);
            }
            if (( i > 1) && (i < 26))
            {
              delay(19);
            }
          }
          delay(970);
        }
      }
      else
      {
        ledcWrite(ledChannel, 0);
      }
    }
  }
}




void MQTT_connect()
{
  int8_t ret;


  // Stop if already connected.


  if (mqtt.connected())
  {
    return;
  }


  Serial.print("Connecting to MQTT... ");


  uint8_t retries = 3;


  while ((ret = mqtt.connect()) != 0)   // connect will return 0 for connected
  {
    Serial.println(mqtt.connectErrorString(ret));


    Serial.println("Retrying MQTT connection in 5 seconds...");


    mqtt.disconnect();


    delay(5000);  // wait 5 seconds


    retries--;


    if (retries == 0)
    {
      // basically die and wait for WDT to reset me
      while (1);
    }
  }


  Serial.println("MQTT Connected!");
}