Problem with looping led blinking script

So i was trying to make a script in arduino that will use hc-05 bluetooth and i will have some buttons on phone that will change led blinking pattern, but i only can make led blink once becuase i don't know how i need to loop the if function. I've made an looping function but when i've tried to change the pattern the pattern would not change and stay the same. The script is very simple, could you make this script working like i want? I am trying to do this whole day
char Incoming_value = 0;

int LED1 = 8;
int bin = 0;
void setup()
{
Serial.begin(9600);
pinMode(LED1, OUTPUT);
}

void loop()
{
if(Serial.available() > 0)
{
Incoming_value = Serial.read();
Serial.print(Incoming_value);
Serial.print("\n");
if(Incoming_value == 'A')
bin = 2;
patternR2L();

}
if(Serial.available() > 0)
{
Incoming_value = Serial.read();
Serial.print(Incoming_value);
Serial.print("\n");
if(Incoming_value == 'B')
bin = 3;
patternL2R();

}
}

void patternR2L()
{
if(bin == 2) {
digitalWrite(LED1, HIGH);
delay(200);
digitalWrite(LED1, LOW);
delay(200);
}

}

void patternL2R()
{
if(bin == 3) {
digitalWrite(LED1, HIGH);
delay(20);
digitalWrite(LED1, LOW);
delay(20);
}

}

consider

#undef MyHW
#ifdef MyHW
int LED1 = LED_BUILTIN;

#else
int LED1 = 8;
int bin = 0;
#endif

void setup()
{
    Serial.begin(9600);
    pinMode(LED1, OUTPUT);
}
void loop()
{
    if(Serial.available() > 0)
    {
        byte val = Serial.read();
        Serial.println(val);
        
        switch (val)  {
        case 'A':
            patternR2L();
            break;

        case 'B':
            patternL2R();
            break;
        }
    }
}

void patternR2L()
{
    digitalWrite(LED1, HIGH);
    delay(200);
    digitalWrite(LED1, LOW);
}

void patternL2R()
{
    digitalWrite(LED1, HIGH);
    delay(20);
    digitalWrite(LED1, LOW);
}
1 Like

You do this lines two times. But after the first Serial.read() the byte is taken out of the receivebuffer and then the second

if(Serial.available() > 0)

never becomes true
except you would send "AB"
then the first Serial.read() would deliver the "A"

but the "B" would still be in the receive-buffer and

if(Serial.available() > 0)

would evaluate to true

If you want a reaction on a single byte being "A" or "B" you have to store the received byte in a variable like it is done with

 if(Serial.available() > 0)
    {
        byte val = Serial.read();

in gcjr's code
best regards Stefan

1 Like

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Thanks but i wanted to loop the R2L pattern And L2R pattern to make led blink constantly with other blinking time, i dont know how to loop it becuase if i loop it then i cant activate the other function

you want the LED to flash repeatedly?

#undef MyHW
#ifdef MyHW
int LED1 = LED_BUILTIN;

#else
int LED1 = 8;
int bin = 0;
#endif

unsigned long msec;
unsigned long msecLst;

unsigned blinkPeriod = 0;

void loop()
{
    msec = millis ();

    if (blinkPeriod && (msec - msecLst) > blinkPeriod)  {
        msecLst = msec;
        digitalWrite(LED1, ! digitalRead(LED1));
    }

    if(Serial.available() > 0)
    {
        byte val = Serial.read();
        Serial.println(val);
        
        switch (val)  {
        case 'A':
            blinkPeriod = 200;
            break;

        case 'B':
            blinkPeriod = 20;
            break;

        case '0':
            blinkPeriod = 0;
            break;
        }
    }
}

void setup()
{
    Serial.begin(9600);
    pinMode(LED1, OUTPUT);
}
1 Like

It's a normal practice to avoid delay functions in Embedded Programming. Here is a blog that, I think, can help you :

It is about using the timer interrupt. I have used Arduino Nano.

yes thank you

Thank you very very very much, last question, How to add a seperate LED with other blining time?

Start by reviewing the code offered you and mastering everything that is written in the sketch.

You can then add extra variables and code necessary to have another LED sequence.

1 Like

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