Guest User

Untitled

a guest
May 3rd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1.  
  2. #include <IRremote.h>
  3.  
  4. /*
  5.  
  6. Send infrared commands from the Arduino to the iRobot Roomba
  7. by probono
  8.  
  9. 2013-03-17 Initial release
  10.  
  11. Copyright (c) 2013 by probono
  12. All rights reserved.
  13.  
  14. Redistribution and use in source and binary forms, with or without
  15. modification, are permitted provided that the following conditions are met:
  16.  
  17. 1. Redistributions of source code must retain the above copyright notice, this
  18. list of conditions and the following disclaimer.
  19. 2. Redistributions in binary form must reproduce the above copyright notice,
  20. this list of conditions and the following disclaimer in the documentation
  21. and/or other materials provided with the distribution.
  22.  
  23. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  27. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  30. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33.  
  34. IR Remote Control
  35. 129 Left
  36. 130 Forward
  37. 131 Right
  38. 132 Spot
  39. 133 Max
  40. 134 Small
  41. 135 Medium
  42. 136 Large / Clean
  43. 137 Stop
  44. 138 Power
  45. 139 Arc Left
  46. 140 Arc Right
  47. 141 Stop
  48.  
  49. Scheduling Remote
  50. 142 Download
  51. 143 Seek Dock
  52.  
  53. Roomba Discovery Driveon Charger
  54. 240 Reserved
  55. 248 Red Buoy
  56. 244 Green Buoy
  57. 242 Force Field
  58. 252 Red Buoy and Green Buoy
  59. 250 Red Buoy and Force Field
  60. 246 Green Buoy and Force Field
  61. 254 Red Buoy, Green Buoy and Force
  62.  
  63. Roomba 500 Drive-on Charger
  64. 160 Reserved
  65. 161 Force Field
  66. 164 Green Buoy
  67. 165 Green Buoy and Force Field
  68. 168 Red Buoy
  69. 169 Red Buoy and Force Field
  70. 172 Red Buoy and Green Buoy
  71. 173 Red Buoy, Green Buoy and Force Field
  72.  
  73. Roomba 500 Virtual Wall
  74. 162 Virtual Wall
  75.  
  76. Roomba 500 Virtual Wall Lighthouse ###### (FIXME: not yet supported here)
  77. 0LLLL0BB
  78. LLLL = Virtual Wall Lighthouse ID
  79. (assigned automatically by Roomba
  80. 560 and 570 robots)
  81. 1-10: Valid ID
  82. 11: Unbound
  83. 12-15: Reserved
  84. BB = Which Beam
  85. 00 = Fence
  86. 01 = Force Field
  87. 10 = Green Buoy
  88. 11 = Red Buoy
  89.  
  90. */
  91.  
  92. IRsend irsend; // hardwired to pin 3; use a transistor to drive the IR LED for maximal range
  93.  
  94. void setup()
  95. {
  96. Serial.begin(9600);
  97. }
  98.  
  99. void loop()
  100. {
  101. // Read 3-digit code from serial line and send it via IR
  102. char buffer[] = {
  103. ' ',' ',' ' }; // Receive up to 3 bytes
  104. while (!Serial.available()); // Wait for characters
  105. Serial.readBytesUntil('\n', buffer, 3);
  106. int incomingValue = atoi(buffer);
  107. if(incomingValue > 0) roomba_send(incomingValue);
  108. delay(1000);
  109. }
  110.  
  111. void roomba_send(int code)
  112. {
  113. Serial.print("Sending Roomba code ");
  114. Serial.print(code);
  115. int length = 8;
  116. unsigned int raw[length*2];
  117. unsigned int one_pulse = 3000;
  118. unsigned int one_break = 1000;
  119. unsigned int zero_pulse = one_break;
  120. unsigned int zero_break = one_pulse;
  121.  
  122. int arrayposition = 0;
  123. // Serial.println("");
  124. for (int counter = length-1; counter >= 0; --counter) {
  125. if(code & (1<<counter)) {
  126. // Serial.print("1");
  127. raw[arrayposition] = one_pulse;
  128. raw[arrayposition+1] = one_break;
  129. }
  130. else {
  131. // Serial.print("0");
  132. raw[arrayposition] = zero_pulse;
  133. raw[arrayposition+1] = zero_break;
  134. }
  135. arrayposition = arrayposition + 2;
  136. }
  137. for (int i = 0; i < 3; i++) {
  138. irsend.sendRaw(raw, 15, 38);
  139. delay(50);
  140. }
  141. Serial.println("");
  142.  
  143. /*
  144. Serial.print("Raw timings:");
  145. for (int z=0; z<length*2; z++) {
  146. Serial.print(" ");
  147. Serial.print(raw[z]);
  148. }
  149. Serial.print("\n\n");
  150. */
  151. }
Add Comment
Please, Sign In to add comment