judlex
October 21, 2021, 5:19pm
1
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);
}
}
gcjr
October 21, 2021, 6:12pm
2
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
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is frustrating for you and frustrating for us.
The people who try to help with your pro…
judlex
October 23, 2021, 9:59am
5
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
gcjr
October 23, 2021, 10:14am
6
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 :
Date : August 19, 2021
Hey, this is Shaunak !
When we are working with tactile switches integrated in an Embedded System, being a programmer, we often face the switch debouncing issue which is happens due to the mechanical contact of the switch...
It is about using the timer interrupt. I have used Arduino Nano.
judlex
October 23, 2021, 3:20pm
9
Thank you very very very much, last question, How to add a seperate LED with other blining time?
LarryD
October 23, 2021, 3:26pm
10
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
system
Closed
April 21, 2022, 3:27pm
11
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.