Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * --------------------------------------
- * Arduino with a 12V MagLock - XBee for comms. 0013A200 418D8E11
- * Also has a VL6180x/GY6180 Range sensor.
- * --------------------------------------
- *
- * PINOUT
- *
- * Maglock > MOSFET > Arduino
- * TODO
- *
- * XBEE > Arduino
- * 10 Gnd > Gnd
- * 1 VCC > 3.3V
- * 2 DOUT > 2
- * 3 DIN > 4
- *
- * VL6180x/GY6180 Range
- * VIN > 5V
- * GND > GNd
- * SCL > A5
- * SDA > A4
- * 0 > D5
- * TODO
- */
- #include <Wire.h>
- #include <XBee.h>
- #include <SoftwareSerial.h>
- #include <Printers.h>
- //XBEE & COMMUNICATIONS
- SoftwareSerial xbeeSerial(A0, A1); // RX, TX
- //Works with Series1 and 2
- XBeeWithCallbacks xbee;
- // Build a reuseable message packet to send to the Co-Ordinator
- XBeeAddress64 coordinatorAddr = XBeeAddress64(0x00000000, 0x00000000);
- uint8_t distanceMessagePayload[1] = {0};
- ZBTxRequest distanceMessage = ZBTxRequest(coordinatorAddr, distanceMessagePayload,
- sizeof(distanceMessagePayload));
- void setup() {
- Serial.begin(9600); // Initialize serial communications with the PC
- Wire.begin();
- // XBEE
- xbeeSerial.begin(9600);
- xbee.setSerial(xbeeSerial);
- // Make sure that any errors are logged to Serial. The address of
- // Serial is first cast to Print*, since that's what the callback
- // expects, and then to uintptr_t to fit it inside the data parameter.
- xbee.onPacketError(printErrorCb, (uintptr_t)(Print*)&Serial);
- xbee.onTxStatusResponse(printErrorCb, (uintptr_t)(Print*)&Serial);
- xbee.onZBTxStatusResponse(printErrorCb, (uintptr_t)(Print*)&Serial);
- // These are called when an actual packet received
- //xbee.onZBRxResponse(zbReceive, (uintptr_t)(Print*)&Serial);
- // Print any unhandled response with proper formatting
- xbee.onOtherResponse(printResponseCb, (uintptr_t)(Print*)&Serial);
- // Enable this to print the raw bytes for _all_ responses before they
- // are handled
- xbee.onResponse(printRawResponseCb, (uintptr_t)(Print*)&Serial);
- Serial.println("SETUP");
- }
- void loop() {
- // Continuously let xbee read packets and call callbacks.
- xbee.loop();
- SendTestPacket('a');
- delay(500);
- }
- void SendTestPacket( char msg)
- {
- distanceMessagePayload[0] = msg;
- distanceMessage.setFrameId(xbee.getNextFrameId());
- Serial.println("SENDING...");
- // Send the command and wait up to N ms for a response. xbee loop continues during this time.
- uint8_t status = xbee.sendAndWait(distanceMessage, 3000);
- if (status == 0)
- {
- Serial.println(F("SEND ACKNOWLEDGED"));
- }
- else
- { //Complain, but do not reset timeElapsed - so that a new packet comes in and
- //tried again immedietly.
- Serial.print(F("SEND FAILED: "));
- printHex(status, 2);
- Serial.println();
- }
- }
- // UTIL FUNCTIONS
- void printHex(int num, int precision) {
- char tmp[16];
- char format[128];
- sprintf(format, "0x%%.%dX", precision);
- sprintf(tmp, format, num);
- Serial.print(tmp);
- }
RAW Paste Data