Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. LiquidCrystal_I2C lcd(0x27, 16, 2);
  4.  
  5. //Motor pins
  6. int motorLeftBackward = 2 ;
  7. int motorLeftForward = 3;
  8. int motorRightBackward = 4;
  9. int motorRightForward = 9;
  10.  
  11. //Echo pins
  12. int ultraSoundTrigger = 13; // Ultrasound signal pin
  13. int ultraSoundEcho = 12; // Ultrasound signal pin
  14. long ultraSoundValue, cm;
  15.  
  16. //Infrared pins
  17. int infraLeft = 10;
  18. int infraRight = 11;
  19. int ledLight = 13;
  20.  
  21. int i = 0;
  22. char input;
  23. const int MPU_addr = 0x68; // I2C address of the MPU-6050
  24. int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
  25. int roll = 2300;
  26. int pitch = 2300;
  27.  
  28. void forward(int time, int speed)
  29. {
  30. analogWrite(motorRightForward, speed + 30);
  31. analogWrite(motorLeftForward, speed - 10 );
  32. delay(time);
  33. if (time > 0) {
  34. stop();
  35. }
  36. delay(50);
  37. }
  38.  
  39. void backward(int time)
  40. {
  41. digitalWrite(motorRightBackward, HIGH);
  42. digitalWrite(motorLeftBackward, HIGH);
  43. delay(time);
  44. if (time > 90) {
  45. stop();
  46. }
  47. delay(50);
  48. }
  49.  
  50. void turn(String direction, int amount)
  51. {
  52. if (direction == "right") {
  53. digitalWrite(motorRightBackward, HIGH);
  54. digitalWrite(motorLeftForward, HIGH);
  55. delay(amount);
  56. stop();
  57. }
  58. else if (direction == "left") {
  59. digitalWrite(motorRightForward, HIGH);
  60. digitalWrite(motorLeftBackward, HIGH);
  61. delay(amount);
  62. stop();
  63. } else {
  64. Serial.println("Direction needs to be 'left' or 'right'");
  65. }
  66. delay(50);
  67. }
  68.  
  69. void stop()
  70. {
  71. digitalWrite(motorRightForward, LOW);
  72. digitalWrite(motorLeftForward, LOW);
  73. digitalWrite(motorRightBackward, LOW);
  74. digitalWrite(motorLeftBackward, LOW);
  75. }
  76.  
  77. int speed()
  78. {
  79. ping();
  80. int speed = 205 - cm;
  81. if (speed < 80) {
  82. speed = 90;
  83. }
  84.  
  85. return speed;
  86. }
  87.  
  88. unsigned long ping() //read ultrasound
  89. {
  90. digitalWrite(ultraSoundTrigger, LOW); // Send low pulse
  91. delayMicroseconds(5);
  92. digitalWrite(ultraSoundTrigger, HIGH); // Send high pulse
  93. delayMicroseconds(10);
  94. digitalWrite(ultraSoundTrigger, LOW); // Holdoff
  95. pinMode(ultraSoundEcho, INPUT);
  96.  
  97. ultraSoundValue = pulseIn(ultraSoundEcho, HIGH);
  98. cm = (ultraSoundValue / 2) / 29.1;
  99. if (cm < 2500 && !cm == 0 ) {
  100. Serial.println (cm);
  101. delay (100);
  102. return cm;
  103. }
  104. }
  105.  
  106. int irRead(int readPin)
  107. {
  108. int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
  109. int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond
  110. int i;
  111. for (i = 0; i <= cycles; i++)
  112. {
  113. pinMode(readPin, OUTPUT);
  114. digitalWrite(readPin, HIGH);
  115. delayMicroseconds(halfPeriod);
  116.  
  117. pinMode(readPin, INPUT);
  118.  
  119. digitalWrite(readPin, LOW);
  120. delayMicroseconds(halfPeriod - 1); // - 1 to make up for digitaWrite overhead
  121. }
  122. return digitalRead(readPin);
  123. }
  124.  
  125. void irCheck()
  126. {
  127. Serial.print(irRead(infraLeft));
  128. Serial.print("\t");
  129. Serial.print(irRead(infraRight));
  130. Serial.print("\n");
  131. delay(10);
  132. }
  133.  
  134. void gyrMeter() {
  135. ping();
  136. Wire.beginTransmission(MPU_addr);
  137. Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  138. Wire.endTransmission(false);
  139. Wire.requestFrom(MPU_addr, 14, true); // request a total of 14 registers
  140. AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  141. AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  142. AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  143. Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  144. GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  145. GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  146. GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  147.  
  148. if (cm >= 6 && cm < 200) {
  149. lcd.clear();
  150. if (AcX <= -roll) {
  151. lcd.setCursor(0, 0);
  152. lcd.print("Roll-r");
  153. } else if (AcX >= roll) {
  154. lcd.setCursor(0, 0);
  155. lcd.print("Roll-l");
  156. } else {
  157. lcd.setCursor(0, 0);
  158. lcd.print("No-roll");
  159. }
  160. if (AcY <= -pitch) {
  161. lcd.setCursor(8, 0);
  162. lcd.print("Tilt-fw");
  163. } else if (AcY >= pitch) {
  164. lcd.setCursor(8, 0);
  165. lcd.print("Tilt-bw");
  166. } else {
  167. lcd.setCursor(8, 0);
  168. lcd.print("No-tilt");
  169. }
  170. lcd.setCursor(0, 1);
  171. lcd.print(round(cm));
  172. lcd.print ("cm.");
  173. }
  174.  
  175. Serial.print("AcX = "); Serial.print(AcX);
  176. Serial.print(" | AcY = "); Serial.print(AcY);
  177. Serial.print(" | AcZ = "); Serial.print(AcZ);
  178. Serial.print(" | Tmp = "); Serial.print(Tmp / 340.00 + 36.53); //equation for temperature in degrees C from datasheet
  179. Serial.print(" | GyX = "); Serial.print(GyX);
  180. Serial.print(" | GyY = "); Serial.print(GyY);
  181. Serial.print(" | GyZ = "); Serial.println(GyZ);
  182. }
  183.  
  184. void linestay()
  185. {
  186.  
  187. if (irRead(infraLeft) == 0 && irRead(infraRight) == 0) {
  188. forward(0, speed());
  189. }
  190. else if (irRead(infraLeft) == 1 && irRead (infraRight) == 1) {
  191. stop();
  192. backward(300);
  193. turn("right", 800);
  194. }
  195. else if (irRead(infraLeft) == 1) {
  196. stop();
  197. backward(300);
  198. turn("left", 500);
  199. }
  200. else if (irRead(infraRight) == 1) {
  201. stop();
  202. backward(300);
  203. turn("right", 500);
  204. }
  205. }
  206.  
  207. void linefollow()
  208. {
  209. if (irRead(infraLeft) == 0 && irRead(infraRight) == 0) {
  210. forward(0, 70);
  211. }
  212. else if (irRead(infraLeft) == 1 && irRead (infraRight) == 1) {
  213. forward(0, 70);
  214. }
  215. else if (irRead(infraLeft) == 1) {
  216. stop();
  217. turn("left", 20);
  218. }
  219. else if (irRead(infraRight) == 1) {
  220. stop();
  221. turn("right", 20);
  222. }
  223. }
  224.  
  225. void setup() {
  226. pinMode(motorLeftForward, OUTPUT);
  227. pinMode(motorLeftBackward, OUTPUT);
  228. pinMode(motorRightForward, OUTPUT);
  229. pinMode(motorRightBackward, OUTPUT);
  230. pinMode(ultraSoundTrigger, OUTPUT);
  231. pinMode(ultraSoundEcho, INPUT);
  232. pinMode(ledLight, OUTPUT);
  233.  
  234. Wire.begin();
  235. Wire.beginTransmission(MPU_addr);
  236. Wire.write(0x6B); // PWR_MGMT_1 register
  237. Wire.write(0); // set to zero (wakes up the MPU-6050)
  238. Wire.endTransmission(true);
  239. lcd.begin();
  240. }
  241.  
  242. void loop() {
  243. //gyrMeter();
  244. forward(0, 70);
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement