#include #include #include #define READ_REMOTE_EVERY_MS 400 #define RED_DIODE_INTERVAL 500 #define rxPin 0 #define txPin 1 RemoteTX remote; NewSoftSerial serial(rxPin, txPin); XBee xbee(true, serial); // remote is acting as master; but cannot handle serial as parameter - old arduino unsigned int xbeeCommand[XBEE_SEND_COMMAND_LENGTH]; unsigned long lastTime; // for blinking int redDiodeVal; // for blinking boolean wasConnected; /**********************************************************************************/ /** * Preparation part */ void setup() { lastTime = 0; redDiodeVal = 0; wasConnected = false; } /**********************************************************************************/ /** * Main loop of the remote control */ void loop() { unsigned long time = millis(); /* Process XBEE */ xbee.loop(); /* Check connectivity */ if ( !xbee.isConnected() ) { // blink red led if( time - lastTime >= RED_DIODE_INTERVAL ) { redDiodeVal = 1 - redDiodeVal; // negate value remote.setRedDiode(redDiodeVal); lastTime = time; } wasConnected = false; return; // was not connected and now is }else if( wasConnected==false ){ // turn off red diode remote.setRedDiode(LOW); wasConnected = true; } /* Read remote control */ if( time - lastTime >= READ_REMOTE_EVERY_MS ) { lastTime = time; remote.loop(); } /* If not sending, get command and send it if is any */ if( xbee.isReadyToSend() && !remote.isEmpty() ) { // read and split into two 8bits values unsigned int command = remote.getNextCommand(); xbeeCommand[0] = command >> 8; xbeeCommand[1] = command & 0xFF; // send it xbee.sendCommand(xbeeCommand); } }