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);
}
}
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.
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);
}
}
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.
// 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
}
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
}
}
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.
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.
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.
quijano930a:
im only sending a 'r'
But what is the software adding?
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..
i solved it it whas a problem with the app 