how would i turn off this while loop using a serial command?

ok first i will start by saying im using bluetooth to send commands to my arduino mega..

i can turn on the loop by sending c. i would like to send d to turn it off... here is my code..

void setup() {

Serial.begin(9600);
Serial1.begin(9600);
pinMode(13, OUTPUT); // only put because of floating pin
pinMode(12, OUTPUT);}

void loop() {

char val;
val=Serial1.read();
if(val == 'a')
{
digitalWrite(12, HIGH);
Serial1.write("ledon");
Serial.write("ledon");
}
if(val == 'b')
{
digitalWrite(12, LOW);
Serial1.write("ledoff");
Serial.write("ledoff");
}
while(val == 'c')
{

digitalWrite(12, HIGH);
delay(100);
digitalWrite(12, LOW);
delay(100);

}
// here is where i would use d to turn off the while loop for c
}

I see you've made several posts, but did not use code tags for your program code. Please read Nick Gammon's two posts at the top of this Forum for the proper way to post code. You'll likely get more responses if you do.

I'm not exactly sure what you're trying to do. Is this close?

void setup() {

  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {

  char val;

  if (Serial.available() > 0) {
    val = tolower(Serial.read());   // in case the enter upper case
    switch (val) {
      case 'a':
        digitalWrite(13, HIGH);
        Serial.println("ledon");
        break;
      case 'b':
        digitalWrite(13, LOW);
        Serial.println("ledoffon");
        break;

      case 'c':
        digitalWrite(13, HIGH);
        delay(100);
        digitalWrite(13, LOW);
        delay(100);       
        break;
      default:
        break;

    }
  }
  // here is where i would use d to turn off the while loop for c
}

Your while loop can never exit, because you never read the serial buffer again.

youknowj2:
i can turn on the loop by sending c. i would like to send d to turn it off... here is my code..

In simple terms, that is not possible. A WHILE loop continues until it completes. You need to use an IF and rely on the repetition of the loop() function to cause the iterations. That way your code can also receive the next instruction and respond to it.

Have a look at how the code in Planning and Implementing a Program is organized so it can receive serial data while doing other things

...R
Serial Input Basics

Thanks for the replies. I'm just going to not put the loop in it for now.

This is not necessarily a good way to solve your problem, but I think it will do what you want.

Remove the section of code below:

while(val == 'c')
{
   
   digitalWrite(12, HIGH);
   delay(100);
     digitalWrite(12, LOW);
     delay(100);
  
    
     }

Replace the code with the code below.

  boolean stayInLoopFlag = 1;
  while (val == 'c' && stayInLoopFlag)
  {
    digitalWrite(12, HIGH);
    stayInLoopFlag = watchAndWait('d', 100);
    digitalWrite(12, LOW);
    stayInLoopFlag = watchAndWait('d', 100);
  }

Here's the function "watchAndWait()".

boolean watchAndWait(byte characterToWatchFor, unsigned long delayInterval)
{
  unsigned long startDelayTime = millis();
  boolean stayInLoopFlag = 1;
  while (stayInLoopFlag && millis() - startDelayTime < delayInterval)
  {
    while (Serial.available() > 0 && stayInLoopFlag)
    {
      byte inputCharacter = Serial.read();
      if (inputCharacter == characterToWatchFor)
      {
        stayInLoopFlag = 0;
      }
    }
  }
  return stayInLoopFlag;
}

I second the other suggestions made in this thread.

Yes that worked. Thanks. Except I had to add the 1 after the Serial's. I have to send d a few times sometimes to get it to turn off. I was tempted to use a goto which everywhere I looked says its not reccomended. I even tried to use a do while loop but couldn't get it to work either.