Advertisement
Guest User

Untitled

a guest
Jul 21st, 2020
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. #include <SoftwareSerial.h> // Allows communication with Bluetooth
  2. #include <HCSR04.h>
  3. #include <L298N.h>
  4. #include <Servo.h>
  5. #include <Adafruit_NeoPixel.h>
  6. #ifdef __AVR__
  7. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  8. #endif
  9. // Which pin on the Arduino is connected to the NeoPixels?
  10. // On a Trinket or Gemma we suggest changing this to 1:
  11. #define LED_PIN 5
  12.  
  13. // How many NeoPixels are attached to the Arduino?
  14. #define LED_COUNT 42
  15. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  16.  
  17. SoftwareSerial mySerial(0, 1); // RX, TX
  18. int data;
  19.  
  20. const unsigned int IN1 = 7;
  21. const unsigned int IN2 = 8;
  22. const unsigned int EN = 6;
  23. const unsigned int IN3 = 9;
  24. const unsigned int IN4 = 4;
  25. const unsigned int ENB = 3;
  26. const unsigned int trigPin = 13;
  27. const unsigned int echoPin = 12;
  28.  
  29. int mode = 0; //mode 0 is bluetooth mode, mode 1 is obstacle-avoiding mode
  30. boolean hasFlashed = false;
  31.  
  32. UltraSonicDistanceSensor distanceSensor(13, 12); // Initialize sensor that uses digital pins 13 and 12.
  33.  
  34. L298N motor(EN, IN1, IN2);
  35. L298N motor2(ENB, IN3, IN4);
  36.  
  37. Servo servo1;d
  38.  
  39. void setup() {
  40. // Open serial communications and wait for port to open:
  41. mySerial.begin(9600);
  42. Serial.begin(9600);
  43. while (!Serial) {
  44. ; // wait for serial port to connect. Needed for native USB port only
  45. }
  46. Serial.println("Initiated");
  47. motor.setSpeed(80); //set the speed of the motors, between 0-255
  48. motor2.setSpeed (81);
  49. pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  50. pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
  51. servo1.attach(11);
  52. servo1.write(115);
  53. pinMode(2, OUTPUT);
  54.  
  55. strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  56. strip.show(); // Turn OFF all pixels ASAP
  57. //strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  58. }
  59. void loop() {
  60. Serial.println ("Work already");
  61.  
  62. while (mode == 0) //changed if to while loop (RA)
  63. {
  64. if (!hasFlashed)
  65. {
  66. flashLED(247, 201, 69);
  67. hasFlashed = true;
  68. }
  69. if (mySerial.available()) {
  70. data = mySerial.read(); // define data as the num recieved from BT
  71. Serial.println(data);
  72.  
  73.  
  74. }
  75. if (data == 1) {
  76. forward_car();
  77. }
  78. else if (data == 2) {
  79. back_car();
  80. }
  81. else if (data == 3) {
  82. left_car();
  83. }
  84. else if (data == 4) {
  85. right_car();
  86. }
  87. else if (data == 5) {
  88. stop_car();
  89. }
  90.  
  91. else if (data == 6) {
  92. mode = 1;
  93. hasFlashed = false;
  94. break;
  95. }
  96. }
  97.  
  98. while (mode == 1) {
  99.  
  100. if (!hasFlashed)
  101. {
  102. flashLED(117, 249, 76);
  103. hasFlashed = true;
  104. }
  105.  
  106. // Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.
  107. int distance = distanceSensor.measureDistanceCm();
  108. motor.forward();
  109. motor2.forward();
  110.  
  111. Serial.println (distance);
  112.  
  113. if (distance <= 80 && distance > 15) {
  114. digitalWrite(2, LOW); // turn the LED on (HIGH is the voltage level)
  115.  
  116. int red = map(distance, 0, 100, 255, 0);
  117. for (int n = 0; n < 42; n++)
  118. {
  119. strip.setPixelColor(n, red, 0, 0);
  120. }
  121. strip.show();
  122. motor.forward();
  123. motor2.forward();
  124.  
  125. }
  126. else if (distance > 80 && distance < 400)
  127. { digitalWrite(2, LOW);
  128. // for (int n = 0; n < 42; n++)
  129. // {
  130. // strip.setPixelColor(n, 0, 0, 0);
  131. // }
  132. // strip.show();
  133. // Serial.println("No Lights are on! Go ahead!");
  134.  
  135. Serial.println ("No obstacle detected. going forward");
  136. motor.forward(); //if there's no obstacle ahead, Go Forward!
  137. motor2.forward();
  138. delay (100);
  139.  
  140. }
  141.  
  142. else if (distance <= 15 && distance > 0) {
  143. digitalWrite(2, HIGH);
  144. Serial.println("Red! Red! Turn Now");
  145. motor.stop();
  146. motor2.stop();
  147. delay(500);
  148. // flashLED(255, 0, 0);
  149. motor.backward();
  150. motor2.backward(); //actually backward
  151. delay(1000);
  152. motor.stop();
  153. motor2.stop();
  154. delay(500);
  155.  
  156. int leftdistance = lookLeft();
  157. int rightdistance = lookRight();
  158.  
  159.  
  160. if (leftdistance > rightdistance) {
  161. motor.forward();
  162. motor2.backward();
  163. delay(500);
  164. motor.stop();
  165. motor2.stop();
  166. delay(200);
  167. motor.forward();
  168. motor2.forward();
  169. }
  170. else {
  171. motor.backward(); //this means turn
  172. motor2.forward();
  173. delay(500);
  174. motor.stop();
  175. motor2.stop();
  176. delay(200);
  177. motor.forward();
  178. motor2.forward();
  179.  
  180. }
  181.  
  182. }
  183.  
  184.  
  185. if (mySerial.available()) {
  186. data = mySerial.read(); // define data as the num recieved from BT
  187. Serial.println(data);
  188.  
  189. }
  190.  
  191. if (data != 6) {
  192. mode = 0;
  193. hasFlashed = false;
  194. break;
  195. }
  196.  
  197. }
  198. }
  199.  
  200.  
  201. void forward_car()
  202. { motor.forward();
  203. motor2.forward();
  204.  
  205. }
  206.  
  207. void back_car()
  208. { motor.backward();
  209. motor2.backward();
  210. }
  211.  
  212. void left_car()
  213. { motor.forward();
  214. motor2.backward();
  215. }
  216.  
  217. void right_car()
  218. { motor.backward();
  219. motor2.forward();
  220. }
  221.  
  222. void stop_car()
  223. { motor.stop();
  224. motor2.stop();
  225. }
  226.  
  227. void flashLED(int r, int g, int b) {
  228.  
  229. for (int d = 0; d < 3; d++) {
  230.  
  231. for (int n = 0; n < 42; n++)
  232. {
  233. strip.setPixelColor(n, r, g, b);
  234. }
  235. strip.show();
  236. delay(100);
  237.  
  238. for (int n = 0; n < 42; n++)
  239. {
  240. strip.setPixelColor(n, 0, 0, 0);
  241. }
  242. strip.show();
  243. delay(100);
  244.  
  245. }
  246. }
  247.  
  248. int lookRight()
  249. {
  250. servo1.write(50);
  251. delay(500);
  252. int distance = distanceSensor.measureDistanceCm();
  253. delay(100);
  254. servo1.write(115);
  255. return distance;
  256. }
  257.  
  258. int lookLeft()
  259. {
  260. servo1.write(170);
  261. delay(500);
  262. int distance = distanceSensor.measureDistanceCm();
  263. delay(100);
  264. servo1.write(115);
  265. return distance;
  266. delay(100);
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement