Serial commands help
have problem controlling leds via serial ( arduino 1 - xbee , xbee - arduino 2 )
so far code turns led 13 on on button push not turn led off
here code both
sender , reciver ,
sender
reciver
so far code turns led 13 on on button push not turn led off
here code both
sender , reciver ,
sender
code: [select]
#include <bounce2.h>
bounce t_button_3 = bounce();
bounce t_button_4 = bounce();
bounce t_button_5 = bounce();
int t_button_pin_3 = 3;
int t_button_pin_4 = 4;
int t_button_pin_5 = 5;
int debounce_time = 3;
void setup() {
pinmode(t_button_pin_3, input_pullup);
pinmode(t_button_pin_4, input_pullup);
pinmode(t_button_pin_5, input_pullup);
/* set each butons bounce */
t_button_3.attach(t_button_pin_3);
t_button_3.interval(debounce_time);
t_button_4.attach(t_button_pin_4);
t_button_4.interval(debounce_time);
t_button_5.attach(t_button_pin_5);
t_button_5.interval(debounce_time);
}
void loop() {
serial.begin(38400);
t_button_3.update();
t_button_4.update();
t_button_5.update();
if(t_button_3.fell() == true) {
serial.println("3 0");
}
if(t_button_3.rose() == true) {
serial.println("3 1");
}
serial.flush();
/// end of loop
}
reciver
code: [select]
int max_cmd_length = 20;
char cmd[20];
int cmdindex;
char incomingbyte;
int led = 13;
void setup() {
serial.begin(38400);
delay(200);/////?
pinmode(led, output);
cmdindex = 0;
}
void loop() {
/*if (incomingbyte=serial.available()>0)*/
while (serial.available() > 0)
{
char bytein = serial1.read();
cmd[cmdindex] = bytein;
if(bytein=='\r'){
cmd[cmdindex] = '\0';
cmdindex = 0;
if(strcmp(cmd, "3 1") == 0){
digitalwrite(led,high);
}else if (strcmp(cmd, "3 0") == 0) {
digitalwrite(led,low);
}else{
}
}else{
if(cmdindex++ >= max_cmd_length){
cmdindex = 0;
}
}
}
}
quote
so far code turns led 13 on on button push not turn led offyou don't consume newline character send @ end of println() command. because next string contains *\n3 0" strcmp(cmd, *3 0*) fail.
Arduino Forum > Using Arduino > Programming Questions > Serial commands help
arduino
Comments
Post a Comment