arduino - the led didn't switch on -
i have strange problem :o make connection between 2 xbee when click on button led connected pin 13 light on , xbee coordinator send information switch on led connected pin d3 of xbee router. problem when click on button times led switch on not. didn't know problem in code or connection problem
int led = 13; const int bouton = 2; boolean state; boolean laststate; void setup() { // put setup code here, run once: pinmode(led, output); serial.begin(9600); pinmode(bouton, input); digitalwrite(led, low); } void loop() { // put main code here, 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 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); }
it looks problem in delay(5000) micro-controller wait 5 seconds between sample of button's state. if remove delay statement should on , off instantly.
you should try trigger setremotestate on state change, not send constantly. 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 }
Comments
Post a Comment