Getting out of a loop in an esp32

Hello, I have this project that I have to finish but I am still a beginner in the Arduino world

I have an ESP32 and i want to turn an led on and off using the Bluetooth connectivity, I have that all set but I have one problem. I also want it to run automatically and then if I decide to get out of the automatic function, I can go back to choosing on or off.

here is my code if anyone can help me, please

//Program to control LED (ON/OFF) from ESP32 using Serial Bluetooth
#include <Arduino.h>
#include <BluetoothSerial.h> //Header File for Serial Bluetooth, will be added by default into Arduino

BluetoothSerial ESP_BT; //Object for Bluetooth

int incoming;
int yellow_led = 13;

void setup() {
Serial.begin(9600); //Start Serial monitor in 9600
ESP_BT.begin("ESP32_LED_Control"); //Name of your Bluetooth Signal
Serial.println("Bluetooth Device is Ready to Pair");

pinMode (yellow_led, OUTPUT);//Specify that LED pin is output
}

void automatic()
{
digitalWrite(yellow_led, HIGH);
ESP_BT.println("LED turned ON");

delay(2000);

digitalWrite(yellow_led, LOW);
ESP_BT.println("LED turned OFF");

delay(2000);

if(incoming == 49 || incoming == 48)
loop();
}

void loop() {

if (ESP_BT.available()) //Check if we receive anything from Bluetooth
{
incoming = ESP_BT.read(); //Read what we recevive
Serial.print("Received:"); Serial.println(incoming);

if (incoming == 49)
{
digitalWrite(yellow_led, HIGH);
ESP_BT.println("LED turned ON");
}

else if (incoming == 48)
{
digitalWrite(yellow_led, LOW);
ESP_BT.println("LED turned OFF");
}

else
{
while (incoming == 50)
automatic();
}
}
delay(20);
}

What does "run automatically" mean?
Makes no sense.

.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Sorry for the misunderstanding, the esp32 has 3 functions, one to turn the led on, another to turn the led off, and another to be in a loop turning it on and off.

my question is when it gets to the loop, how am I able to get out by clicking the on or off button

Hi,
You have 2 MODES, Manual and Auto,

So two buttons on your smartyphone , ONE for Manual/Auto mode, the OTHER for ON and OFF when in Manual.

Tom... :slight_smile:

Calling loop() doesn't make any sense. The moment your automatic() function ends it jumps back to where it was called from (in loop()).

Now the main problem is probably the while() loop you're calling automatic() from. I guess that after automatic() calls loop(), that function will run once, when completed returns to where it was called in automatic(), which then terminates, returning to the while loop, and as in that instance the variable incoming never gets updated you're stuck.

Can't follow what I'm saying here? Now you know why this kind of coding is colloquially called "spaghetti code".

It is that you have these delay() calls (another indication of poor programming, normally) or you'd no doubt get a WDT timeout crash.

Hi,

else
        {
          while (incoming == 50)
            automatic();
        }
void automatic()
{
  digitalWrite(yellow_led, HIGH);
  ESP_BT.println("LED turned ON");
   delay(2000);
  digitalWrite(yellow_led, LOW);
  ESP_BT.println("LED turned OFF");
  delay(2000);
  if(incoming == 49 || incoming == 48)
    loop();
}

You have 50 as the command to get into the automatic, but you are using a while statement.
Where in the while statement or automatic function do you check to see if a command other than 50 has been received by BlueTooth?
You need to loop continuously through the void loop(), otherwise you will never get updates of the commands you send.
while statement is not going to work here.

Also the use of delays to blink will hinder your code.
Look up the IDE Example, "Blink without Delay"

Tom..... :slight_smile: