InfraTrans.ino 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <IRremote.h>
  2. const int led = LED_BUILTIN; // On board LED, Pin 13
  3. const int RECV_PIN = 7; // IR Receiver
  4. String input = "";
  5. bool done = false;
  6. IRsend sendr; // Make Send Object, DANGER a fixed pin number is always used! (3)
  7. //IRrecv recvr(RECV_PIN); // Make Recever Object
  8. decode_results results; // Make Results Object to store received codes
  9. void setup() {
  10. // Initalize on board LED
  11. pinMode(led, OUTPUT);
  12. digitalWrite(led, LOW);
  13. // Initalize input routine
  14. input.reserve(60); // Max input of chars, to merge into string
  15. done = false;
  16. // Initalize IR connections
  17. //recvr.enableIRIn();
  18. //recvr.blink13(false); // Disabled Pin 13 blinking on IR signal received
  19. // Initalize Output routine
  20. Serial.begin(9600); // Serial COM's Port
  21. while(!Serial) { delay(10); } // Wait for conection
  22. Serial.println("Serial Connected!");
  23. digitalWrite(led, LOW);
  24. }
  25. void sendCode(long code, int bit_len, bool repeat) {
  26. // If repeat the code then repeat it 3 times
  27. if(repeat) {
  28. for(int x = 0; x <= 3; x++) {
  29. sendr.sendNEC(code, bit_len);
  30. delay(40);
  31. }
  32. } else {
  33. // Else send the code then repeat a blank 2 times
  34. sendr.sendNEC(code, bit_len);
  35. delay(40);
  36. for(int x = 0; x <= 2; x++) {
  37. sendr.sendNEC(0xFFFFFFFF, 32);
  38. delay(40);
  39. }
  40. }
  41. }
  42. void action(String act){
  43. // Given String execute action
  44. if(act == "tv p"){
  45. sendCode(0x57E3E817, 32, true);
  46. /*for(int x = 0; x <= 3; x++) {
  47. sendr.sendNEC(0x57E3E817, 32);
  48. delay(40);
  49. }*/
  50. Serial.println("tv power");
  51. }/* else if(act == "m p"){
  52. sendCode(0x807F02FD, 32, false);
  53. Serial.println("movie power");
  54. }*/
  55. }
  56. void loop() {
  57. // Are we sending/receiving data?
  58. if(Serial.available() > 0) {
  59. char in = (char)Serial.read();
  60. if (in == '\n') {
  61. done = true;
  62. } else if (in == '\n') {
  63. done = true;
  64. } else {
  65. input += in;
  66. }
  67. }
  68. if(done){
  69. action(input); // Execute command / handle command
  70. input = "";
  71. done = false;
  72. }
  73. }