Advertisement
Boothy920Q

Code V2

Jan 2nd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1.  
  2. #include <IRremote.h>
  3. #include <IRremoteInt.h>
  4.  
  5.  
  6.  
  7. int IRpin = 9; // pin for the IR sensor
  8. IRrecv irrecv(IRpin);
  9. decode_results results;
  10.  
  11.  
  12. //Tell the Arduino where the sensor is hooked up
  13.  
  14.  
  15. int enableA = 11;
  16. int pinA1 = 6;
  17. int pinA2 = 5;
  18.  
  19. int enableB = 10;
  20. int pinB1 = 4;
  21. int pinB2 = 3;
  22.  
  23. long inches;
  24.  
  25. void setup() {
  26. pinMode(enableA, OUTPUT);
  27. pinMode(pinA1, OUTPUT);
  28. pinMode(pinA2, OUTPUT);
  29.  
  30. pinMode(enableB, OUTPUT);
  31. pinMode(pinB1, OUTPUT);
  32. pinMode(pinB2, OUTPUT);
  33.  
  34. Serial.begin(9600);
  35. irrecv.enableIRIn(); // Start the receiver
  36. }
  37.  
  38. //Define high-level H-bridge commands
  39. void enableMotors()
  40. {
  41. motorAOn();
  42. motorBOn();
  43. }
  44.  
  45. void disableMotors()
  46. {
  47. motorAOff();
  48. motorBOff();
  49. }
  50.  
  51. void forward(int time)
  52. {
  53. motorAForward();
  54. motorBForward();
  55. delay(time);
  56. }
  57.  
  58. void backward(int time)
  59. {
  60. motorABackward();
  61. motorBBackward();
  62. delay(time);
  63. }
  64.  
  65. void turnLeft(int time)
  66. {
  67. motorABackward();
  68. motorBForward();
  69. delay(time);
  70. }
  71.  
  72. void turnRight(int time)
  73. {
  74. motorAForward();
  75. motorBBackward();
  76. delay(time);
  77. }
  78.  
  79. void coast(int time)
  80. {
  81. motorACoast();
  82. motorBCoast();
  83. delay(time);
  84. }
  85.  
  86. void brake(int time)
  87. {
  88. motorABrake();
  89. motorBBrake();
  90. delay(time);
  91. }
  92. //Define low-level H-bridge commands
  93.  
  94. //enable motors
  95. void motorAOn()
  96. {
  97. digitalWrite(enableA, HIGH);
  98. }
  99.  
  100. void motorBOn()
  101. {
  102. digitalWrite(enableB, HIGH);
  103. }
  104.  
  105. //disable motors
  106. void motorAOff()
  107. {
  108. digitalWrite(enableB, LOW);
  109. }
  110.  
  111. void motorBOff()
  112. {
  113. digitalWrite(enableA, LOW);
  114. }
  115.  
  116. //motor A controls
  117. void motorAForward()
  118. {
  119. digitalWrite(pinA1, HIGH);
  120. digitalWrite(pinA2, LOW);
  121. }
  122.  
  123. void motorABackward()
  124. {
  125. digitalWrite(pinA1, LOW);
  126. digitalWrite(pinA2, HIGH);
  127. }
  128.  
  129. //motor B controls
  130. void motorBForward()
  131. {
  132. digitalWrite(pinB1, HIGH);
  133. digitalWrite(pinB2, LOW);
  134. }
  135.  
  136. void motorBBackward()
  137. {
  138. digitalWrite(pinB1, LOW);
  139. digitalWrite(pinB2, HIGH);
  140. }
  141.  
  142. //coasting and braking
  143. void motorACoast()
  144. {
  145. digitalWrite(pinA1, LOW);
  146. digitalWrite(pinA2, LOW);
  147. }
  148.  
  149. void motorABrake()
  150. {
  151. digitalWrite(pinA1, HIGH);
  152. digitalWrite(pinA2, HIGH);
  153. }
  154.  
  155. void motorBCoast()
  156. {
  157. digitalWrite(pinB1, LOW);
  158. digitalWrite(pinB2, LOW);
  159. }
  160.  
  161. void motorBBrake()
  162. {
  163. digitalWrite(pinB1, HIGH);
  164. digitalWrite(pinB2, HIGH);
  165. }
  166.  
  167. void loop() {
  168.  
  169. //Run the motors at slightly less than full power
  170. analogWrite(enableA, 200);
  171. analogWrite(enableB, 200);
  172.  
  173. if (irrecv.decode(&results)){ //this checks to see if a code has been received
  174.  
  175. if (results.value == 0xFF18E7){ //if the button press equals the hex value 0xC284
  176. analogWrite(enableA, 255);
  177. analogWrite(enableB, 255);
  178. forward(100);
  179. }
  180.  
  181. irrecv.resume(); //receive the next value
  182. }
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement