Advertisement
Guest User

M_Fire_Version_5-6_1.ino

a guest
Jul 21st, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.22 KB | None | 0 0
  1. /*
  2. MazeFireFighter
  3.  
  4. Robot traverses the maze to find and put out fires.
  5. It right wall following algorithm to keep within the
  6. maze wall. Once it detects a fire, it using fire
  7. fighting algorithm to put it out.
  8.  
  9. The circuit:
  10. * 3 Ultrasonic sensors (2 logic pins each)
  11. * 2 fire sensors (1 logic pins each)
  12. * 1 l293d IC (4 logic pins, 2 per wheel)
  13. * 1 motor for fan (2 digital pins for power)
  14.  
  15. December, 18, 2015
  16. Ryerson University
  17.  
  18. Judges:
  19. Ryerson University
  20. University of Toronto
  21. */
  22.  
  23. //Motors must be on PMW pins (Pulse Width Modulation)
  24. const byte right_motor_pin_1 = 3; //PMW pin
  25. const byte right_motor_pin_2 = 9; //PMW pin
  26. const byte left_motor_pin_1 = 5; //PMW pin
  27. const byte left_motor_pin_2 = 6; //PMW pin
  28.  
  29. //UltraSonic Sensor Pins
  30. //Left Sensor
  31. const byte trigPinLeft = 2; //Trigger pin
  32. const byte echoPinLeft = 4; //Echo Pin
  33.  
  34. //RightSensor
  35. const byte trigPinRight = A3; //Trigger pin
  36. const byte echoPinRight = A4; //Echo Pin
  37.  
  38. //Middle Sensor
  39. const byte trigPinMiddle = A0; //Trigger pin
  40. const byte echoPinMiddle = 8; //Echo Pin
  41.  
  42. //Power pins for the fan, need 2 digital pins to get desired speed on the motor
  43. const byte fanPin_1 = 10;
  44. const byte fanPin_2 = 7;
  45.  
  46. //Fire sensor logic pins
  47. const byte firePin_1 = A1; //left fire sensor
  48. const byte firePin_2 = A5; ///right fire sensor
  49.  
  50. const byte rightTurnPowerShort = 25; //Used when going around a 90 degree corner. Adjustable to match the car (%).
  51. const byte rightTurnPowerLong = 35; //Used when going around a 180 degree corner. Adjustable to match the car (%).
  52.  
  53. const int quarterTurnTime =750; //Time it takes for car to do a 90 degree turn. Adjustable to match the car (milliseconds).
  54.  
  55. const int fireBound = 500;
  56. const int sweetSpotLowerBound = 110;
  57. const int sweetSpotUpperBound = 140;
  58. const int leftTightSpotBound = 60;
  59. const int middleWallBound = 100;
  60. const int sidesWallBound = 250;
  61.  
  62. long millimeterRight, millimeterLeft, millimeterMiddle; //millimeter measurment from ultrasonic sensors
  63.  
  64. //Fire readings from fire sensor logic pins
  65. int fireValue_1 = 0; //Value of left fire sensor
  66. int fireValue_2 = 0; //Value of right fire sensor
  67.  
  68. byte turnCounter = 0; //How many short turns have been made using power of rightTurnPowerShort to find the right wall
  69. boolean fireDetected = false; //true = fire detected, false = not detected
  70.  
  71. byte fireCounter = 0; //Number of fires that have been put out
  72.  
  73. boolean debugging = false; //debugging to check for values
  74.  
  75. /*
  76. * Function: setup
  77. * ----------------
  78. * Initialize pins and start serial monitoring depending on debugging state.
  79. */
  80. void setup()
  81. {
  82. //initialize pins modes for wheel logic
  83. pinMode(right_motor_pin_1,OUTPUT); //right wheel
  84. pinMode(right_motor_pin_2,OUTPUT); //right wheel
  85. pinMode(left_motor_pin_1,OUTPUT); //left wheel
  86. pinMode(left_motor_pin_2,OUTPUT); //left wheel
  87.  
  88. //initialize pins modes for sensor logic
  89. pinMode(trigPinLeft, OUTPUT);
  90. pinMode(echoPinLeft, INPUT);
  91. pinMode(trigPinRight, OUTPUT);
  92. pinMode(echoPinRight, INPUT);
  93. pinMode(trigPinMiddle, OUTPUT);
  94. pinMode(echoPinMiddle, INPUT);
  95.  
  96. //initialize pins modes for fan power
  97. pinMode(fanPin_1,OUTPUT);
  98. pinMode(fanPin_2,OUTPUT);
  99. digitalWrite(fanPin_1,LOW); //turn off fan
  100. digitalWrite(fanPin_2,LOW); //turn off fan
  101.  
  102. delay(2000); //short delay of 2 seconds to allow user to place the car down
  103.  
  104. getDistanceReadings(); //get initial ultrasonic sensor readings
  105. getFireReadings(); //get initial fire readings
  106.  
  107. if(debugging == true)
  108. {
  109. //if debugging is true start serial monitor
  110. Serial.begin(9600); //using 9600 as baud rate
  111. Serial.println("Started");
  112. }
  113. }
  114.  
  115. /*
  116. * Function: loop
  117. * ---------------
  118. * Get sensor readings and perform car action.
  119. */
  120. void loop()
  121. {
  122. getDistanceReadings(); //get distance readings
  123. getFireReadings(); //get fire readings
  124.  
  125. carAction(); //actions based on sensor readings
  126.  
  127. if(debugging == true)
  128. {
  129. printSensorReadings(); //print sensor readings if debugging is true
  130. }
  131. }
  132.  
  133. /*
  134. * Function: fightFire
  135. * --------------------
  136. * Robot checks makes sure there is a fire and fights the fire. Once the
  137. * robot finishs its blowAtFire sweeping method, then the robot re-checks
  138. * if the fire has been put out. If the secondary check proves the fire has
  139. * been put out then fire counter is update and robot can carry on with
  140. * traversing the maze, otherwise robot must repeat blowAtFire.
  141. */
  142. void fightFire()
  143. {
  144. getDistanceReadings(); //get distance readings
  145.  
  146. //while any of the sensors detect fire
  147. while(fireValue_1<=fireBound || fireValue_2<=fireBound)
  148. {
  149. blowAtFire(); //blow at fire using sweeping method
  150.  
  151. //if fire has been put out re-check surroundings
  152. if(fireValue_1>fireBound|| fireValue_2>fireBound)
  153. {
  154. //turn left and check sensors for fire (~45 degrees)
  155. turnLeftForwardTightVariable(100);
  156. delay(400);
  157. turnOff();
  158. delay(100);
  159. int fireLeft1 = analogRead(firePin_1); //left turn, left sensor reading
  160. int fireLeft2 = analogRead(firePin_2); //left turn, right sensor reading
  161.  
  162. //turn back to middle
  163. turnRightForwardTightVariable(100);
  164. delay(400);
  165. turnOff();
  166. delay(100);
  167.  
  168. //turn right and check sensors for fire (~45 degrees)
  169. turnRightForwardTightVariable(100);
  170. delay(400);
  171. turnOff();
  172. delay(100);
  173. int fireRight1 = analogRead(firePin_1); //right turn, left sensor reading
  174. int fireRight2 = analogRead(firePin_2); //right turn, right sensor reading
  175.  
  176. //turn back to middle
  177. turnLeftForwardTightVariable(100);
  178. delay(400);
  179. turnOff();
  180. delay(100);
  181.  
  182. //if any of the 4 values show fire then set global fire values to less then fireBound (in this case to 40)
  183. if(fireRight1<=fireBound || fireLeft1<=fireBound || fireLeft2<=fireBound || fireRight2<=fireBound)
  184. {
  185. fireValue_1 = 40;
  186. fireValue_2 = 40;
  187. }
  188. }
  189. }
  190.  
  191. getDistanceReadings(); //get distance readings
  192. fireCounter++; //fire has been extinguished, so update fire counter
  193. fireDetected = false; //fire detected to false
  194. }
  195.  
  196. /*
  197. * Function: blowAtFire
  198. * ---------------------
  199. * Robot tries to stay close to the right wall as it fights
  200. * the fire. It does a slow sweep turn with the fan on to left and right
  201. * in order to extinguish the fire.
  202. */
  203. void blowAtFire()
  204. {
  205. //move forward a little if the car is between 12 cm and 13 cm to the right wall
  206. if(millimeterRight>=sweetSpotLowerBound+10 && millimeterRight<=sweetSpotUpperBound-10)
  207. {
  208. //move forward a little bit
  209. forwardVariable(100);
  210. delay(500);
  211. turnOff();
  212. }
  213. //if the car is closer then 12 cm, then turn left for 1/20th of a second and move forward a little
  214. else if(millimeterRight<sweetSpotLowerBound+10)
  215. {
  216. //turn left a little
  217. turnLeftForwardTightVariable(100);
  218. delay(50);
  219. turnOff();
  220. delay(50);
  221. //move forward a little
  222. forwardVariable(100);
  223. delay(500);
  224. turnOff();
  225. }
  226. //if the car is further then 13 cm, then turn right for 1/20th of a second and move forward a little
  227. else if(millimeterRight>sweetSpotUpperBound-10)
  228. {
  229. //turn right a little
  230. turnRightForwardTightVariable(100);
  231. delay(50);
  232. turnOff();
  233. delay(50);
  234. //move forward a little
  235. forwardVariable(100);
  236. delay(500);
  237. turnOff();
  238. }
  239.  
  240. //turn on fan
  241. digitalWrite(fanPin_1,HIGH);
  242. digitalWrite(fanPin_2,HIGH);
  243. delay(3000); //stay in middle for 3 seconds
  244.  
  245. //sweep to the left for 1 second (~45 degree turn)
  246. leftPowerBack(40);
  247. rightPower(40);
  248. delay(1000);
  249.  
  250. //sweep back to middle
  251. leftPower(40);
  252. rightPowerBack(40);
  253. delay(1000);
  254.  
  255. //stay in middle for 1 second
  256. turnOff();
  257. delay(1000);
  258.  
  259. //sweep to the right for 1 second (~45 degree turn)
  260. leftPower(50);
  261. rightPowerBack(50);
  262. delay(1000);
  263.  
  264. //sweep back to middle
  265. leftPowerBack(50);
  266. rightPower(50);
  267. delay(1000);
  268.  
  269. //stay in middle for 1 second
  270. turnOff();
  271. delay(1000);
  272.  
  273. //turn off fan
  274. digitalWrite(fanPin_1,LOW);
  275. digitalWrite(fanPin_2,LOW);
  276.  
  277. delay(1000); //wait 1 second for fan to stop
  278.  
  279. getDistanceReadings(); //get distance readings
  280. getFireReadings(); //get fire sensor readings
  281. }
  282.  
  283. /*
  284. * Function: aroundTheCornerFireCheck
  285. * -----------------------------------
  286. * Checking for fire when car is going around a corner. The car
  287. * sweeps left and and right to check for fire. If fire is found
  288. * then car goes into fire fighting mode.
  289. */
  290. void aroundTheCornerFireCheck()
  291. {
  292. //sweep to the right
  293. turnRightForwardTightVariable(100);
  294. delay(250);
  295. turnOff();
  296. delay(100);
  297. int right1 = analogRead(firePin_1); //get right sweep left sensor data
  298. int right2 = analogRead(firePin_2); //get right sweep right sensor data
  299.  
  300. //sweep back to the middle
  301. turnLeftForwardTightVariable(100);
  302. delay(250);
  303. turnOff();
  304. delay(100);
  305. int middle1 = analogRead(firePin_1); //get middle sweep left sensor data
  306. int middle2 = analogRead(firePin_2); //get middle sweep right sensor data
  307.  
  308. //sweep to the left
  309. turnLeftForwardTightVariable(100);
  310. delay(250);
  311. turnOff();
  312. delay(100);
  313. int left1 = analogRead(firePin_1); //get left sweep left sensor data
  314. int left2 = analogRead(firePin_2); //get left sweep right sensor data
  315.  
  316. //sweep back to the middle
  317. turnRightForwardTightVariable(100);
  318. delay(250);
  319. turnOff();
  320. delay(100);
  321.  
  322. //check if any of the 6 values indicate a fire
  323. if(right1<=fireBound || right2<=fireBound || middle1<=fireBound || middle2<=fireBound || left1<=fireBound ||left2<=fireBound)
  324. {
  325. fireDetected = true; //set fire detected to true
  326. turnOff(); //turn off car
  327. fightFire(); //fight fire
  328. }
  329. }
  330.  
  331. /*
  332. * Function: carAction
  333. * ------------------------
  334. * Use wall information to determine next course of action for the car.
  335. */
  336. void carAction()
  337. {
  338. byte wallNumber = checkForWalls();
  339. /*1 left
  340. *2 right
  341. *3 left+right
  342. *4 middle
  343. *5 left+middle
  344. *6 right+middle
  345. *7 left+right+middle
  346. */
  347. switch (wallNumber)
  348. {
  349. case 1:turnOff(); //this case is delt with in noWallToTheRight method
  350. break;
  351. case 2:wallOnRight(); //keeps to the right
  352. break;
  353. case 3:wallOnRight(); //keeps to the right
  354. break;
  355. case 4:turnOff(); //this case is delt with in noWallToTheRight method
  356. break;
  357. case 5:turnOff(); //this case is delt with in noWallToTheRight method
  358. break;
  359. case 6:wallOnMiddleAndRight(); //make a 90 degree left turn
  360. break;
  361. case 7:allSides(); //makes a 180 degree turn if there is enough space
  362. break;
  363. default:turnOff(); //this case is delt with in noWallToTheRight method
  364. break;
  365. }
  366. }
  367.  
  368. /*
  369. * Function: allSides
  370. * -------------------
  371. * If there is walls on all sides then the robot first checks if
  372. * there is enough space to make a turn around. If there is not
  373. * enough space then the robot backs up to a point were it can.
  374. * If there is enough space, then the robot turns around and moves
  375. * a little closer to the right wall to make sure it can require it.
  376. */
  377. void allSides()
  378. {
  379. getDistanceReadings(); //get distance readings
  380.  
  381. //if there is not enough room then back up till there is enough room on the left side
  382. if(millimeterLeft<=leftTightSpotBound)
  383. {
  384. while(millimeterLeft<=leftTightSpotBound)
  385. {
  386. backwardVariable(100);
  387. getDistanceReadings();
  388. }
  389. }
  390.  
  391. turnAround(); //180 degree turn
  392.  
  393. //get closer to the right wall
  394. turnRightForwardTightVariable(100);
  395. delay(500);
  396. forwardVariable(100);
  397. delay(500);
  398. turnLeftForwardTightVariable(100);
  399. delay(600);
  400. turnOff();
  401.  
  402. turnCounter = 0; //set turn counter to 0
  403. }
  404.  
  405. /*
  406. * Function: wallOnRight
  407. * ----------------------
  408. * If there is wall on the right then follow the keep to the right algorithm
  409. */
  410. void wallOnRight()
  411. {
  412. keepToRight(); //keep to right
  413. turnCounter = 0;//set turn counter to 0
  414. }
  415.  
  416. /*
  417. * Function: wallOnMiddleAndRight
  418. * -------------------------------
  419. * Since the robot cannot got forward anymore, it must do a 90 degree turn
  420. * to the left inorder to make the current middle wall its right wall.
  421. */
  422. void wallOnMiddleAndRight()
  423. {
  424. turnLeft(); //turn left
  425. turnCounter = 0;//set turn counter to 0
  426. }
  427.  
  428. /*
  429. * Function: keepToRight
  430. * ----------------------
  431. * Keep right wall between 12 and 14 centimeters. If the car is in the
  432. * sweet spot then keep moving forward, otherwise turn left or right
  433. * to get back in the sweet spot. This will keep the car aligned straight
  434. * with the right wall.
  435. * Check sensors for fire, and if turn counter is greater then 2 then do
  436. * an active sweep check for fire by turning the car left and right.
  437. */
  438. void keepToRight()
  439. {
  440. //if car is in the sweet spot then move forward
  441. if(millimeterRight>=sweetSpotLowerBound && millimeterRight<=sweetSpotUpperBound)
  442. {
  443. forwardVariable(100);
  444. }
  445. //if the car is too close the wall then turn left
  446. else if(millimeterRight<sweetSpotLowerBound)
  447. {
  448. turnLeftForwardTightVariable(100);
  449. }
  450. //if the car is too far awau from the wall then turn right
  451. else if(millimeterRight>sweetSpotUpperBound)
  452. {
  453. turnRightForwardTightVariable(100);
  454. }
  455.  
  456. //check for fire values
  457. if(fireValue_1<=fireBound || fireValue_2<=fireBound)
  458. {
  459. fireDetected = true;
  460. turnOff();
  461. fightFire();
  462. }
  463.  
  464. //if turn is >=2 then do an active fire check by sweeping the car left and right
  465. if(turnCounter>=2)
  466. {
  467. aroundTheCornerFireCheck(); //sweep check for fire
  468. }
  469.  
  470. turnCounter = 0;//set turn counter to 0
  471. }
  472.  
  473. /*
  474. * Function: checkForWalls
  475. * ------------------------
  476. * Use sensor readings to check if there are walls to left, right or middle.
  477. * return: number which corresponds to were the walls are around the robot
  478. */
  479. byte checkForWalls()
  480. {
  481. /*
  482. * 1 left
  483. * 2 right
  484. * 3 left+right
  485. * 4 middle
  486. * 5 left+middle
  487. * 6 right+middle
  488. * 7 left+right+middle
  489. */
  490.  
  491. byte wallNumber = 0;
  492.  
  493. if(millimeterLeft<=sidesWallBound)
  494. wallNumber += 1; //wall to the left
  495. if(millimeterRight<=sidesWallBound)
  496. wallNumber += 2; //wall to the right
  497. else
  498. noWallToTheRight(); //if there is no wall to right then run right wall finding algorithm
  499.  
  500.  
  501. if(millimeterMiddle<=middleWallBound)
  502. wallNumber += 4; //wall in in the middle
  503.  
  504. return wallNumber;
  505. }
  506.  
  507. /*
  508. * Function: noWallToTheRight
  509. * ---------------------------
  510. * If there is no wall to the right then the robot can start to
  511. * slowly turn towards the right to re-find the right wall.
  512. * The first 2 turns at a long arc. If the robot has not found
  513. * the right wall using these two long turns then that must mean
  514. * the wall is at a 90 degree angle or more. The robot then does
  515. * tighter turns to find the right wall.
  516. *
  517. * If fire sensors detect fire then the robot goes into fire
  518. * fighting mode.
  519. */
  520. void noWallToTheRight()
  521. {
  522. turnCounter++; //update turnCounter
  523.  
  524. //if counter >=3 then do tighter right turns
  525. if(turnCounter>=3)
  526. {
  527. leftPower(100);
  528. rightPower(rightTurnPowerShort);
  529. delay(1000);
  530. turnOffWait();
  531. }
  532. //if counter <3 then do longer right turns
  533. else
  534. {
  535. leftPower(100);
  536. rightPower(rightTurnPowerLong);
  537. delay(1000);
  538. turnOffWait();
  539. }
  540.  
  541. //if eaither of the sensors detect fire then go into fire fighting mode
  542. if(fireValue_1<=fireBound || fireValue_2<=fireBound)
  543. {
  544. fireDetected = true; //fire detected is true
  545. turnOff(); //turn off car
  546. fightFire(); //start fire fighting algorithm
  547. }
  548. }
  549.  
  550. /*
  551. * Function: ping
  552. * ---------------
  553. * Sends an ultrasonic pulse, then waits to recieve an echo back.
  554. * The time is takes to recieve an echo is used to calculate
  555. * distance to object depending on the orientation of the
  556. * sensors.
  557. * t: trigger pin of an ultrasonic sensor
  558. * e: echo pin of an ultrasonic sensor
  559. * return: distance to object in millimeters
  560. */
  561. long ping(int t, int e)
  562. {
  563. digitalWrite(t, LOW); //low flush to clear sensor
  564. delayMicroseconds(2); //wait 2 microseconds for the low flush to kick in
  565. digitalWrite(t, HIGH); //turn trigger pin to high
  566. delayMicroseconds(10); //wait 10 seconds (this will give a reading of ATLEAST 14.5 millimeters (10*2.9/2 = 14.5 mm)
  567. digitalWrite(t, LOW); //turn trigger to low to stop pulse and wait for echo
  568. long duration = pulseIn(e, HIGH); //wait for echo pin to recieve the pulse
  569. return microsecondsToMillimeters(duration); //use microsecond time to calculate distance in millimeters
  570. }
  571.  
  572. /*
  573. * Function: microsecondsToMillimeters
  574. * ------------------------------------
  575. * Converts microseconds to distance traveled by sound.
  576. * microsecond: time it took to get echo back from sensor
  577. * return: distance travelled by sound in millimeters
  578. */
  579. long microsecondsToMillimeters(long microseconds)
  580. {
  581. //Time it takes sounds to travel 1 mm = (1000 microseconds) / (345 m/seconds) = 2.9 microseconds
  582. //Distance = (microseconds) / (Time it takes sounds to travel 1 mm) / 2
  583. return microseconds / 2.9 / 2.0;
  584. }
  585.  
  586. /*
  587. * Function: turnOff
  588. * ------------------
  589. * Makes robot turn off.
  590. */
  591. void turnOff()
  592. {
  593. digitalWrite(right_motor_pin_1,LOW);
  594. digitalWrite(right_motor_pin_2,LOW);
  595. digitalWrite(left_motor_pin_1,LOW);
  596. digitalWrite(left_motor_pin_2,LOW);
  597. }
  598.  
  599. /*
  600. * Function: turnOffWait
  601. * ---------------------_
  602. * Makes robot turn off for half a second.
  603. * The reason this method exists is to reduce unnecessary code.
  604. */
  605. void turnOffWait()
  606. {
  607. turnOff();
  608. delay(500);
  609. }
  610.  
  611. /*
  612. * Function: turnAround
  613. * ---------------------
  614. * Makes robot turn off for half a second.
  615. * Then it turns left for 2 times quarter turn time.
  616. * Finally robot turns off for half a second.
  617. */
  618. void turnAround()
  619. {
  620. turnOffWait();
  621. turnLeftForwardTight();
  622. delay(quarterTurnTime*2);
  623. turnOffWait();
  624. }
  625.  
  626. /*
  627. * Function: turLeft
  628. * ------------------
  629. * Makes robot turn off for half a second.
  630. * Then it turns left for quarter turn time.
  631. * Finally robot turns off for half a second.
  632. */
  633. void turnLeft()
  634. {
  635. turnOffWait();
  636. turnLeftForwardTight();
  637. delay(quarterTurnTime);
  638. turnOffWait();
  639. }
  640.  
  641. /*
  642. * Function: turRight
  643. * ------------------
  644. * Makes robot turn off for half a second.
  645. * Then it turns right for quarter turn time.
  646. * Finally robot turns off for half a second.
  647. */
  648. void turnRight()
  649. {
  650. turnOffWait();
  651. turnRightForwardTight();
  652. delay(quarterTurnTime);
  653. turnOffWait();
  654. }
  655.  
  656. /*
  657. * Function: turnLeftForwardTight
  658. * --------------------------------
  659. * Makes robot turn left on its center of mass at full speed.
  660. */
  661. void turnLeftForwardTight()
  662. {
  663. digitalWrite(right_motor_pin_1,HIGH);
  664. digitalWrite(right_motor_pin_2,LOW);
  665. digitalWrite(left_motor_pin_1,LOW);
  666. digitalWrite(left_motor_pin_2,HIGH);
  667. }
  668.  
  669. /*
  670. * Function: turnRightForwardTight
  671. * --------------------------------
  672. * Makes robot turn right on its center of mass at full speed.
  673. */
  674. void turnRightForwardTight()
  675. {
  676. digitalWrite(right_motor_pin_1,LOW);
  677. digitalWrite(right_motor_pin_2,HIGH);
  678. digitalWrite(left_motor_pin_1,HIGH);
  679. digitalWrite(left_motor_pin_2,LOW);
  680. }
  681.  
  682. /*
  683. * Function: turnRightForwardTightVariable
  684. * ----------------------------------------
  685. * Makes robot turn right on its center of mass with desired speed.
  686. * amount: % value of speed of robot
  687. */
  688. void turnRightForwardTightVariable(float amount)
  689. {
  690. int calibratedSpeed = (amount/100.0)*255.0; //calibrate speed out of 255
  691. //analog write the calibrated speed
  692. analogWrite(right_motor_pin_1,0);
  693. analogWrite(right_motor_pin_2,calibratedSpeed);
  694. analogWrite(left_motor_pin_1,calibratedSpeed);
  695. analogWrite(left_motor_pin_2,0);
  696. }
  697.  
  698. /*
  699. * Function: turnLeftForwardTightVariable
  700. * ---------------------------------------
  701. * Makes robot turn left on its center of mass with desired speed.
  702. * amount: % value of speed of robot
  703. */
  704. void turnLeftForwardTightVariable(float amount)
  705. {
  706. int calibratedSpeed = (amount/100.0)*255.0; //calibrate speed out of 255
  707. //analog write the calibrated speed
  708. analogWrite(right_motor_pin_1,calibratedSpeed);
  709. analogWrite(right_motor_pin_2,0);
  710. analogWrite(left_motor_pin_1,0);
  711. analogWrite(left_motor_pin_2,calibratedSpeed);
  712. }
  713.  
  714. /*
  715. * Function: leftPower
  716. * --------------------
  717. * Makes left wheel move forward with desired speed.
  718. * amount: % value of speed of robot
  719. */
  720. void leftPower(float amount)
  721. {
  722. int calibratedSpeed = (amount/100.0)*255.0; //calibrate speed out of 255
  723. //analog write the calibrated speed
  724. analogWrite(left_motor_pin_1,calibratedSpeed);
  725. analogWrite(left_motor_pin_2,0);
  726. }
  727.  
  728. /*
  729. * Function: rightPower
  730. * ---------------------
  731. * Makes right wheel move forward with desired speed.
  732. * amount: % value of speed of robot
  733. */
  734. void rightPower(float amount)
  735. {
  736. int calibratedSpeed = (amount/100.0)*255.0; //calibrate speed out of 255
  737. //analog write the calibrated speed
  738. analogWrite(right_motor_pin_1,calibratedSpeed);
  739. analogWrite(right_motor_pin_2,0);
  740. }
  741.  
  742. /*
  743. * Function: leftPowerBack
  744. * ------------------------
  745. * Makes left wheel move backwards with desired speed.
  746. * amount: % value of speed of robot
  747. */
  748. void leftPowerBack(float amount)
  749. {
  750. int calibratedSpeed = (amount/100.0)*255.0; //calibrate speed out of 255
  751. //analog write the calibrated speed
  752. analogWrite(left_motor_pin_1,0);
  753. analogWrite(left_motor_pin_2,calibratedSpeed);
  754. }
  755.  
  756. /*
  757. * Function: rightPowerBack
  758. * -------------------------
  759. * Makes right wheel move backwards with desired speed.
  760. * amount: % value of speed of robot
  761. */
  762. void rightPowerBack(float amount)
  763. {
  764. int calibratedSpeed = (amount/100.0)*255.0; //calibrate speed out of 255
  765. //analog write the calibrated speed
  766. analogWrite(right_motor_pin_1,0);
  767. analogWrite(right_motor_pin_2,calibratedSpeed);
  768. }
  769.  
  770. /*
  771. * Function: forwardVariable
  772. * --------------------------
  773. * Makes robot move forward with desired speed.
  774. * amount: % value of speed of robot
  775. */
  776. void forwardVariable(float amount)
  777. {
  778. int calibratedSpeed = (amount/100.0)*255.0; //calibrate speed out of 255
  779. //analog write the calibrated speed
  780. analogWrite(right_motor_pin_1,calibratedSpeed);
  781. analogWrite(right_motor_pin_2,0);
  782. analogWrite(left_motor_pin_1,calibratedSpeed);
  783. analogWrite(left_motor_pin_2,0);
  784. }
  785.  
  786. /*
  787. * Function: backwardVariable
  788. * ---------------------------
  789. * Makes robot move backwards with desired speed.
  790. * amount: % value of speed of robot
  791. */
  792. void backwardVariable(float amount)
  793. {
  794. int calibratedSpeed = (amount/100.0)*255.0; //calibrate speed out of 255
  795. //analog write the calibrated speed
  796. analogWrite(right_motor_pin_1,0);
  797. analogWrite(right_motor_pin_2,calibratedSpeed);
  798. analogWrite(left_motor_pin_1,0);
  799. analogWrite(left_motor_pin_2,calibratedSpeed);
  800. }
  801.  
  802. /*
  803. * Function: getDistanceReadings
  804. * ------------------------------
  805. * Gets distance readings on each ultrasonic sensors.
  806. */
  807. void getDistanceReadings()
  808. {
  809. millimeterMiddle = ping(trigPinMiddle,echoPinMiddle); //get distance readings on middle sensor.
  810. millimeterRight = ping(trigPinRight,echoPinRight); //get distance readings on right sensor.
  811. millimeterLeft = ping(trigPinLeft,echoPinLeft); //get distance readings on left sensor.
  812. }
  813.  
  814. /*
  815. * Function: getFireReadings
  816. * --------------------------
  817. * Gets analog readings from left and right fire sensor logic pins.
  818. */
  819. void getFireReadings()
  820. {
  821. fireValue_1 = analogRead(firePin_1); //left fire sensor
  822. fireValue_2 = analogRead(firePin_2); //right fire sensor
  823. }
  824.  
  825. /*
  826. * Function: printSensorReadings
  827. * ------------------------------
  828. * Prints information from various sensors.
  829. * Prints information about left, right and middle ultrasonic sensors.
  830. * Prints information about fire values of left and right fire sensors.
  831. */
  832. void printSensorReadings()
  833. {
  834. Serial.print("Distances: ");
  835. Serial.print(millimeterLeft);
  836. Serial.print(" ");
  837. Serial.print(millimeterMiddle);
  838. Serial.print(" ");
  839. Serial.print(millimeterRight);
  840. Serial.print(" - Fire: ");
  841. Serial.print(fireValue_1);
  842. Serial.print(" ");
  843. Serial.print(fireValue_2);
  844. Serial.print("\n");
  845. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement