Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. //1
  4. enum pinNum {pin1 = 1, pinInfraredProximitySensor_L, pinInfraredProximitySensor_R, pinUltraSonicSensor, pinServo, pinBackMotor_E, pinBuzzer, pinFrontMotor_L, pinFrontMotor_R, pin10=10, pinBackMotor_F, pinBackMotor_B};
  5.  
  6. //enum direction {left=-1, straight, right, backward=-1, stop, forward, slow=0};
  7. //enum direction {straight, left, right, stop=0, backward, forward, slow=0};pin10=10
  8. enum direction {dirLeft, dirMiddle, dirRight, dirBackward = 0, dirStay, dirForward};
  9. enum speed {spdFull = 255, spdHalf = 127, spdStop = 0};
  10. enum distance {disDanger = 30, disYell = 70};
  11. enum pitch {tnCheck = 250, tnReady = 320, tnYell = 700, tnScream = 850};
  12. enum distanceFlag {dfSafe, dfYell, dfDanger};
  13.  
  14. int distanceFlagLog[3]; //D=Danger Y=Yell S=Safe
  15. int checkSeq[4] = {dirMiddle, dirRight, dirMiddle, dirLeft};
  16.  
  17. const bool debug = true;
  18. const int svAngleDefault = 90;
  19. const int svAngleChange = 45;
  20. const int serialPort = 9600;
  21. int currentXdir = -1;
  22. int currentYdir = -1;
  23. int currentSpeed = -1;
  24.  
  25. Servo _servo;
  26. //int svdir = dirLeft;
  27.  
  28. void setup() {
  29. //tone(pinBuzzer, tnReady, 500);
  30. if (debug) {
  31. Serial.begin(serialPort);
  32. Serial.println(); // cursor to home
  33. Serial.println();
  34. Serial.println();
  35. Serial.println();
  36. Serial.println("*******");
  37. Serial.println("*******");
  38. }
  39. _servo.attach(pinServo); //*************
  40. //pinMode(pinInfraredProximitySensor_L, INPUT);
  41. //pinMode(pinInfraredProximitySensor_R, INPUT);
  42. firstCheck();
  43. turnSV(dirMiddle);
  44. tone(pinBuzzer, tnReady, 500);
  45. debugPrint("0********", 0);
  46. delay(5000);
  47. // attachInterrupt(digitalPinToInterrupt(pinInfraredProximitySensor_L), blink, FALLING);
  48. // attachInterrupt(digitalPinToInterrupt(pinInfraredProximitySensor_R), blink, FALLING);
  49. }
  50.  
  51. //44000102110895
  52. void loop() {
  53. for (int i = 0; i < 4; i++) {
  54. turnSV(checkSeq[i]);
  55. smartCar();
  56. debugPrint("Check: ", i);
  57. if ( busyIRCheck(3, 300) == false) {
  58. logDistance(checkSeq[i]);
  59. }
  60. //delay(100000);
  61. }
  62. }
  63.  
  64. void firstCheck() {
  65. for (int i = 0; i < 3; i++) {
  66. turnSV(i);
  67. delay(500);
  68. logDistance(i);
  69. tone(pinBuzzer, tnCheck, 150);
  70. debugPrint("FirstCheck: ", i);
  71. }
  72. }
  73.  
  74. void smartCar() {
  75. int xdir = dirMiddle;
  76. int ydir = dirStay;
  77. int spd = spdStop;
  78. switch (distanceFlagLog[1]) {
  79. case dfYell:
  80. spd = spdHalf;
  81. ydir = dirForward;
  82. xdir = pickDirection(false);
  83. tone(pinBuzzer, tnYell, 500);
  84. debugPrint("smartCar Yell ", ydir);
  85. break;
  86. case dfDanger:
  87. spd = spdHalf;
  88. ydir = dirBackward;
  89. xdir = pickDirection(true);
  90. tone(pinBuzzer, tnScream, 500);
  91. debugPrint("smartCar Danger ", ydir);
  92. break;
  93. default:
  94. spd = spdFull;
  95. ydir = dirForward;
  96. debugPrint("smartCar Safe ", ydir);
  97. }
  98. if(currentXdir != xdir || currentYdir != ydir || currentSpeed != spd){
  99. debugPrint("smartCar Adjust ", 0);
  100. runMotors (xdir, ydir, spd);
  101. }
  102. currentXdir = xdir;
  103. currentYdir = ydir;
  104. currentSpeed = spd;
  105. return;
  106. }
  107.  
  108. int pickDirection(bool mustTurn) {
  109. if (distanceFlagLog[0] > distanceFlagLog[2]) {
  110. return dirRight;
  111. } else if (distanceFlagLog[0] < distanceFlagLog[2]) {
  112. return dirLeft;
  113. }
  114. if (mustTurn) {
  115. return round((double)rand() / (double)RAND_MAX) * 2;
  116. }
  117. return 0;
  118. }
  119.  
  120. /*void StillCheck(int dir) {
  121. turnSV(dir);
  122. delay(400);
  123. logDistance(dir);
  124. tone(pinBuzzer, tnReady, 150);
  125. debugPrint("Check: ", dir);
  126. }*/
  127.  
  128. void logDistance(int direction) {
  129. int currentDistance = getDistance(pinUltraSonicSensor);
  130. debugPrint("Distance:", currentDistance);
  131. if (currentDistance <= disDanger) {
  132. distanceFlagLog[direction] = dfDanger;
  133. } else if (currentDistance <= disYell) {
  134. distanceFlagLog[direction] = dfYell;
  135. } else {
  136. distanceFlagLog[direction] = dfSafe;
  137. }
  138. }
  139.  
  140. bool busyIRCheck(int counter, int waitTime) {
  141. int irL = HIGH;
  142. int irR = HIGH;
  143. for (int j = 0; j < counter; j++) {
  144. irL = getIRPS(pinInfraredProximitySensor_L);
  145. irR = getIRPS(pinInfraredProximitySensor_R);
  146. if (irL && irR) {
  147. distanceFlagLog[dirMiddle] = dfDanger;
  148. debugPrint("busyIRCheck Danger ", j);
  149. return true;
  150. } else if (irL == true && irR == false) {
  151. distanceFlagLog[dirRight] = dfDanger;
  152. debugPrint("busyIRCheck R Danger ", j);
  153. return true;
  154. } else if (irL == false && irR == true) {
  155. distanceFlagLog[dirLeft] = dfDanger;
  156. debugPrint("busyIRCheck L Danger ", j);
  157. return true;
  158. }
  159. debugPrint("busyIRCheck Safe ", j);
  160. delay(waitTime / counter);
  161. }
  162. debugPrint("busyIRCheck wait ", waitTime);
  163. return false;
  164. }
  165.  
  166. //***************
  167. //Motors
  168. //***************
  169. void runMotors (int xdir, int ydir, unsigned int spd) { //00 straight 01 left 10 right 0-255 speed
  170.  
  171. digitalWrite(pinFrontMotor_L, xdir==0);
  172. digitalWrite(pinFrontMotor_R, xdir==2);
  173. debugPrint("Turn:", xdir);
  174. debugPrint("pinFrontMotor_L PIN:", pinFrontMotor_L);
  175. debugPrint("pinFrontMotor_R PIN:", pinFrontMotor_R);
  176. debugPrint("pinFrontMotor_L:", xdir==0);
  177. debugPrint("pinFrontMotor_R:", xdir==2);
  178.  
  179. analogWrite(pinBackMotor_E, spd);
  180. digitalWrite(pinBackMotor_F, ydir==2);
  181. digitalWrite(pinBackMotor_B, ydir==0);
  182. debugPrint("pinBackMotor_E PIN:", pinBackMotor_E);
  183. debugPrint("pinBackMotor_F PIN:", pinBackMotor_F);
  184. debugPrint("pinBackMotor_B PIN:", pinBackMotor_B);
  185. debugPrint("pinBackMotor_F:", ydir==2);
  186. debugPrint("pinBackMotor_B:", ydir==0);
  187. debugPrint("Run:", ydir);
  188. debugPrint("Speed:", spd);
  189. //delay(2000);
  190. //digitalWrite(backMotor_F, -1 * dir + abs(dir));
  191. //digitalWrite(backMotor_B, dir + abs(dir));
  192. }
  193.  
  194.  
  195. //***************
  196. //Servo
  197. //***************
  198. void turnSV(int dir) {
  199. //int angle = SV_S_Angle + (dir & 2 - 1) * SV_T_Angle;
  200. int angle = svAngleDefault + (dir - 1) * svAngleChange;
  201. //turnSV(angle);
  202. _servo.write(angle);
  203. debugPrint("Servo turn:", dir);
  204. debugPrint("Angle:", angle);
  205. }
  206.  
  207. //***************
  208. //3Pin Ultrasonic Sensor
  209. //***************
  210. int getDistance(int pinNum) {
  211. long duration = 0;
  212. long distance = 0;
  213. pinMode(pinNum, OUTPUT);
  214. digitalWrite(pinNum, LOW);
  215. delayMicroseconds(2);
  216. digitalWrite(pinNum, HIGH);
  217. delayMicroseconds(10);
  218. digitalWrite(pinNum, LOW);
  219. pinMode(pinNum, INPUT);
  220. duration = pulseIn(pinNum, HIGH);
  221. if (duration <= 0) {
  222. distance = 99999;
  223. }
  224. distance = duration * 0.034 / 2;
  225. debugPrint("Duration:", duration);
  226. debugPrint("Distance:", distance);
  227. return (int) distance;
  228. }
  229.  
  230. //***************
  231. //Infrared Proximity Sensor
  232. //***************
  233. bool getIRPS(int pinNum) {
  234. pinMode(pinNum, INPUT);
  235. if (digitalRead(pinNum) == LOW) {
  236. debugPrint("IRPS ALARM ", pinNum);
  237. return true;
  238. }
  239. debugPrint("IRPS OK ", pinNum);
  240. return false;
  241. }
  242.  
  243. //***************
  244. //Debug
  245. //***************
  246. void debugPrint(String msg, int data) {
  247. msg += data;
  248. if (debug) {
  249. Serial.println(msg);
  250. }
  251. }
  252.  
  253. //***************
  254. //ActiveBuzzer
  255. //***************
  256. /*void activeBuzz(int pinNum, unsigned duriation, unsigned int highDuration, unsigned int lowDuration) {
  257. if (debug){
  258. Serial.println("BUZZ " + pinNum + " " + duriation + " " + highDuration + " " + lowDuration);
  259. }
  260. if(abs(lowDuration) + abs(highDuration) ==0 ){
  261. return;
  262. }
  263. int cycle = round(abs(duriation) / (abs(lowDuration) + abs(highDuration)));
  264. if (debug){
  265. Serial.println("BUZZ Cycle " + cycle);
  266. }
  267. if(cycle == 0){
  268. return;
  269. }
  270. int i = 0;
  271. for(i=0;i<cycle;i++) {
  272. digitalWrite(pinNum,HIGH);
  273. delay(highDuration);
  274. digitalWrite(pinNum,LOW);
  275. delay(lowDuration);
  276. }
  277. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement