How can I exit a loop without ending the program

void loop()
{
 blueToothSerial.write("AT+DISC?");
  Serial.println("Discovery on");
  //lets me know if the device is starting discovery
  delay(1000);
    Serial.println("Connection found");
    blueToothSerial.write("AT+CONE4E112960A56");
    Reply=blueToothSerial.readString();
    Serial.print(Reply);

char reply[20];
   if (Reply.startsWith("OK+CONN:E4E112960A56"));
   {
    end
   }
   else if (Reply.startsWith("")
   {
   blueToothSerial.write("AT+DISC?");
   Serial.print(Reply);
   blueToothSerial.write("AT+CONE4E112960A56")
    
   }
}

This is my code, I want to make it so that when the bluetooth pieces do connect, "If Reply.startswith" line, that the code will do nothing else, but it will not loop back to the beginning, disconnecting the device and then connecting again.
Thank you

If you just want the code to stop and do nothing else, just create an infinite loop;

for(;;);

or

while(true);

Off the top of my head, with the disclaimer that I have never done any off this connection stuff, I'd use a boolean flag called say connectionIsMade, initialised false. Then do the "discovery" in side an if where connectionIsMade is false. Set connectionIsMade true in the part where you decide you're happy with the connection... then the code will still continue to run, but won't do the discovery again since the flag is true.

edit... I was taking the view that actually stopping or blocking the code is not a great solution in the long run, since you may want the same sketch to do other stuff.

As an enhancement to what I said above (assuming that made sense in the first place) it would make it very tidy to have the discovery stuff in a function called say doDiscovery() and only call it if connectionIsMade is false. Having things tucked away in functions makes things clean and easy to follow, rather than having a ton of stuff all in loop().

can I see how you would write the code. I am still pretty new to this

Does this require me to completely redesign my code?

Just as per my previous post.. either of those statements instead of the "end" that you have currently will cause the program to effectively stop doing anything further.

What exactly is the whole piece of code expected to do?

That's the real question. If OP literally wants things to stop then the for or while approach is fine, but not if the code is ever meant to do anything else before the next reset.

Not completely no, more of a tidy up. If I get a gap later I'll see if I can do a quick example- just starting the day here, 0815.


#include <SoftwareSerial.h>

SoftwareSerial blueToothSerial(10, 11); // RX, TX

char c=' ';
char reply[30];
int pos = 0;
//int cmp[] = "OK+CONN";

String Reply;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(7, OUTPUT);
  delay(1000);
  Serial.println("Searching for device");
  blueToothSerial.begin(9600);
  Serial.println(" ");

  delay(1000);
  Serial.println("Lemme set up real quick");
  //this step tells me when the device is attempting to connect;
  blueToothSerial.write("AT+IMME1\r\n");
  Reply = blueToothSerial.readString();
  Serial.print(Reply);
  Serial.println("Auto connection is off");
  //allows me to know if the command AT+IMME is sent;
  delay(1000);
  blueToothSerial.write("AT+ROLE1\r\n");
  Reply=blueToothSerial.readString();
  Serial.print(Reply);
  Serial.println("Master Role set");
  //allows me to know if the command AT+ROLE1 is sent, configues device to be master upon startup;
  delay(1000);
 

}
void loop()
{
 blueToothSerial.write("AT+DISC?");
  Serial.println("Discovery on");
  //lets me know if the device is starting discovery
  delay(1000);
    Serial.println("Connection found");
    blueToothSerial.write("AT+CONE4E112960A56");
    Reply=blueToothSerial.readString();
    Serial.print(Reply);

char reply[20];
   if (Reply.startsWith("OK+CONN:E4E112960A56"));
   {
    end
   }
   else if (Reply.startsWith("")
   {
   blueToothSerial.write("AT+DISC?");
   Serial.print(Reply);
   blueToothSerial.write("AT+CONE4E112960A56")
    
   }
}

The code is a master bluetooth device that will always search for a slave signal. The slave signal will not sent unless a tripwire has been set off. What I want this code to do is so that it constantly searches for the slave signal, and when it does find the slave signal, the device will connect to the slave and an alarm will sound. I have gotten everything done except the programming of the master device. My plan is for the search to be a loop, and when it does successfully connect, to not end the code, but prevent the arduino from looping again.

So a trip wire gets triggered by the slave, and connects to the master... that then sounds an alarm.

But once this happens, then what? Does the alarm ever stop? Does the system need to reset and wait for another trip wire slave?

@andrewl429m your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that e.g. you write, not for questions. Feel free to write a tutorial once you have solved your problem :wink:

No it remains triggered.

It would be very useful if you would give a complete overview about the wanted functionality and the reality context in which you are using this.
Most comfort way to make it easy to help you is if you write a 10 to 20 sententences long description of two things

  1. what you want to do in the end in the real context
  2. a detailed description in normal words of the wanted functionality

about 1:
is your device switching on a soft-ice-machine pouring ice endlessly as soon as a party-guest holds his smartphone close to it?

about 2: the soft-ice machine stays switched on as the wanted functionality is a try to do a new world-record in soft-ice inhalating

I'm 1000% sure this is not what you want. But what is it?

Be the change you want to see in the world
best regards Stefan

So our the problem with our Alarm is it will connect and the alarm will go off for only a second, the problem is because the code is a loop, the device will automatically disconnect and try to find the signal again. I want the device to stop searching or discovering “AT+DISC?” As soon as it has connected with the slave device. The alarm is a one time use, once tripped you have to completely reset it, by installing a new tripwire, which in turn will shut off the slave device and shut off the alarm.

I try to desrcibe in my own words:

there is no blue-tooth-device detected by the slave-device => alarm shall be switched ON permanently

a blue-tooth-device is detected by one slave-device => all other slave-devices and the master-device receive a message and all devices start alarming-beebing in intervalls

  • one second on
  • one second off

this interval alarming shall only start whenever a new tripwire is installed.

Is this a correct description of the wanted behaviour?
If yes confirm. If no correct in simple sentences in a similar pattern as I have written it.

This pattern is: fully detailed description of conditions lead to => fully detailed description of all actions that happen

best regards Stefan

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(7, OUTPUT);
  delay(1000);
  Serial.println("Searching for device");
  blueToothSerial.begin(9600);
  Serial.println(" ");

This is our setup, and sets the master device's baud band to 9600. the pinMode originally was suppose to be hooked up to an alarm, but that was scrapped and I still haven't deleted it.

 delay(1000);
  Serial.println("Lemme set up real quick");
  //this step tells me when the device is attempting to connect;
  blueToothSerial.write("AT+IMME1\r\n");
  Reply = blueToothSerial.readString();
  Serial.print(Reply);
  Serial.println("Auto connection is off");
  //allows me to know if the command AT+IMME is sent;
  delay(1000);
  blueToothSerial.write("AT+ROLE1\r\n");
  Reply=blueToothSerial.readString();
  Serial.print(Reply);
  Serial.println("Master Role set");
  //allows me to know if the command AT+ROLE1 is sent, configues device to be master upon startup;
  delay(1000);

These commands sets the HM bluetooth device, to the master role, and switches the auto connection off. That way I can connect the bluetooth device to the slave device specifically and not a random phone or TV.

void loop()
{
 blueToothSerial.write("AT+DISC?");
  Serial.println("Discovery on");
  //lets me know if the device is starting discovery
  delay(1000);
    Serial.println("Connection found");
    blueToothSerial.write("AT+CONE4E112960A56");
    Reply=blueToothSerial.readString();
    Serial.print(Reply);

  {
    for(Reply.startsWith("OK+CONN:E4E112960A56"));{
    stop();
   }
  }
}

This is the part of the code I am having trouble with. What it is suppose to do is constantly search, constantly do the Discovery command "AT+DISC?" the master device will therefore try to find any signals from the slave device

The alarm is on the slave device, the slave bluetooth piece will not turn on until, the tripwire is tripped, when the tripwire is trip, the device will turn on and start sending out a signal. The master will therefore detect the, now on, slave device and connect to it. The "AT+CONN:E..." but the problem I'm having is that it will connect and then immediately disconnect because its a loop so it will disconnect and start discovery again. I want it to stop discovering once it connects to the slave device.

The tripwire when reloaded will shut off the slave device, which will disconnect the master, hence restarting the alarm.

Also forgive me for my poor English, it is not my first language.

use google-translate.
You will be much more informative if you write down a detailed description in your motherlanguage and let do google-translate the translation

What is so hard about writing a description in this pattern:

  1. masterdevice is constantly scanning for bluetooth slaves
  2. if a slave is detected establish connection to this slave
  3. keep the connection to this slave established
  4. if slave disconnects (through bluetooth-module of slave beeing switched off) start scanning for slaves again

your actual code is assuming that there is a slave and sends the AT+CONE-command
it sends this command regardless of if a slave is really found.

your code does send the
AT+CONE-command

each and every iteration of the loop

You do no checking at all if a slave is really present attempting to connect

without this checking if a slave is really present attempting to connect
you have no chance to change your code from how it works now.

You will have to learn more about the commands and how to retrieve status-messages from the bluetooth-module.

Be the change you want to see in the world
best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.