Advertisement
Guest User

Untitled

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