Advertisement
Guest User

Untitled

a guest
May 24th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #ifndef CarLib_h
  2. #define CarLib_h
  3.  
  4. #include "Arduino.h"
  5.  
  6. void initialize();
  7. int getDistance(int pin);
  8. void updateMotors(int Mleft, int Mright);
  9. void updateSpeed(int left, int right);
  10.  
  11. void Forward(int del);
  12. void Backward(int del);
  13. void TurnRight(int del);
  14. void TurnLeft(int del);
  15. void Stop(int del);
  16.  
  17. #endif
  18.  
  19.  
  20.  
  21.  
  22.  
  23. -
  24.  
  25.  
  26.  
  27. #include "Arduino.h"
  28. #include "CarLib.h"
  29.  
  30. int MSleft = 0;
  31. int MSright = 0;
  32.  
  33. void updateSpeed(int left, int right)
  34. {
  35. MSleft = abs(left);
  36. MSright = abs(right);
  37. updateMotors(left, right);
  38. }
  39.  
  40. void initialize()
  41. {
  42. Serial.begin(9600);
  43.  
  44. pinMode(A0, OUTPUT);
  45. pinMode(A1, INPUT);
  46. pinMode(A2, INPUT);
  47. pinMode(A3, INPUT);
  48. pinMode(A6, INPUT);
  49. pinMode(A7, INPUT);
  50.  
  51. pinMode(4, OUTPUT);
  52. pinMode(5, OUTPUT);
  53. pinMode(6, OUTPUT);
  54.  
  55. pinMode(7, OUTPUT);
  56. pinMode(8, OUTPUT);
  57. pinMode(9, OUTPUT);
  58. }
  59.  
  60. int getDistance(int pin)
  61. {
  62. digitalWrite(A0, LOW);
  63. delayMicroseconds(2);
  64. digitalWrite(A0, HIGH);
  65. delayMicroseconds(10);
  66. digitalWrite(A0, LOW);
  67. int duration;
  68. switch(pin)
  69. {
  70. case 1: duration = pulseIn(A1, HIGH); break;
  71. case 2: duration = pulseIn(A2, HIGH); break;
  72. case 3: duration = pulseIn(A3, HIGH); break;
  73. case 4: duration = pulseIn(A6, HIGH); break;
  74. case 5: duration = pulseIn(A7, HIGH); break;
  75. }
  76. digitalWrite(A0, LOW);
  77. return (duration / 58.2);
  78. }
  79.  
  80. void updateMotors(int Mleft, int Mright)
  81. {
  82. if (Mleft > 0)
  83. {
  84. analogWrite(9, abs(Mleft));
  85. digitalWrite(8, LOW);
  86. digitalWrite(7, HIGH);
  87. }
  88. else if (Mleft < 0)
  89. {
  90. analogWrite(9, abs(Mleft));
  91. digitalWrite(8, HIGH);
  92. digitalWrite(7, LOW);
  93. }
  94. else
  95. {
  96. analogWrite(9, 0);
  97. digitalWrite(8, LOW);
  98. digitalWrite(7, LOW);
  99. }
  100.  
  101. if (Mright > 0)
  102. {
  103. analogWrite(6, abs(Mright));
  104. digitalWrite(5, LOW);
  105. digitalWrite(4, HIGH);
  106. }
  107. else if (Mright < 0)
  108. {
  109. analogWrite(6, abs(Mright));
  110. digitalWrite(5, HIGH);
  111. digitalWrite(4, LOW);
  112. }
  113. else
  114. {
  115. analogWrite(6, 0);
  116. digitalWrite(5, LOW);
  117. digitalWrite(4, LOW);
  118. }
  119. }
  120.  
  121. void TurnRight(int del)
  122. {
  123. updateMotors(MSleft, -MSright);
  124. delay(del);
  125. }
  126.  
  127. void TurnLeft(int del)
  128. {
  129. updateMotors(-MSleft, MSright);
  130. delay(del);
  131. }
  132.  
  133. void Forward(int del)
  134. {
  135. updateMotors(MSleft, MSright);
  136. delay(del);
  137. }
  138.  
  139. void Backward(int del)
  140. {
  141. updateMotors(-MSleft, -MSright);
  142. delay(del);
  143. }
  144.  
  145. void Stop(int del)
  146. {
  147. updateMotors(0, 0);
  148. delay(del);
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement