LearnCode.ino 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Demo for RF remote switch receiver.
  3. * This example is for the new KaKu / Home Easy type of remotes!
  4. *
  5. * For details, see NewRemoteReceiver.h!
  6. *
  7. * With this sketch you can control a LED connected to digital pin 13,
  8. * after the sketch learned the code. After start, the LED starts to blink,
  9. * until a valid code has been received. The led stops blinking. Now you
  10. * can control the LED with the remote.
  11. *
  12. * Note: only unit-switches are supported in this sketch, no group or dim.
  13. *
  14. * Set-up: connect the receiver to digital pin 2 and a LED to digital pin 13.
  15. */
  16. #include <NewRemoteReceiver.h>
  17. boolean codeLearned = false;
  18. unsigned long learnedAddress;
  19. byte learnedUnit;
  20. void setup() {
  21. // LED-pin as output
  22. pinMode(13, OUTPUT);
  23. // Init a new receiver on interrupt pin 0, minimal 2 identical repeats, and callback set to processCode.
  24. NewRemoteReceiver::init(0, 2, processCode);
  25. }
  26. void loop() {
  27. // Blink led until a code has been learned
  28. if (!codeLearned) {
  29. digitalWrite(13, HIGH);
  30. delay(500);
  31. digitalWrite(13, LOW);
  32. delay(500);
  33. }
  34. }
  35. // Callback function is called only when a valid code is received.
  36. void processCode(NewRemoteCode receivedCode) {
  37. // A code has been received.
  38. // Do we already know the code?
  39. if (!codeLearned) {
  40. // No! Let's learn the received code.
  41. learnedAddress = receivedCode.address;
  42. learnedUnit = receivedCode.unit;
  43. codeLearned = true;
  44. } else {
  45. // Yes!
  46. // Is the received code identical to the learned code?
  47. if (receivedCode.address == learnedAddress && receivedCode.unit == learnedUnit) {
  48. // Yes!
  49. // Switch the LED off if the received code was "off".
  50. // Anything else (on, dim, on_with_dim) will switch the LED on.
  51. if (receivedCode.switchType == NewRemoteCode::off) {
  52. digitalWrite(13, LOW);
  53. } else {
  54. digitalWrite(13, HIGH);
  55. }
  56. }
  57. }
  58. }