Advertisement
Guest User

Final Project

a guest
Feb 20th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. const int trigPin = 9;
  2. const int echoPin = A3;
  3. long duration;
  4. int distance;
  5. #include <LiquidCrystal.h>
  6. LiquidCrystal lcd(8, 7, 6, 5, 4, 3);
  7. const int greenLed = A0;
  8. const int redLed = 2;
  9. #define enA 11
  10. #define in1 12
  11. #define in2 13
  12. #define enB 10
  13. #define in3 A2
  14. #define in4 A1
  15. int motorSpeedA = 0;
  16. int motorSpeedB = 0;
  17.  
  18. void setup()
  19. {
  20. pinMode(enA, OUTPUT);
  21. pinMode(enB, OUTPUT);
  22. pinMode(in1, OUTPUT);
  23. pinMode(in2, OUTPUT);
  24. pinMode(in3, OUTPUT);
  25. pinMode(in4, OUTPUT);
  26. pinMode(trigPin, OUTPUT);
  27. pinMode(echoPin, INPUT);
  28. lcd.begin(16, 2);
  29. Serial.begin(9600);
  30. pinMode (greenLed, OUTPUT);
  31. pinMode (redLed, OUTPUT);
  32. }
  33. void sensor ()
  34. {
  35. // Reads how far away an object is from the robot (cm)
  36. digitalWrite(trigPin, LOW);
  37. delayMicroseconds(2);
  38. digitalWrite(trigPin, HIGH);
  39. delayMicroseconds(10);
  40. digitalWrite(trigPin, LOW);
  41. duration = pulseIn(echoPin, HIGH);
  42. distance= duration*0.034/2;
  43. Serial.print("Distance: ");
  44. Serial.println(distance);
  45. }
  46. void lcdWords ()
  47. {
  48. // Words on the LCD and when they occur
  49. if (distance < 35 && distance > 5)
  50. {
  51. lcd.clear ();
  52. lcd.setCursor (5,0);
  53. lcd.print ("Excuse");
  54. lcd.setCursor (7,1);
  55. lcd.print ("Me");
  56. digitalWrite (redLed, HIGH);
  57. digitalWrite (greenLed, LOW);
  58. delay (500);
  59. }
  60. else if (distance < 15)
  61. {
  62. lcd.clear ();
  63. lcd.setCursor (6,0);
  64. lcd.print ("MOVE");
  65. delay (1000);
  66. lcd.setCursor (5,1);
  67. lcd.print ("please");
  68. digitalWrite (redLed, HIGH);
  69. digitalWrite (greenLed, LOW);
  70. delay (500);
  71. }
  72. else
  73. {
  74. lcd.clear();
  75. lcd.setCursor(5, 0);
  76. lcd.print("Hello");
  77. lcd.setCursor (3, 1);
  78. lcd.print ("I am Tobor");
  79. digitalWrite (greenLed, HIGH);
  80. digitalWrite (redLed, LOW);
  81. delay (500);
  82. }
  83. }
  84. void motors ()
  85. {
  86. int xAxis = analogRead(A5);
  87. int yAxis = analogRead(A4);
  88. if (yAxis < 470) {
  89. // Set Motor A backward
  90. digitalWrite(in1, HIGH);
  91. digitalWrite(in2, LOW);
  92. // Set Motor B backward
  93. digitalWrite(in3, HIGH);
  94. digitalWrite(in4, LOW);
  95. // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
  96. motorSpeedA = map(yAxis, 470, 0, 0, 255);
  97. motorSpeedB = map(yAxis, 470, 0, 0, 255);
  98. }
  99. else if (yAxis > 550) {
  100. // Set Motor A forward
  101. digitalWrite(in1, LOW);
  102. digitalWrite(in2, HIGH);
  103. // Set Motor B forward
  104. digitalWrite(in3, LOW);
  105. digitalWrite(in4, HIGH);
  106. // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
  107. motorSpeedA = map(yAxis, 550, 1023, 0, 255);
  108. motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  109. }
  110. else {
  111. motorSpeedA = 0;
  112. motorSpeedB = 0;
  113. }
  114. if (xAxis < 470) {
  115. int xMapped = map(xAxis, 470, 0, 0, 255);
  116. motorSpeedA = motorSpeedA - xMapped;
  117. motorSpeedB = motorSpeedB + xMapped;
  118. if (motorSpeedA < 0) {
  119. motorSpeedA = 0;
  120. }
  121. if (motorSpeedB > 255) {
  122. motorSpeedB = 255;
  123. }
  124. }
  125. if (xAxis > 550) {
  126. int xMapped = map(xAxis, 550, 1023, 0, 255);
  127. motorSpeedA = motorSpeedA + xMapped;
  128. motorSpeedB = motorSpeedB - xMapped;
  129. if (motorSpeedA > 255) {
  130. motorSpeedA = 255;
  131. }
  132. if (motorSpeedB < 0) {
  133. motorSpeedB = 0;
  134. }
  135. }
  136. if (motorSpeedA < 70) {
  137. motorSpeedA = 0;
  138. }
  139. if (motorSpeedB < 70) {
  140. motorSpeedB = 0;
  141. }
  142. analogWrite(enA, motorSpeedA);
  143. analogWrite(enB, motorSpeedB);
  144. }
  145. void loop()
  146. {
  147. sensor ();
  148. lcdWords ();
  149. motors ();
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement