sending a word
hi 
so i've connected bluetooth module arduino , phone , i'm controlling 3 leds sending if want turn red led on , b if want turn orange 1 .. etc.
but wand send word such red turn red 1 can't figure out how .. please :~
and here's code :
int led1 = 8;
int led2 = 7;
int led3 = 4;
char state = 0;
void setup () {
serial.begin (9600);
pinmode(led1, output);
pinmode(led2, output);
pinmode(led3, output);
}
void loop () {
if (serial.available () > 0) {
state = serial.read ();
}
if (state == 'a') {
digitalwrite (led1, high);
digitalwrite (led2, low);
digitalwrite (led3, low);
}
if (state == 'b') {
digitalwrite (led1, low);
digitalwrite (led2, high);
digitalwrite (led3, low);
}
if (state == 'c') {
digitalwrite (led1, low);
digitalwrite (led2, low);
digitalwrite (led3, high);
}
}

so i've connected bluetooth module arduino , phone , i'm controlling 3 leds sending if want turn red led on , b if want turn orange 1 .. etc.
but wand send word such red turn red 1 can't figure out how .. please :~
and here's code :
int led1 = 8;
int led2 = 7;
int led3 = 4;
char state = 0;
void setup () {
serial.begin (9600);
pinmode(led1, output);
pinmode(led2, output);
pinmode(led3, output);
}
void loop () {
if (serial.available () > 0) {
state = serial.read ();
}
if (state == 'a') {
digitalwrite (led1, high);
digitalwrite (led2, low);
digitalwrite (led3, low);
}
if (state == 'b') {
digitalwrite (led1, low);
digitalwrite (led2, high);
digitalwrite (led3, low);
}
if (state == 'c') {
digitalwrite (led1, low);
digitalwrite (led2, low);
digitalwrite (led3, high);
}
}
arduino can read 1 character @ time, need array can store characters come in:
you need variable keeps track of in array next available slot is:
every time read byte:
you check if matches terminating character. can of choosing, common tactic have serial monitor send newline, , use that:
if true, can use strcmp() see if matches string:
and take action accordingly. should clear buffer:
if not match new line character, stick buffer, increment index , null terminate array (required use string functions strcmp()):
code: [select]
char mybuffer[10+1]; // can receive 10 characters plus null terminatoryou need variable keeps track of in array next available slot is:
code: [select]
int myindex = 0;every time read byte:
code: [select]
inbyte = serial.read();you check if matches terminating character. can of choosing, common tactic have serial monitor send newline, , use that:
code: [select]
if (inbyte == '\n')if true, can use strcmp() see if matches string:
code: [select]
if (strcmp(mybuffer, "red") == 0)and take action accordingly. should clear buffer:
code: [select]
mybuffer[0] = '\0';
myindex = 0;if not match new line character, stick buffer, increment index , null terminate array (required use string functions strcmp()):
code: [select]
mybuffer[myindex] = inbyte; // put byte in buffer
myindex++; // move next index
mybuffer[myindex] = '\0'; // , null terminate array
Arduino Forum > Using Arduino > Programming Questions > sending a word
arduino
Comments
Post a Comment