PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Series1_Rx.pde 2.8KB

3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Copyright (c) 2009 Andrew Rapp. All rights reserved.
  3. *
  4. * This file is part of XBee-Arduino.
  5. *
  6. * XBee-Arduino is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * XBee-Arduino is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with XBee-Arduino. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <XBee.h>
  20. /*
  21. This example is for Series 1 XBee (802.15.4)
  22. Receives either a RX16 or RX64 packet and sets a PWM value based on packet data.
  23. Error led is flashed if an unexpected packet is received
  24. */
  25. XBee xbee = XBee();
  26. XBeeResponse response = XBeeResponse();
  27. // create reusable response objects for responses we expect to handle
  28. Rx16Response rx16 = Rx16Response();
  29. Rx64Response rx64 = Rx64Response();
  30. int statusLed = 11;
  31. int errorLed = 12;
  32. int dataLed = 10;
  33. uint8_t option = 0;
  34. uint8_t data = 0;
  35. void flashLed(int pin, int times, int wait) {
  36. for (int i = 0; i < times; i++) {
  37. digitalWrite(pin, HIGH);
  38. delay(wait);
  39. digitalWrite(pin, LOW);
  40. if (i + 1 < times) {
  41. delay(wait);
  42. }
  43. }
  44. }
  45. void setup() {
  46. pinMode(statusLed, OUTPUT);
  47. pinMode(errorLed, OUTPUT);
  48. pinMode(dataLed, OUTPUT);
  49. // start serial
  50. Serial.begin(9600);
  51. xbee.setSerial(Serial);
  52. flashLed(statusLed, 3, 50);
  53. }
  54. // continuously reads packets, looking for RX16 or RX64
  55. void loop() {
  56. xbee.readPacket();
  57. if (xbee.getResponse().isAvailable()) {
  58. // got something
  59. if (xbee.getResponse().getApiId() == RX_16_RESPONSE || xbee.getResponse().getApiId() == RX_64_RESPONSE) {
  60. // got a rx packet
  61. if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
  62. xbee.getResponse().getRx16Response(rx16);
  63. option = rx16.getOption();
  64. data = rx16.getData(0);
  65. } else {
  66. xbee.getResponse().getRx64Response(rx64);
  67. option = rx64.getOption();
  68. data = rx64.getData(0);
  69. }
  70. // TODO check option, rssi bytes
  71. flashLed(statusLed, 1, 10);
  72. // set dataLed PWM to value of the first byte in the data
  73. analogWrite(dataLed, data);
  74. } else {
  75. // not something we were expecting
  76. flashLed(errorLed, 1, 25);
  77. }
  78. } else if (xbee.getResponse().isError()) {
  79. //nss.print("Error reading packet. Error code: ");
  80. //nss.println(xbee.getResponse().getErrorCode());
  81. // or flash error led
  82. }
  83. }