Advertisement
Boothy920Q

Code Failing

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