Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.45 KB | None | 0 0
  1. #include <QTRSensors.h>
  2.  
  3. // Description: Line sensing robot with object avoidance
  4. // Written By: Michael Lindley
  5. // Date: 17/11/2019
  6.  
  7. //Libraries
  8. #include <NewPing.h>
  9. #include <Adafruit_MotorShield.h>
  10. #include <Wire.h>
  11. #include <LiquidCrystal.h>
  12.  
  13. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  14. ////////
  15. #define NUM_SENSORS 8 // number of sensors used
  16. #define NUM_SAMPLES_PER_SENSOR 4 // average 4 analog samples per sensor reading
  17. #define EMITTER_PIN 2 // emitter is controlled by digital pin 2
  18. // Pins defined for Ultrasonic Sensor HC-SR04
  19. // Sensor 1 - Middle
  20. #define Gnd_PIN 30 //Ground PIN
  21. #define Echo_PIN 32 //Signal Out PIN
  22. #define Trig_PIN 34 //Return Signal
  23. #define Vcc_PIN 36 //5vdc supply
  24. #define Max_Dist1 200 //Setting Max Distance uS can sense for
  25. // Sensor 2 - LH Side
  26. #define Gnd2_PIN 22 //Ground PIN
  27. #define Echo2_PIN 24 //Signal Out PIN
  28. #define Trig2_PIN 26 //Return Signal
  29. #define Vcc2_PIN 28 //5vdc supply
  30. #define Max_Dist2 200 //Setting Max Distance uS can sense for
  31. // Sensor 3 - RH Side
  32. #define Gnd3_PIN 38 //Ground PIN
  33. #define Echo3_PIN 40 //Signal Out PIN
  34. #define Trig3_PIN 42 //Return Signal
  35. #define Vcc3_PIN 44 //5vdc supply
  36. #define Max_Dist3 200 //Setting Max Distance uS can sense for
  37.  
  38. // sensors 0 through 5 are connected to analog inputs 0 through 5, respectively
  39. QTRSensorsAnalog qtra((unsigned char[]) {15, 14, 13, 12, 11, 10, 9, 8},
  40. NUM_SENSORS, NUM_SAMPLES_PER_SENSOR, EMITTER_PIN);
  41. unsigned int sensorValues[NUM_SENSORS];
  42. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  43. Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1);
  44. Adafruit_DCMotor *myMotor3 = AFMS.getMotor(3);
  45.  
  46. int dark = 800;
  47. int Program = 0;
  48. int Distance1; //Ultrasonic sensor 1
  49. int Distance2; //Ultrasonic sensor 2
  50. int Distance3; //Ultrasonic sensor 3
  51.  
  52. NewPing sonar1(Trig_PIN, Echo_PIN, Max_Dist1); // NewPing setup of pins and maximum distance.
  53. NewPing sonar2(Trig2_PIN, Echo2_PIN, Max_Dist2); // NewPing setup of pins and maximum distance.
  54. NewPing sonar3(Trig3_PIN, Echo3_PIN, Max_Dist3); // NewPing setup of pins and maximum distance.
  55.  
  56. void setup() {
  57. Serial.begin(9600); // set up Serial library at 9600 bps
  58. ////////////
  59. lcd.begin(16, 2); // start the library
  60. lcd.setCursor(0,0);
  61. lcd.print("UltraSense");
  62. delay(1000); // delay for 1000 miliseconds
  63. lcd.clear();
  64. lcd.setCursor(0,0);
  65. lcd.print("Duration: ");
  66. lcd.setCursor(0,1);
  67. lcd.print("Distance: ");
  68.  
  69. //////////////////
  70. Serial.println("Adafruit Motorshield v2 - DC Motor test!");
  71. AFMS.begin(); // create with the default frequency 1.6KHz
  72. // Set the speed to start, from 0 (off) to 255 (max speed)
  73. myMotor1->run(RELEASE);
  74. myMotor3->run(RELEASE);
  75. /////////////////////////////////////////
  76. delay(500);
  77. pinMode(13, OUTPUT);
  78. digitalWrite(13, HIGH); // turn on Arduino's LED to indicate we are in calibration mode
  79.  
  80. Serial.println("Calibrating");
  81. for (int i = 0; i < 400; i++) // make the calibration take about 10 seconds
  82. {
  83. qtra.calibrate(); // reads all sensors 10 times at 2.5 ms per six sensors (i.e. ~25 ms per call)
  84. }
  85. digitalWrite(13, LOW); // turn off Arduino's LED to indicate we are through with calibration
  86.  
  87. // print the calibration minimum values measured when emitters were on
  88. Serial.begin(9600);
  89. for (int i = 0; i < NUM_SENSORS; i++)
  90. {
  91. Serial.print(qtra.calibratedMinimumOn[i]);
  92. Serial.print(' ');
  93. }
  94. Serial.println();
  95. // print the calibration maximum values measured when emitters were on
  96. for (int i = 0; i < NUM_SENSORS; i++)
  97. {
  98. Serial.print(qtra.calibratedMaximumOn[i]);
  99. Serial.print(' ');
  100. }
  101. Serial.println();
  102. //Serial.println();
  103. delay(1000);
  104. }
  105. {
  106. //Setting Pins to I/Os - Sensor 1
  107. pinMode(Gnd_PIN, OUTPUT); //Setting the ground pin to an output
  108. digitalWrite(Gnd_PIN, LOW); //Setting the ground pin low, 0v
  109. pinMode(Vcc_PIN, OUTPUT); //Setting the supply voltage to an output
  110. digitalWrite(Vcc_PIN, HIGH); //Setting the supply voltage to 5v
  111. pinMode(echoPin, INPUT); //Setting ECHO pin to an input
  112. pinMode(trigPin, OUTPUT); //Setting TRIGGER pin to an output
  113. //Setting Pins to I/Os - Sensor 2
  114. pinMode(Gnd2_PIN, OUTPUT); //Setting the ground pin to an output
  115. digitalWrite(Gnd2_PIN, LOW); //Setting the ground pin low, 0v
  116. pinMode(Vcc2_PIN, OUTPUT); //Setting the supply voltage to an output
  117. digitalWrite(Vcc2_PIN, HIGH); //Setting the supply voltage to 5v
  118. pinMode(echoPin2, INPUT); //Setting ECHO pin to an input
  119. pinMode(trigPin2, OUTPUT); //Setting TRIGGER pin to an output
  120. //Setting Pins to I/Os - Sensor 3
  121. pinMode(Gnd3_PIN, OUTPUT); //Setting the ground pin to an output
  122. digitalWrite(Gnd3_PIN, LOW); //Setting the ground pin low, 0v
  123. pinMode(Vcc3_PIN, OUTPUT); //Setting the supply voltage to an output
  124. digitalWrite(Vcc3_PIN, HIGH); //Setting the supply voltage to 5v
  125. pinMode(echoPin3, INPUT); //Setting ECHO pin to an input
  126. pinMode(trigPin3, OUTPUT); //Setting TRIGGER pin to an output
  127. }
  128. void loop() {
  129. {
  130. long Distance1, Distance2, Distance3;
  131. //Ultrasonic Sensor Serial Coms to display value
  132. delay(500);
  133. unsigned int uS1 = sonar1.ping(); // Send ping, get ping time in microseconds (uS).
  134. unsigned int uS2 = sonar2.ping(); // Send ping, get ping time in microseconds (uS).
  135. unsigned int uS3 = sonar3.ping(); // Send ping, get ping time in microseconds (uS).
  136. Distance1 = (uS1 / US_ROUNDTRIP_CM);
  137. Distance2 = (uS2 / US_ROUNDTRIP_CM);
  138. Distance3 = (uS3 / US_ROUNDTRIP_CM);
  139. Serial.print("Ping: ");
  140. Serial.print(Distance1); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  141. Serial.println("cm"); // Serial Display Units in Centimeters
  142. }
  143.  
  144. int dark = 800;
  145. unsigned int position = qtra.readLine(sensorValues);
  146. for (unsigned char i = 0; i < NUM_SENSORS; i++)
  147. {
  148. Serial.print(sensorValues[i]);
  149. Serial.print('\t');
  150. }
  151. Serial.println();
  152. ////////////////////////////
  153. while (Program == 0){
  154. if(sensorValues[4] > dark && sensorValues[3] > dark && sensorValues[0] > dark && sensorValues[1] > dark && sensorValues[2] > dark){
  155. myMotor1->run(FORWARD);
  156. myMotor3->run(FORWARD);
  157. myMotor1->setSpeed(75);
  158. myMotor3->setSpeed(25); // was 25
  159. lcd.setCursor(0,0);
  160. lcd.print("right 90 Degree ");
  161. delay(100);
  162. }
  163. else
  164. //// LINE FOLLOWER
  165. if(sensorValues[0] > dark || sensorValues[1] > dark || sensorValues[2] > dark){
  166. right();
  167. }
  168. if(sensorValues[7] > dark || sensorValues[6] > dark || sensorValues[5] > dark){
  169. left();
  170. }
  171. if(sensorValues[4] > dark && sensorValues[3] > dark){
  172. foward();
  173. }
  174. }
  175. if(sensorValues[4] > dark && sensorValues[3] > dark && sensorValues[7] > dark && sensorValues[6] > dark && sensorValues[5] > dark){
  176. myMotor1->run(BACKWARD);
  177. myMotor3->run(BACKWARD);
  178. myMotor1->setSpeed(25);
  179. myMotor3->setSpeed(75); // was 25
  180. lcd.setCursor(0,0);
  181. lcd.print("left 90 Degree ");
  182. delay(100);
  183. }
  184. else{
  185. //// LINE FOLLOWER
  186. if(sensorValues[0] > dark || sensorValues[1] > dark || sensorValues[2] > dark){
  187. right();
  188. }
  189. if(sensorValues[7] > dark || sensorValues[6] > dark || sensorValues[5] > dark){
  190. left();
  191. }
  192. if(sensorValues[4] > dark && sensorValues[3] > dark){
  193. foward();
  194. }
  195. }
  196.  
  197. // If the Ultrasonic Sensor 1 detects an object less than 10cm, a 1 is set in a Data Register to initiate Obstacle Avoidance
  198. while (Program == 0) {
  199. if (Distance1 <= 10){
  200. Program = Program +1; // Detect obstacle at <=10cm - Set 1 in Data Register
  201. } else {
  202. Program = 0;
  203. }
  204. }
  205. // If 1 in Data Register - Activate Object Avoidance Program.
  206. while (Program == 1){
  207. if (Distance3 >= 10){
  208. myMotor1->run(FORWARD);
  209. myMotor3->run(BACKWARD);
  210. myMotor1->setSpeed(25);
  211. myMotor3->setSpeed(90);
  212. } else
  213. if (Distance3 <= 10){ // Once uS3 detects object, +1 into Program register to move program on
  214. Program = Program +1 ;
  215. delay (500);
  216. }
  217. }
  218. while (Program == 2){
  219. if(sensorValues[4] > dark && sensorValues[3] > dark && sensorValues[7] > dark && sensorValues[6] > dark && sensorValues[5] > dark){
  220. Program = Program +1;
  221. delay (100);
  222. } else {
  223. myMotor1->run(FORWARD);
  224. myMotor3->run(BACKWARD);
  225. myMotor1->setSpeed(40);
  226. myMotor3->setSpeed(35);
  227. }
  228. }
  229. // Once line detected turn robot till middle sensors reading high and outer sensors reading low.
  230. while (Program == 3) {
  231. if(sensorValues[4] > dark && sensorValues[3] > dark && sensorValues[7] > dark && sensorValues[6] > dark && sensorValues[5] > dark){
  232. Program = Program+1;
  233. } else {
  234. myMotor1->run(FORWARD);
  235. myMotor3->run(BACKWARD);
  236. myMotor1->setSpeed(0);
  237. myMotor3->setSpeed(35);
  238. }
  239. }
  240. // Stop Object Avoidance Program - Set 0 in Data Register.
  241. if(Program == 4){
  242. Program = 0;
  243. } else
  244. if(Program > 4){
  245. Program = 0;
  246. end_if;
  247. }
  248. // Start Line Following Program.
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement