HC-05 Bluetooth Cant break out of FOR LOOP

This is probably pretty simple but I’m still a newbie at coding. I created an Android app which works fine with the HC-05 Bluetooth with Nano as I am creating a LED Police Light Effect however I cannot seem to break out of the FOR LOOP with the button call if(data ==‘h’) . Can anyone help me?

void setup() {
Serial.begin(9600);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
}

void loop() {
if(Serial.available()>0)
{
char data = Serial.read();
if (data == ‘a’)
{
digitalWrite(2,HIGH);
}
else if(data == ‘b’)
{
digitalWrite(2,LOW);
}

if (data == ‘c’)
{
digitalWrite(3,HIGH);
}
else if(data == ‘d’)
{
digitalWrite(3,LOW);
}

if (data == ‘e’)
{
digitalWrite(4,HIGH);
}
else if(data == ‘f’)
{
digitalWrite(4,LOW);
}

if (data == ‘g’)
{

// FOR LOOP
for (int i = 0; i <= 255; i++) {
//
digitalWrite(2,HIGH);
delay(50);
digitalWrite(2,LOW);
delay(50);
digitalWrite(3,HIGH);
delay(50);
digitalWrite(3,LOW);
delay(50);

if(data ==‘h’){
break; }

}

}

// if(data == ‘h’)
// {
// digitalWrite(2,LOW);
// digitalWrite(3,LOW);
// }

}
}

Your Serial.read() is outside your for loop, so data never gets updated once in the loop.

 if (data == 'g')
    {

// FOR LOOP
       for (int i = 0; i <= 255; i++) {
//
      digitalWrite(2,HIGH);
      delay(50);
      digitalWrite(2,LOW);
      delay(50);
      digitalWrite(3,HIGH);
      delay(50);
      digitalWrite(3,LOW);
      delay(50);
 
    if(data =='h'){
    break; }
   
    }

If ‘data’ is ‘g’, how is it ever going to be ‘h’? You have to check for and process input from your serial port if you ever hope to see an ‘h’ in place of the ‘g’. If the only thing you care about is receiving an ‘h’ then put if(Serial.read() == ‘h’) in place of if(data == ‘h’). This WILL throw away any input character that is not an ‘h’.

you might find this interesting

#define LedA   12
#define LedB   11

int flag = 0;

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin(9600);

    pinMode (LedA, OUTPUT);
    pinMode (LedB, OUTPUT);

    digitalWrite (LedA, HIGH);
    digitalWrite (LedB, HIGH);
}

// -----------------------------------------------------------------------------
void
twiddle (void)
{
    static int state             = 0;
    static unsigned long msecLst = 0;
           unsigned long msec    = millis ();

    if (msec - msecLst < 100)  
        return;

    msecLst = msec;

    switch (state)  {
        case 1:
            digitalWrite (LedA, HIGH);
            break;

        case 2:
            digitalWrite (LedA, LOW);
            break;

        case 3:
            digitalWrite (LedB, HIGH);
            break;

        default:
            digitalWrite (LedA, LOW);
            digitalWrite (LedB, LOW);
            break;
    }
    state = 3 > state ? state + 1 : 0;
}

// -----------------------------------------------------------------------------
void
cmds (void)
{
    if (! Serial.available ())
        return;

    switch (Serial.read ())  {
        case 'a':
            digitalWrite (LedA, 1^digitalRead (LedA));
            break;

        case 'b':
            digitalWrite (LedA, 1^digitalRead (LedA));
            break;

        case 't':
            flag ^= 1;
            break;
    }
}

// -----------------------------------------------------------------------------
void
loop () {
    cmds ();
    if (flag)
        twiddle ();
}
            digitalWrite (LedA, 1^digitalRead (LedA));

Did you perhaps mean,

            digitalWrite (LedA,  not digitalRead (LedA));

?

that's another way ... but it works for me

gcjr:
that's another way ... but it works for me

OP: "I'm still a newbie at coding"

aarg:
Did you perhaps mean,

            digitalWrite (LedA,  not digitalRead (LedA));

had never seen "not".

i see it's not standard C, but apparently is a keyword C++

but presumably this would work as well

            digitalWrite (LedA,  ! digitalRead (LedA));

The last line is the canonical port toggle expression in Arduino land. You will see it in all kinds of sketches.

I'm confused by some of your post. The reason for the

if (data == 'g') is used by the Android App to turn on the loop. The if(data =='h') is used to turn off.

If someone could show me how to break out of the FOR LOOP by using the if(data =='h') would be awesome.

Thanks for your reply.

You were handed the solution in reply #1, and in excruciating detail in reply #2 :slight_smile: .

If someone could just re-write the code so I could see it, I would understand it better and very much highly appreciate it. It's senseless to try and explain it in post pieces to a newbie like me.

i thought i did.

Yes, I got it, that works. Thank you all very, very much for the if(Serial.read() == ‘h’) information.

WORKING CODE

void setup() {
Serial.begin(9600);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
}

void loop() {
if(Serial.available()>0)
{
char data = Serial.read();
if (data == ‘a’)
{
digitalWrite(2,HIGH);
}
else if(data == ‘b’)
{
digitalWrite(2,LOW);
}

if (data == ‘c’)
{
digitalWrite(3,HIGH);
}
else if(data == ‘d’)
{
digitalWrite(3,LOW);
}

if (data == ‘e’)
{
digitalWrite(4,HIGH);
}
else if(data == ‘f’)
{
digitalWrite(4,LOW);
}

if (data == ‘g’)
{

// FOR LOOP
for (int i = 0; i <= 255; i++) {
//
digitalWrite(2,HIGH);
delay(50);
digitalWrite(2,LOW);
delay(50);
digitalWrite(3,HIGH);
delay(50);
digitalWrite(3,LOW);
delay(50);

if(Serial.read() == ‘h’){
break; }

}

}

}
}

In future, please put your code in code tags, per the forum etiquette.