# Problem Serial and blinking a led

**URL:** <https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669>\
**Category:** Programming\
**Created:** [May 1, 2015, 1:59pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669 "2015-05-01T13:59:46Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![quijano930a](https://avatars.discourse-cdn.com/v4/letter/q/3e96dc/32.png) [@quijano930a](https://forum.arduino.cc/u/quijano930a)\
**Post date:** [May 1, 2015, 1:59pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/1 "2015-05-01T13:59:46Z")

</div>

Hi im trying to blink a led using my android celphone so i dont have problem to turn on/off my led but the problem begin when i need to blink my led just only blink 1 time and i need to continuos blinking here is the code i understand that serials is always reading the serial and thats my problem i think

code :

const int ledAzul = 9;  
const int ledRojo = 10;  
const int ledVerde = 6;  
char d = 'c';  
char c = 'c';  
int a = 1;

void setup() {  
Serial.begin(9600);  
pinMode(ledAzul, OUTPUT);  
pinMode(ledVerde, OUTPUT);  
pinMode(ledRojo, OUTPUT);  
}  
void loop(){

char c =Serial.read();  
if(c=='r'){ // when y recive and 'r' from my phone y activate the blink  
digitalWrite(ledRojo,HIGH);  
delay(500);  
digitalWrite(ledRojo,LOW);  
delay(500);  
}  
if(c=='a'){ // when y recive and 'a' from my phone y activate the blink  
digitalWrite(ledAzul,HIGH);  
delay(500);  
digitalWrite(ledAzul,LOW);  
delay(500);  
}  
if(c=='v'){ // when y recive and 'v' from my phone y activate the blink  
digitalWrite(ledVerde,HIGH);  
delay(500);  
digitalWrite(ledVerde,LOW);  
delay(500);  
}

}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 1, 2015, 2:02pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/2 "2015-05-01T14:02:13Z")

</div>

> i understand that serials is always reading the serial and thats my problem i think

You need to separate the reading of the data from using the data that was read.

You should ONLY be reading from the serial port if there is something to read. Look at the documentation for the Serial.available() function.

---

<div class="post-metadata">

**Author:** ![quijano930a](https://avatars.discourse-cdn.com/v4/letter/q/3e96dc/32.png) [@quijano930a](https://forum.arduino.cc/u/quijano930a)\
**Post date:** [May 1, 2015, 2:05pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/3 "2015-05-01T14:05:25Z")

</div>

i did it but it the same just blink once time

if (Serial.available() \> 0) {  
char c =Serial.read();  
if(c=='r'){ // when y recive and 'r' from my phone y activate the blink  
digitalWrite(ledRojo,HIGH);  
delay(500);  
digitalWrite(ledRojo,LOW);  
delay(500);  
}  
if(c=='a'){ // when y recive and 'r' from my phone y activate the blink  
digitalWrite(ledAzul,HIGH);  
delay(500);  
digitalWrite(ledAzul,LOW);  
delay(500);  
}  
if(c=='v'){ // when y recive and 'r' from my phone y activate the blink  
digitalWrite(ledVerde,HIGH);  
delay(500);  
digitalWrite(ledVerde,LOW);  
delay(500);  
}  
}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 1, 2015, 2:43pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/4 "2015-05-01T14:43:50Z")

</div>

> i did it but it the same just blink once time

That is because you did NOT separate reading the value from using the value.

```arduino
// Read data if there is any
if(Serial.available() > 0)
{
    c = Serial.read();
}

// Use the data
switch(c)
{
    case 'r':
      // do something
      break;
    // other cases
}

```

---

<div class="post-metadata">

**Author:** ![quijano930a](https://avatars.discourse-cdn.com/v4/letter/q/3e96dc/32.png) [@quijano930a](https://forum.arduino.cc/u/quijano930a)\
**Post date:** [May 1, 2015, 2:53pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/5 "2015-05-01T14:53:10Z")

</div>

i did it but its the same just blink once time

const int ledAzul = 9;  
const int ledRojo = 10;  
const int ledVerde = 6;  
char d = 'c';  
char c = 'c';  
int a = 1;

void setup() {  
Serial.begin(9600);  
pinMode(ledAzul, OUTPUT);  
pinMode(ledVerde, OUTPUT);  
pinMode(ledRojo, OUTPUT);  
}  
void loop(){

if(Serial.available() \> 0)  
{  
c = Serial.read();  
}

// Use the data  
switch(c)  
{  
case 'r':  
digitalWrite(ledRojo,HIGH);  
delay(500);  
digitalWrite(ledRojo,LOW);  
delay(500);  
break;  
// other cases  
}

}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 1, 2015, 2:57pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/6 "2015-05-01T14:57:30Z")

</div>

```cpp
char d = 'c';
char c = 'c';

```

WTF?

What is sending the serial data? If you are using the Serial Monitor application, what line ending do you have set?

If you are sending the letter c followed by a carriage return and line feed, you should add cases for them.

---

<div class="post-metadata">

**Author:** ![RayLivingston](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@RayLivingston](https://forum.arduino.cc/u/RayLivingston)\
**Post date:** [May 1, 2015, 3:00pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/7 "2015-05-01T15:00:27Z")

</div>

Rather than blindly shoving ANY received character into c, you should be checking to make sure it's a valid command character. If not, simply discard it without altering c.

Regards,  
Ray L.

---

<div class="post-metadata">

**Author:** ![quijano930a](https://avatars.discourse-cdn.com/v4/letter/q/3e96dc/32.png) [@quijano930a](https://forum.arduino.cc/u/quijano930a)\
**Post date:** [May 1, 2015, 3:02pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/8 "2015-05-01T15:02:57Z")

</div>

im only sending a 'r'

---

<div class="post-metadata">

**Author:** ![RayLivingston](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@RayLivingston](https://forum.arduino.cc/u/RayLivingston)\
**Post date:** [May 1, 2015, 3:04pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/9 "2015-05-01T15:04:23Z")

</div>

> quijano930a:  
> im only sending a 'r'

Which is almost certainly followed by a line-ending, either a carriage return, line feed, or both.

Regards,  
Ray l.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 1, 2015, 3:04pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/10 "2015-05-01T15:04:44Z")

</div>

> quijano930a:  
> im only sending a 'r'

But what is the software adding?

---

<div class="post-metadata">

**Author:** ![quijano930a](https://avatars.discourse-cdn.com/v4/letter/q/3e96dc/32.png) [@quijano930a](https://forum.arduino.cc/u/quijano930a)\
**Post date:** [May 1, 2015, 3:14pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/11 "2015-05-01T15:14:35Z")

</div>

when i put this code  
if(Serial.available() \> 0)  
{  
c = Serial.read();  
Serial.print(c);  
}

in the monitor serial appear and 'r' when y send 'r' and nothing more..

---

<div class="post-metadata">

**Author:** ![quijano930a](https://avatars.discourse-cdn.com/v4/letter/q/3e96dc/32.png) [@quijano930a](https://forum.arduino.cc/u/quijano930a)\
**Post date:** [May 1, 2015, 3:21pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/12 "2015-05-01T15:21:03Z")

</div>

i solved it it whas a problem with the app 😃

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:16pm UTC](https://forum.arduino.cc/t/problem-serial-and-blinking-a-led/308669/13 "2021-05-05T21:16:07Z")

</div>


