Advertisement
Guest User

JrDesignArduinoCode

a guest
Nov 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.34 KB | None | 0 0
  1. #include <Stepper.h>
  2. #include <Key.h>
  3. #include <Keypad.h>
  4. #include <Servo.h>
  5.  
  6. //*******************************************************************************************
  7. #define dirPin 9
  8. #define stepPin 10
  9. #define MS2 8
  10. #define stepsPerRevolution 3500
  11. Stepper myStepper(stepsPerRevolution, dirPin, stepPin);
  12.  
  13. //*******************************************************************************************
  14. #define DIRpin 5
  15. #define STEPpin 6
  16. #define MS3 4
  17. #define stepsPerRevolution2 3900
  18. Stepper myStepper2(stepsPerRevolution2, DIRpin, STEPpin);
  19.  
  20. //*******************************************************************************************
  21. Servo myservo;
  22.  
  23. //*******************************************************************************************
  24.  
  25. #define USONIC_DIV 58.0
  26. #define MEASURE_SAMPLE_DELAY 5
  27. #define MEASURE_SAMPLES 5
  28. #define MEASURE_DELAY 250
  29.  
  30. #define TRIGGER_PIN 33
  31. #define ECHO_PIN 32
  32.  
  33. #define TRIGGER_PIN1 31
  34. #define ECHO_PIN1 30
  35.  
  36. #define TRIGGER_PIN2 29
  37. #define ECHO_PIN2 28
  38.  
  39. #define TRIGGER_PIN3 26
  40. #define ECHO_PIN3 27
  41.  
  42. #define TRIGGER_PIN4 25
  43. #define ECHO_PIN4 24
  44.  
  45. #define TRIGGER_PIN5 23
  46. #define ECHO_PIN5 22
  47.  
  48. //*******************************************************************************************
  49. const byte ROWS = 4;
  50. const byte COLS = 4;
  51.  
  52. char hexaKeys[ROWS][COLS] = {
  53. {'1', '2', '3', 'A'},
  54. {'4', '5', '6', 'B'},
  55. {'7', '8', '9', 'C'},
  56. {'*', '0', '#', 'D'}
  57. };
  58.  
  59. byte rowPins[ROWS] = {35, 37, 39, 41};
  60. byte colPins[COLS] = {34, 36, 38, 40};
  61.  
  62. Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  63.  
  64.  
  65. //******************************************************************************************
  66.  
  67. void setup() {
  68.  
  69. Serial.begin(9600);
  70. // Initializing Trigger Output and Echo Input
  71. pinMode(TRIGGER_PIN, OUTPUT);
  72. pinMode(ECHO_PIN, INPUT);
  73.  
  74. pinMode(TRIGGER_PIN1, OUTPUT);
  75. pinMode(ECHO_PIN1, INPUT);
  76.  
  77. pinMode(TRIGGER_PIN2, OUTPUT);
  78. pinMode(ECHO_PIN2, INPUT);
  79.  
  80. pinMode(TRIGGER_PIN3, OUTPUT);
  81. pinMode(ECHO_PIN3, INPUT);
  82.  
  83. pinMode(TRIGGER_PIN4, OUTPUT);
  84. pinMode(ECHO_PIN4, INPUT);
  85.  
  86. pinMode(TRIGGER_PIN5, OUTPUT);
  87. pinMode(ECHO_PIN5, INPUT);
  88.  
  89.  
  90.  
  91. // Reset the trigger pin and wait a half a second
  92. digitalWrite(TRIGGER_PIN, LOW);
  93. digitalWrite(TRIGGER_PIN1, LOW);
  94. digitalWrite(TRIGGER_PIN2, LOW);
  95. digitalWrite(TRIGGER_PIN3, LOW);
  96. digitalWrite(TRIGGER_PIN4, LOW);
  97. digitalWrite(TRIGGER_PIN5, LOW);
  98.  
  99. delayMicroseconds(100);
  100. double peg_reading[6];
  101.  
  102. peg_reading[0] = measure();
  103. delay(500);
  104. peg_reading[1] = measure1();
  105. delay(500);
  106. peg_reading[2] = measure2();
  107. delay(500);
  108. peg_reading[3] = measure3();
  109. delay(500);
  110. peg_reading[4] = measure4();
  111. delay(500);
  112. peg_reading[5] = measure5();
  113.  
  114.  
  115. for(int ind = 0; ind< 6; ind++){
  116.  
  117. Serial.println(peg_reading[ind]);
  118. }
  119.  
  120.  
  121.  
  122. const int INPUT_SIZE = 6;
  123. String pegEnd_str = "000000";
  124.  
  125. Serial.print("Enter the order of the peg: ");
  126.  
  127. int count = 0;
  128. while(true){
  129.  
  130. char customKey = customKeypad.getKey();
  131.  
  132. if (customKey){
  133.  
  134. Serial.print(customKey);
  135. pegEnd_str[count] = customKey;
  136. count++;
  137.  
  138. }
  139.  
  140. if(count >= INPUT_SIZE){
  141.  
  142. break;
  143. }
  144.  
  145. }
  146. Serial.println();
  147.  
  148. const int SIZE = 6;
  149.  
  150.  
  151. String Peg_order = "000000";
  152.  
  153. Serial.println();
  154.  
  155. BubbleSortPegs(peg_reading, SIZE);
  156.  
  157. ConvertToChar(peg_reading, SIZE, Peg_order);
  158.  
  159. Serial.print("This is the current order: ");
  160. Serial.print(Peg_order);
  161. Serial.println();
  162. Serial.println();
  163.  
  164. //THIS IS THE PIN TO ATTACH THE SERVO
  165. myservo.attach(42);
  166. myservo.write(120);
  167.  
  168. //DECLARE PINS AS OUTPUT FOR THE FIRST STEPPER:
  169. pinMode(stepPin, OUTPUT);
  170. pinMode(dirPin, OUTPUT);
  171. pinMode(MS2, OUTPUT);
  172. myStepper.setSpeed(120);
  173.  
  174. //DECLARE PINS AS OUTPUT FOR THE SECOND STEPPER:
  175. pinMode(STEPpin, OUTPUT);
  176. pinMode(DIRpin, OUTPUT);
  177. myStepper2.setSpeed(120);
  178.  
  179. SpaceBySpace(Peg_order, pegEnd_str);
  180.  
  181. }
  182.  
  183. void loop() {
  184.  
  185. }
  186.  
  187. void forward_stepper1call(){
  188.  
  189. // SET THE SPININING DIRECTION CLOCKWISE.
  190. digitalWrite(MS2, HIGH);
  191. myStepper.step(-stepsPerRevolution);
  192. delay(500);
  193.  
  194. }
  195. void backward_stepper1call(){
  196.  
  197. // Set the spinning direction counterclockwise:
  198. digitalWrite(MS2, HIGH);
  199.  
  200. myStepper.step(stepsPerRevolution);
  201. delay(500);
  202.  
  203. }
  204.  
  205. void open_callMyServo(){
  206.  
  207. int pos = 120;
  208. for (pos = 120; pos <= 146; pos += 1) {
  209.  
  210. myservo.write(pos);
  211. delay(50);
  212.  
  213. }
  214.  
  215. }
  216.  
  217. void close_callMyServo(){
  218.  
  219.  
  220. int pos = 146;
  221. for (pos = 146; pos >= 120; pos -= 1) {
  222.  
  223. myservo.write(pos);
  224. delay(50);
  225. }
  226.  
  227.  
  228. }
  229.  
  230.  
  231. void MoveSide2SideStepper2(int &oldPos, int newPos, int destination, bool &clawStatus){
  232.  
  233. //NOTE: Big assumption here is that, whenever the status of the claw is 0(closed), i assume
  234. // my claw currently has a peg.
  235.  
  236. digitalWrite(MS3, HIGH);
  237. myStepper2.step((oldPos - newPos) * stepsPerRevolution2);
  238. delay(250);
  239.  
  240. //open = 0.
  241. //close = 1.
  242.  
  243. if(clawStatus == 1){
  244. forward_stepper1call();
  245. delay(100);
  246. myservo.attach(42);
  247. close_callMyServo();
  248. myservo.detach();
  249. delay(100);
  250.  
  251. backward_stepper1call();
  252. clawStatus = 0;
  253.  
  254. }else{
  255. forward_stepper1call();
  256. delay(100);
  257. myservo.attach(42);
  258. open_callMyServo();
  259. myservo.detach();
  260. delay(100);
  261. backward_stepper1call();
  262. clawStatus = 1;
  263. }
  264.  
  265. myStepper2.step((newPos - destination) * stepsPerRevolution2);
  266. delay(250);
  267.  
  268. if(clawStatus == 1){
  269. forward_stepper1call();
  270. delay(100);
  271. myservo.attach(42);
  272. close_callMyServo();
  273. myservo.detach();
  274. delay(100);
  275. backward_stepper1call();
  276. clawStatus = 0;
  277.  
  278. }else{
  279. forward_stepper1call();
  280. delay(100);
  281. myservo.attach(42);
  282. open_callMyServo();
  283. myservo.detach();
  284. delay(100);
  285. backward_stepper1call();
  286. clawStatus = 1;
  287. }
  288.  
  289. oldPos = destination;
  290. }
  291.  
  292. //*****************************************************************
  293.  
  294. void SpaceBySpace( String pegOrder, String pegEnd){
  295.  
  296. /* pegOrder: This is the current position state of the pegs to be sorted
  297. *
  298. * pegEnd : This is the desired state of the pegs after sorting.
  299. *
  300. */
  301.  
  302. int oldPos = 0, newPos = 0;
  303. int count1 = 0, count2 = 0;
  304. int destination = 0;
  305. bool clawStatus = 0;
  306.  
  307. for(int i = 0; i < 6; i++){
  308.  
  309. if((pegOrder[i] != pegEnd[i])){
  310.  
  311. // if the current Element on the pegOrder does not match the current element
  312. // on the pegEnd position, traverse the pegorder to locate the empty square once
  313. // and for all.
  314.  
  315. while(pegOrder[count2] != '0' && count2 < 6){
  316.  
  317. // count2: This will be the index of the empty square.
  318. count2++;
  319. }
  320.  
  321. //FUNCTION CALL TO SIDE TO SIDE STEPPER.
  322. newPos = i;
  323. destination = count2;
  324.  
  325. if(newPos != destination){
  326. MoveSide2SideStepper2(oldPos, newPos, destination, clawStatus);
  327.  
  328. // Move the incorrect peg to the empty square.
  329. Serial.print("Move Peg ");
  330. Serial.print(pegOrder[i]);
  331. Serial.print(" to space ");
  332. Serial.print(count2);
  333. Serial.println();
  334. }
  335.  
  336. // The index of the empty square now holds the incorrect peg and the empty square
  337. // moves to the former index of the incorrect peg.
  338. pegOrder[count2] = pegOrder[i];
  339. pegOrder[i] = '0';
  340.  
  341. // Now find the correct peg by traversing the pegOrder to find the element
  342. // that equals the element at pegEnd[i].
  343. while(pegEnd[i] != pegOrder[count1] && count1 < 6){
  344.  
  345. // count1: This will be the index of the correct peg in pegOrder.
  346. count1++;
  347. }
  348.  
  349. //FUNCTION CALL TO SIDE TO SIDE STEPPER.
  350. newPos = count1;
  351. destination = i;
  352.  
  353. if(newPos != destination){
  354. MoveSide2SideStepper2(oldPos, newPos, destination, clawStatus);
  355.  
  356. Serial.print("Move Peg ");
  357. Serial.print(pegOrder[i]);
  358. Serial.print(" to space ");
  359. Serial.print(i);
  360. Serial.println();
  361. }
  362. }
  363.  
  364. // Re-Wrote: pegOrder[i] = pegEnd[i] to pegOrder[i] = pegOrder[count1];;
  365. // i did this because it will help us to know what is contained in pegOrder[count1];
  366. // this is to ascertain whether it contains a good or bad data.
  367.  
  368.  
  369. // Insert the correct peg in the pegOrder[i], the former index of the correct peg is
  370. // set to empty space.
  371. pegOrder[i] = pegEnd[i];
  372. pegOrder[count1] = '0';
  373.  
  374. // Making count2 = count1, eliminates the process of looking for the empty square
  375. // all the time, making the run-tim decrease efficiently by 25%.
  376. count2 = count1;
  377. count1 = 0;
  378.  
  379.  
  380. }
  381.  
  382.  
  383. // Print the order of the pegs.
  384.  
  385. Serial.println();
  386. for(int i = 0; i < 6; i++){
  387.  
  388. Serial.print(pegOrder[i]);
  389. }
  390.  
  391. Serial.println();
  392. Serial.println();
  393.  
  394. Serial.println("Pegs are in order.");
  395. }
  396.  
  397. //***********************************************************************
  398.  
  399. void BubbleSortPegs(double* arr, int n){
  400.  
  401. /*
  402. * arr: This is a pointer to an interger or block of integers
  403. * cpy: Just a copy of the array.
  404. * pos: This is a string where the position of the arr is stored
  405. * n : number of integers contained in the block;
  406. */
  407.  
  408. double cpy[n];
  409.  
  410. for(int ind = 0; ind < 6; ind++){
  411.  
  412. cpy[ind] = arr[ind];
  413. }
  414.  
  415. const int EPSILON = 0.0001;
  416.  
  417. for(int ind = 0; ind < n - 1; ind++){
  418.  
  419. for(int ndx = 0; ndx < n - 1; ndx++){
  420.  
  421. if(cpy[ndx] < cpy[ndx + 1]){
  422.  
  423. swap_Peg(&cpy[ndx], &cpy[ndx + 1]);
  424. }
  425. }
  426. }
  427.  
  428.  
  429. for(int ind = 0; ind < n; ind++){
  430.  
  431. for(int ndx = 0; ndx < n; ndx++){
  432.  
  433. if(fabs(cpy[ind] - arr[ndx]) == EPSILON){
  434.  
  435. arr[ndx] = ind;
  436. }
  437. }
  438.  
  439. }
  440.  
  441. }
  442.  
  443. //*****************************************************************
  444.  
  445. void swap_Peg(double* peg1, double* peg2){
  446.  
  447. /*
  448. * Peg1: Pointer to the value of peg1
  449. * peg2: pointer to the value of peg2
  450. */
  451.  
  452. double temp = *peg1;
  453. *peg1 = *peg2;
  454. *peg2 = temp;
  455. }
  456.  
  457. //*****************************************************************
  458.  
  459. void ConvertToChar(double arr[], int size, String& val){
  460.  
  461. for(int ind = 0; ind < size; ind++){
  462.  
  463. val[ind] = arr[ind] + '0';
  464. }
  465.  
  466. }
  467.  
  468. //*****************************************************************
  469.  
  470. long singleMeasurement()
  471. {
  472. long duration = 0;
  473. // Measure: Put up Trigger...
  474. digitalWrite(TRIGGER_PIN, HIGH);
  475. // ... wait for 11 µs ...
  476. delayMicroseconds(11);
  477. // ... put the trigger down ...
  478. digitalWrite(TRIGGER_PIN, LOW);
  479. // ... and wait for the echo ...
  480. duration = pulseIn(ECHO_PIN, HIGH);
  481. return (long) (((float) duration / USONIC_DIV) * 10.0);
  482. }
  483.  
  484.  
  485. long singleMeasurement1()
  486. {
  487. long duration = 0;
  488. // Measure: Put up Trigger...
  489. digitalWrite(TRIGGER_PIN1, HIGH);
  490. // ... wait for 11 µs ...
  491. delayMicroseconds(11);
  492. // ... put the trigger down ...
  493. digitalWrite(TRIGGER_PIN1, LOW);
  494. // ... and wait for the echo ...
  495. duration = pulseIn(ECHO_PIN1, HIGH);
  496. return (long) (((float) duration / USONIC_DIV) * 10.0);
  497. }
  498.  
  499.  
  500. long singleMeasurement2()
  501. {
  502. long duration = 0;
  503. // Measure: Put up Trigger...
  504. digitalWrite(TRIGGER_PIN2, HIGH);
  505. // ... wait for 11 µs ...
  506. delayMicroseconds(11);
  507. // ... put the trigger down ...
  508. digitalWrite(TRIGGER_PIN2, LOW);
  509. // ... and wait for the echo ...
  510. duration = pulseIn(ECHO_PIN2, HIGH);
  511. return (long) (((float) duration / USONIC_DIV) * 10.0);
  512. }
  513.  
  514.  
  515. long singleMeasurement3()
  516. {
  517. long duration = 0;
  518. // Measure: Put up Trigger...
  519. digitalWrite(TRIGGER_PIN3, HIGH);
  520. // ... wait for 11 µs ...
  521. delayMicroseconds(11);
  522. // ... put the trigger down ...
  523. digitalWrite(TRIGGER_PIN3, LOW);
  524. // ... and wait for the echo ...
  525. duration = pulseIn(ECHO_PIN3, HIGH);
  526. return (long) (((float) duration / USONIC_DIV) * 10.0);
  527. }
  528.  
  529. long singleMeasurement4()
  530. {
  531. long duration = 0;
  532. // Measure: Put up Trigger...
  533. digitalWrite(TRIGGER_PIN4, HIGH);
  534. // ... wait for 11 µs ...
  535. delayMicroseconds(11);
  536. // ... put the trigger down ...
  537. digitalWrite(TRIGGER_PIN4, LOW);
  538. // ... and wait for the echo ...
  539. duration = pulseIn(ECHO_PIN4, HIGH);
  540. return (long) (((float) duration / USONIC_DIV) * 10.0);
  541. }
  542.  
  543.  
  544. long singleMeasurement5()
  545. {
  546. long duration = 0;
  547. // Measure: Put up Trigger...
  548. digitalWrite(TRIGGER_PIN5, HIGH);
  549. // ... wait for 11 µs ...
  550. delayMicroseconds(11);
  551. // ... put the trigger down ...
  552. digitalWrite(TRIGGER_PIN5, LOW);
  553. // ... and wait for the echo ...
  554. duration = pulseIn(ECHO_PIN5, HIGH);
  555. return (long) (((float) duration / USONIC_DIV) * 10.0);
  556. }
  557. long measure()
  558. {
  559. long measureSum = 0;
  560. for (int i = 0; i < MEASURE_SAMPLES; i++)
  561. {
  562. delay(MEASURE_SAMPLE_DELAY);
  563. measureSum += singleMeasurement();
  564. }
  565. return measureSum / MEASURE_SAMPLES;
  566. }
  567.  
  568.  
  569. long measure1()
  570. {
  571. long measureSum = 0;
  572. for (int i = 0; i < MEASURE_SAMPLES; i++)
  573. {
  574. delay(MEASURE_SAMPLE_DELAY);
  575. measureSum += singleMeasurement1();
  576. }
  577. return measureSum / MEASURE_SAMPLES;
  578. }
  579.  
  580.  
  581. long measure2()
  582. {
  583. long measureSum = 0;
  584. for (int i = 0; i < MEASURE_SAMPLES; i++)
  585. {
  586. delay(MEASURE_SAMPLE_DELAY);
  587. measureSum += singleMeasurement2();
  588. }
  589. return measureSum / MEASURE_SAMPLES;
  590. }
  591.  
  592.  
  593. long measure3()
  594. {
  595. long measureSum = 0;
  596. for (int i = 0; i < MEASURE_SAMPLES; i++)
  597. {
  598. delay(MEASURE_SAMPLE_DELAY);
  599. measureSum += singleMeasurement3();
  600. }
  601. return measureSum / MEASURE_SAMPLES;
  602. }
  603.  
  604.  
  605. long measure4()
  606. {
  607. long measureSum = 0;
  608. for (int i = 0; i < MEASURE_SAMPLES; i++)
  609. {
  610. delay(MEASURE_SAMPLE_DELAY);
  611. measureSum += singleMeasurement4();
  612. }
  613. return measureSum / MEASURE_SAMPLES;
  614. }
  615.  
  616.  
  617. long measure5()
  618. {
  619. long measureSum = 0;
  620. for (int i = 0; i < MEASURE_SAMPLES; i++)
  621. {
  622. delay(MEASURE_SAMPLE_DELAY);
  623. measureSum += singleMeasurement5();
  624. }
  625. return measureSum / MEASURE_SAMPLES;
  626. }
  627.  
  628. //*****************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement