-1

i have a strange problem :o i make a connection between two xbee when i click on a button a led connected to the pin 13 light on and then the xbee coordinator send an information to switch on a led connected to the pin D3 of the xbee router. the problem is when i click on the button some times the led switch on sometimes not. i didn't know the problem is in the code or it is just a connection problem

int led = 13;
const int bouton = 2;
boolean state;
boolean laststate; 
void setup() {
  // put your setup code here, to run once:
pinMode(led, OUTPUT);
Serial.begin(9600);
pinMode(bouton, INPUT);
  digitalWrite(led, LOW);


}
void loop() {
  // put your main code here, to run repeatedly:
 state = digitalRead(bouton);
  digitalWrite(led, state);
  if (state == HIGH) {
    Serial.println("on");
    setRemoteState(5);
    delay(5000);
  } else {
    Serial.println("off");
    setRemoteState(4);
    delay(5000);

  }
}  

void setRemoteState(char value){
  Serial.write(0x7E); // start byte
  Serial.write((byte)0x0);
  Serial.write(0x10);
  Serial.write(0x17);
  Serial.write((byte)0x0);
  // id of recipient or use 0xFFFF for broadcast
  Serial.write((byte)00);
  Serial.write((byte)00);
  Serial.write((byte)00);
  Serial.write((byte)00);
  Serial.write((byte)00);
  Serial.write((byte)00);
  Serial.write(0xFF);
  Serial.write(0xFF);
  // 16 bit of reciepent 
  Serial.write(0xFF);
  Serial.write(0xFE);  
  
   Serial.write(0x02); 
   
   Serial.write('D');
   Serial.write('2');
   
   Serial.write(value);
   
   long sum = 0x17 + 0xFF + 0xFF + 0xFF + 0xFE + 0x02 + 'D' + '2' + value;
   Serial.write(0xFF - ( sum & 0xFF) );
   Serial.println(sum,HEX);
}
   
   
sawi
  • 19
  • 2

2 Answers2

0

It looks like the problem is in the delay(5000) the micro-controller will wait 5 seconds between a sample of the button's state. if you remove the delay statement it should be on and off instantly.

you should try to trigger the setRemoteState only on state change, so it will not send it constantly. something like

loop() {
state = digitalRead(bouton);
  digitalWrite(led, state);
if(state != lastState)
{
  if (state == HIGH) {
    Serial.println("on");
    setRemoteState(5);
  } else {
    Serial.println("off");
    setRemoteState(4);

  }
}
  lastState = state
}
assaf
  • 210
  • 2
  • 9
  • you should try to trigger the setRemoteState only on state change, so it will not send it constantly. – assaf Apr 28 '15 at 20:14
  • haw can i make this ! i didn't understand you very well – sawi Apr 28 '15 at 20:16
  • it still always a problem of synchronization between the leds – sawi Apr 28 '15 at 20:20
  • this is going to be more strange !! i get this error "'lastState' was not declared in this scope Erreur lors de la compilation" – sawi Apr 28 '15 at 20:40
  • i get the error ! in my script i use laststate and in the one you wrote it's lastState! and every things works ok thank you – sawi Apr 28 '15 at 20:52
  • i don't know if it can works or not ! but actually my project is about to send a string to the arduino to command the xbee !! for example when we write on the led is going to switch on and when we write off it have to switch off ! for that purpose i will use an arduino mega because i need to use two serial port one for writing and one for sending the information to the other xbee ! can you help how can do this? – sawi Apr 28 '15 at 21:05
  • you can poll the serial connection, and trigger an the "setRemoteState" event when the sequence of characters is arrived. – assaf Apr 28 '15 at 21:08
-1

i think about a script like this one !

String inputString;
int led = 13;
boolean state;
boolean laststate; 
void setup() {
  // put your setup code here, to run once:
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial1.begin(9600);

}
void loop() {
while (Serial.available() ) {
    // get the new byte:
    delay(3);  
    char inChar = Serial.read();
    // add it to the inputString:
    inputString += inChar;
    }
  if (inputString.length() >0) {
    Serial.println(inputString);
if (inputString == "on"){
 digitalWrite(led,HIGH);
  setRemoteState(5);

}

if (inputString == "off"){
 digitalWrite(led,LOW);
setRemoteState(4);

}
inputString=""; 
}
}

void setRemoteState(char value){
  Serial1.write(0x7E); // start byte
  Serial1.write((byte)0x0);
  Serial1.write(0x10);
  Serial1.write(0x17);
  Serial1.write((byte)0x0);
  // id of recipient or use 0xFFFF for broadcast
  Serial1.write((byte)00);
  Serial1.write((byte)00);
  Serial1.write((byte)00);
  Serial1.write((byte)00);
  Serial1.write((byte)00);
  Serial1.write((byte)00);
  Serial1.write(0xFF);
  Serial1.write(0xFF);
  // 16 bit of reciepent 
  Serial1.write(0xFF);
  Serial1.write(0xFE);  
  
   Serial1.write(0x02); 
   
   Serial1.write('D');
   Serial1.write('2');
   
   Serial1.write(value);
   
   long sum = 0x17 + 0xFF + 0xFF + 0xFF + 0xFE + 0x02 + 'D' + '2' + value;
   Serial1.write(0xFF - ( sum & 0xFF) );
   Serial1.print(sum,HEX);
}
   
   
sawi
  • 19
  • 2
  • looks ok, In general, you should avoid using delays in this kind of applications, it could block the hardware polling, and cause "overruns" (depends on how arduino implemented there Serial class). in addition, you should reset the inputString only when a valid command arrives, or - you should read the serial until an escape char is arrived. otherwise, it will reset on every char. – assaf Apr 28 '15 at 21:44