Advertisement
Guest User

Cycle Test For Arduino

a guest
Apr 8th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.72 KB | None | 0 0
  1. [code]
  2. int RelayPin = 8;
  3. int RelayPin2 = 9;
  4. int motorChoices;
  5. #include <SD.h>
  6.  
  7. struct Sequence
  8.  
  9. {
  10.  
  11. unsigned long ONpulse;
  12.  
  13. unsigned long OFFpulse;
  14.  
  15. unsigned long repeat;
  16.  
  17. unsigned long AddDelay;
  18.  
  19. unsigned long motorSelection;
  20.  
  21. };
  22.  
  23. const int MaxSequences=5;
  24. const int MaxIntervals=50;
  25. Sequence mySequences[MaxSequences];
  26.  
  27. unsigned long PumpIntervalMap [MaxIntervals][2];
  28. int intervalCount=0;
  29. int SeqsInUse_count =0;
  30. int SeqtoRemove = 10;
  31. int menuSelection = 0;
  32. float TotalBrewSecs = 0;
  33. unsigned long TotalBrewTime = 0;
  34.  
  35.  
  36. /*************************************************************************/
  37.  
  38. void setup()
  39.  
  40. {
  41.  
  42.  
  43.  
  44. //______
  45. pinMode(RelayPin, OUTPUT);
  46. pinMode(RelayPin2, OUTPUT);
  47.  
  48. pinMode(10,OUTPUT);
  49. // pinMode(4,OUTPUT);
  50.  
  51.  
  52. // set default values for mySequences
  53.  
  54. for(int i=0; i<MaxSequences;i++)
  55.  
  56. {
  57.  
  58. mySequences[i].ONpulse=0;
  59.  
  60. mySequences[i].OFFpulse=0;
  61.  
  62. mySequences[i].repeat=0;
  63.  
  64. mySequences[i].AddDelay=0;
  65.  
  66. mySequences[i].motorSelection=0;
  67.  
  68. }
  69.  
  70.  
  71.  
  72. //Initialize serial and wait for port to open:
  73.  
  74. Serial.begin(9600);
  75.  
  76. while (!Serial)
  77.  
  78. {
  79.  
  80. ; // wait for serial port to connect. Needed for Leonardo only
  81.  
  82. }
  83.  
  84. Serial.println("Motor Sequencing Tool");
  85. }
  86.  
  87. /********************** Main *******************************************/
  88.  
  89. void loop()
  90.  
  91. {
  92. menuSelection = 0;
  93.  
  94. clearAndHome();
  95.  
  96. Serial.println("Motor Sequencing Tool");
  97.  
  98. menu();
  99.  
  100.  
  101. switch (menuSelection)
  102.  
  103. {
  104.  
  105. case 1: Seq_Add();
  106.  
  107. break;
  108.  
  109. case 2: Seq_Remove();
  110.  
  111. break;
  112.  
  113. case 3: Seq_Mod();
  114.  
  115. break;
  116.  
  117. case 4: Seqs_Execute();
  118.  
  119. default:
  120.  
  121. break;
  122.  
  123. }
  124.  
  125.  
  126. }
  127.  
  128. /*************************************************************************/
  129.  
  130. void clearAndHome()
  131.  
  132. {
  133.  
  134.  
  135. }
  136.  
  137. /************************************************************************/
  138.  
  139. void menu()
  140.  
  141. {
  142.  
  143. TotalBrewTime = 0;
  144.  
  145.  
  146.  
  147. Serial.println("\n-----Motor CYCLE SUMMARY-----\n");
  148.  
  149.  
  150.  
  151. if(SeqsInUse_count <= 0)
  152.  
  153. Serial.print("No Motor Cycle Sequences.");
  154.  
  155. else
  156.  
  157. Seq_dataDisplayAll();
  158.  
  159.  
  160.  
  161. Create_PumpIntervalMap();
  162.  
  163. for(int i=0;i<intervalCount;i++)
  164.  
  165. TotalBrewTime += PumpIntervalMap[i][0];
  166.  
  167.  
  168.  
  169. TotalBrewSecs= float(TotalBrewTime)/1000;
  170.  
  171. Serial.print("\n\n\rTotal Motor Cycle Time(S) : "); Serial.print(TotalBrewSecs);
  172.  
  173.  
  174.  
  175.  
  176.  
  177. Serial.println("\n\n\r--------- MAIN MENU --------\n");
  178.  
  179. Serial.println(" 1 - Add new Motor sequence");
  180.  
  181. Serial.println(" 2 - Remove Motor sequence");
  182.  
  183. Serial.println(" 3 - Modify Motor sequence");
  184.  
  185. Serial.println(" 4 - Execute Motor Cycle");
  186.  
  187. Serial.println("\n----------------------------");
  188.  
  189. Serial.print("\nEnter Selection: ");
  190.  
  191.  
  192.  
  193. while(Serial.available()<=0);
  194.  
  195. menuSelection = Serial.parseInt();
  196.  
  197. Serial.println(menuSelection);
  198.  
  199.  
  200.  
  201. if((menuSelection < 1)||menuSelection > 4)
  202.  
  203. Serial.print("\nInvalid Choice.\n\r");
  204.  
  205. // ^ Makes sure you selected from the choices given
  206.  
  207. }
  208.  
  209. /*************************************************************************/
  210.  
  211. void Seq_dataDisplay(int Seq)
  212.  
  213. {
  214.  
  215.  
  216. Serial.print("\n\rSEQUENCE "); Serial.print(Seq+1);
  217.  
  218. Serial.print("\n\n\r1 - ON TIME(ms) : "); Serial.print(mySequences[Seq].ONpulse);
  219.  
  220. Serial.print("\n\r2 - OFF TIME(ms) : "); Serial.print(mySequences[Seq].OFFpulse);
  221.  
  222. Serial.print("\n\r3 - REPEAT : "); Serial.print(mySequences[Seq].repeat);
  223.  
  224. Serial.print("\n\r4 - DELAY(ms) : "); Serial.print(mySequences[Seq].AddDelay);
  225.  
  226. Serial.print("\n\r5 - Motor Selection: "); Serial.print(mySequences[Seq].motorSelection);
  227.  
  228.  
  229. }
  230.  
  231. /*************************************************************************/
  232.  
  233. void Seq_dataDisplayAll()
  234.  
  235. {
  236.  
  237. for(int j=0; j < SeqsInUse_count; j++)
  238.  
  239. {
  240.  
  241. Serial.print("\n\rSEQUENCE "); Serial.print(j+1);
  242.  
  243. Serial.print(" ON TIME: "); Serial.print(mySequences[j].ONpulse);
  244.  
  245. Serial.print(" | ");
  246.  
  247. //Serial.print("\r\33[30C");
  248.  
  249. Serial.print("OFF TIME: "); Serial.print(mySequences[j].OFFpulse);
  250.  
  251. Serial.print(" | ");
  252. //Serial.print("\r\33[48C");
  253.  
  254. Serial.print("REPEAT: "); Serial.print(mySequences[j].repeat);
  255.  
  256. Serial.print(" | ");
  257.  
  258. //Serial.print("\r\33[62C");
  259.  
  260. Serial.print("DELAY: "); Serial.print(mySequences[j].AddDelay);
  261.  
  262. Serial.print(" | ");
  263.  
  264. Serial.print("Motor Selection(s): Motor(s) "); Serial.print(mySequences[j].motorSelection);
  265.  
  266.  
  267. }
  268.  
  269. }
  270.  
  271. /*************************************************************************/
  272.  
  273. void Seq_Add()
  274.  
  275. {
  276.  
  277.  
  278.  
  279. menuSelection = 0;
  280.  
  281.  
  282.  
  283. while(menuSelection != 6)
  284.  
  285. {
  286.  
  287. clearAndHome();
  288.  
  289. Serial.println("Motor Sequencing Tool");
  290.  
  291. Serial.print("\nAdd Sequence values.\n\r");
  292.  
  293.  
  294.  
  295. Seq_dataDisplay(SeqsInUse_count);
  296.  
  297. Serial.print("\n\r6 - DONE");
  298.  
  299.  
  300.  
  301. Serial.print("\n\n\rSelect value to set: ");
  302.  
  303. while(Serial.available()<=0);
  304.  
  305. menuSelection = Serial.parseInt();
  306.  
  307. Serial.print(menuSelection);
  308.  
  309.  
  310.  
  311. if((menuSelection<1)||(menuSelection>6))
  312.  
  313. Serial.print("\n\rInvalid Choice.");
  314.  
  315. else
  316.  
  317. {
  318.  
  319.  
  320.  
  321. switch (menuSelection)
  322.  
  323. {
  324.  
  325. case 1:
  326.  
  327. Serial.print("\n\rEnter ON TIME: ");
  328.  
  329. while(Serial.available()<=0);
  330.  
  331. mySequences[SeqsInUse_count].ONpulse = Serial.parseInt();
  332.  
  333. break;
  334.  
  335. case 2:
  336.  
  337. Serial.print("\n\rEnter OFF TIME: ");
  338.  
  339. while(Serial.available()<=0);
  340.  
  341. mySequences[SeqsInUse_count].OFFpulse = Serial.parseInt();
  342.  
  343. break;
  344.  
  345. case 3:
  346.  
  347. Serial.print("\n\rEnter REPEAT: ");
  348.  
  349. while(Serial.available()<=0);
  350.  
  351. mySequences[SeqsInUse_count].repeat = Serial.parseInt();
  352.  
  353. break;
  354.  
  355. case 4:
  356.  
  357. Serial.print("\n\rEnter DELAY: ");
  358.  
  359. while(Serial.available()<=0);
  360.  
  361. mySequences[SeqsInUse_count].AddDelay = Serial.parseInt();
  362.  
  363. break;
  364.  
  365. default:
  366.  
  367. SeqsInUse_count++;
  368.  
  369. break;
  370.  
  371. case 5:
  372.  
  373.  
  374. motorSelection();
  375.  
  376. mySequences[SeqsInUse_count].motorSelection = motorChoices;//Serial.parseInt();
  377.  
  378.  
  379. break;
  380.  
  381.  
  382.  
  383. }
  384.  
  385. }
  386.  
  387. }
  388.  
  389.  
  390.  
  391. }
  392.  
  393. /*************************************************************************/
  394.  
  395. void Seq_Remove()
  396.  
  397. {
  398.  
  399.  
  400.  
  401. clearAndHome();
  402.  
  403. Serial.println("Motor Sequencing Tool");
  404.  
  405. Seq_dataDisplayAll();
  406.  
  407.  
  408.  
  409. Serial.print("\n\n\rEnter Sequence number to remove: ");
  410.  
  411. while(Serial.available()<=0);
  412.  
  413. SeqtoRemove = Serial.parseInt();
  414.  
  415.  
  416. Serial.print(SeqtoRemove);
  417.  
  418.  
  419.  
  420. if((SeqtoRemove > 0)&&(SeqtoRemove <= SeqsInUse_count))
  421.  
  422. {
  423.  
  424. for(int k=(SeqtoRemove-1); k<SeqsInUse_count; k++)
  425.  
  426. {
  427.  
  428. if(k == (SeqsInUse_count-1))
  429.  
  430. {
  431.  
  432. mySequences[k].ONpulse = 0;
  433.  
  434. mySequences[k].OFFpulse = 0;
  435.  
  436. mySequences[k].repeat = 0;
  437.  
  438. mySequences[k].AddDelay = 0;
  439.  
  440. mySequences[k].motorSelection = 0;
  441.  
  442. break;
  443.  
  444. }
  445.  
  446. else
  447.  
  448. {
  449.  
  450. mySequences[k] = mySequences[k+1];
  451.  
  452. }
  453.  
  454. }
  455.  
  456.  
  457.  
  458. SeqsInUse_count--;
  459.  
  460. }
  461.  
  462.  
  463.  
  464. }
  465.  
  466. /*************************************************************************/
  467.  
  468. void Seq_Mod()
  469.  
  470. {
  471.  
  472. int SeqtoMod=100;
  473.  
  474. clearAndHome();
  475.  
  476. Serial.println("Motor Sequencing Tool");
  477.  
  478. Seq_dataDisplayAll();
  479.  
  480.  
  481.  
  482. Serial.print("\n\n\rEnter Sequence number to modify: ");
  483.  
  484. while(Serial.available()<=0);
  485.  
  486. SeqtoMod = Serial.parseInt();
  487.  
  488. Serial.print(SeqtoMod);
  489.  
  490.  
  491.  
  492. if((SeqtoMod >0)&&(SeqtoMod <= SeqsInUse_count))
  493.  
  494. {
  495.  
  496. while(menuSelection != 6)
  497.  
  498. {
  499.  
  500. clearAndHome();
  501.  
  502. Serial.println("Motor Sequencing Tool");
  503.  
  504. Serial.print("\n\rModifying: \n\r");
  505.  
  506.  
  507.  
  508. Seq_dataDisplay(SeqtoMod-1);
  509.  
  510. Serial.print("\n\r6 - DONE");
  511.  
  512.  
  513.  
  514. Serial.print("\n\n\rSelect value to set: ");
  515.  
  516. while(Serial.available()<=0);
  517.  
  518. menuSelection = Serial.parseInt();
  519.  
  520. Serial.print(menuSelection);
  521.  
  522.  
  523.  
  524. if((menuSelection<1)||(menuSelection>6))
  525.  
  526. Serial.print("\n\rInvalid Choice.");
  527.  
  528. else
  529.  
  530. {
  531.  
  532.  
  533.  
  534. switch (menuSelection)
  535.  
  536. {
  537.  
  538. case 1:
  539.  
  540. Serial.print("\n\rEnter ON TIME: ");
  541.  
  542. while(Serial.available()<=0);
  543.  
  544. mySequences[SeqtoMod-1].ONpulse = Serial.parseInt();
  545.  
  546. break;
  547.  
  548. case 2:
  549.  
  550. Serial.print("\n\rEnter OFF TIME: ");
  551.  
  552. while(Serial.available()<=0);
  553.  
  554. mySequences[SeqtoMod-1].OFFpulse = Serial.parseInt();
  555.  
  556. break;
  557.  
  558. case 3:
  559.  
  560. Serial.print("\n\rEnter REPEAT: ");
  561.  
  562. while(Serial.available()<=0);
  563.  
  564. mySequences[SeqtoMod-1].repeat = Serial.parseInt();
  565.  
  566. break;
  567.  
  568. case 4:
  569.  
  570. Serial.print("\n\rEnter DELAY: ");
  571.  
  572. while(Serial.available()<=0);
  573.  
  574. mySequences[SeqtoMod-1].AddDelay = Serial.parseInt();
  575.  
  576. break;
  577.  
  578. case 5:
  579.  
  580. Serial.print("\n\rEnter New Motor Selection(s): ");
  581.  
  582. while(Serial.available()<=0);
  583.  
  584. motorChoices = Serial.parseInt();
  585. mySequences[SeqtoMod-1].motorSelection = motorChoices;//Serial.parseInt(),
  586.  
  587.  
  588.  
  589. break;
  590.  
  591. //default:
  592.  
  593. // break;
  594.  
  595. }
  596.  
  597. }
  598.  
  599. }
  600.  
  601. }
  602.  
  603.  
  604.  
  605. }
  606.  
  607. /*************************************************************************/
  608. void Seqs_Execute()
  609. {
  610.  
  611. unsigned long StartTime = 0;
  612.  
  613. unsigned long CurrentTime=0;
  614.  
  615. unsigned long currentInterval=0;
  616.  
  617. TotalBrewSecs = 0;
  618.  
  619.  
  620. int i=0;
  621.  
  622. boolean setDisplay=false;
  623.  
  624. TotalBrewTime = 0;
  625.  
  626.  
  627.  
  628. Create_PumpIntervalMap();
  629.  
  630.  
  631.  
  632. clearAndHome();
  633.  
  634. Serial.println("Motor Sequencing Tool");
  635.  
  636.  
  637.  
  638. //Serial.print("\n\r");
  639.  
  640. Serial.println("Motor Cycle Sequence Execution:");
  641.  
  642. Seq_dataDisplayAll();
  643.  
  644.  
  645.  
  646. Serial.println("\n\n\r(During Motor cycle, press any key to cancel)");
  647.  
  648.  
  649. for(i=0;i<intervalCount;i++)
  650.  
  651. TotalBrewTime += PumpIntervalMap[i][0];
  652.  
  653.  
  654.  
  655. TotalBrewSecs= float(TotalBrewTime)/1000;
  656.  
  657. Serial.print("\n\rTotal Motor Test Cycle Time(S) : "); Serial.print(TotalBrewSecs);
  658.  
  659.  
  660.  
  661. Serial.println("\n\n\rPRESS ANY KEY TO EXECUTE: ");
  662.  
  663. while(Serial.available()<=0);
  664.  
  665. menuSelection = Serial.parseInt();
  666.  
  667. //Serial.print("\n\r");
  668.  
  669.  
  670.  
  671. i=0;
  672.  
  673. while((i<intervalCount)&&(Serial.available() <= 0))
  674.  
  675. {
  676.  
  677.  
  678.  
  679. CurrentTime = millis();
  680.  
  681.  
  682.  
  683. if((i==0)||(setDisplay))
  684.  
  685. {
  686.  
  687. if(setDisplay)
  688.  
  689. {
  690.  
  691.  
  692. setDisplay=false;
  693.  
  694. }
  695.  
  696.  
  697.  
  698.  
  699.  
  700. Serial.println("\rInterval(ms): ");
  701.  
  702.  
  703. Serial.println("\nMotor state: ");
  704.  
  705.  
  706.  
  707. }
  708.  
  709.  
  710.  
  711. if((StartTime+currentInterval)<=CurrentTime)
  712.  
  713. {
  714.  
  715. StartTime=millis();
  716.  
  717.  
  718.  
  719. //operate pump
  720. //*****************************************************************
  721.  
  722. if (motorChoices== 1){
  723. if(PumpIntervalMap[i][1]) {
  724.  
  725.  
  726. digitalWrite(RelayPin, HIGH);
  727. }
  728. else
  729.  
  730. digitalWrite(RelayPin, LOW);
  731. }
  732.  
  733. if (motorChoices== 2){
  734. if(PumpIntervalMap[i][1]) {
  735. digitalWrite(RelayPin2, HIGH);
  736. }
  737. else
  738.  
  739. digitalWrite(RelayPin2, LOW);
  740. }
  741.  
  742. if (motorChoices== 12){
  743. if(PumpIntervalMap[i][1]) {
  744.  
  745.  
  746. digitalWrite(RelayPin, HIGH),digitalWrite(RelayPin2,HIGH);
  747.  
  748. }
  749.  
  750. else
  751.  
  752. digitalWrite(RelayPin, LOW),digitalWrite(RelayPin2,LOW);
  753. }
  754.  
  755.  
  756. Serial.print(PumpIntervalMap[i][0]);
  757.  
  758.  
  759.  
  760.  
  761.  
  762. if(PumpIntervalMap[i][1]==1)
  763.  
  764. Serial.print("|ON|");
  765.  
  766. else
  767.  
  768. Serial.print("|OFF|");
  769.  
  770.  
  771.  
  772. currentInterval=PumpIntervalMap[i][0];
  773.  
  774. i++;
  775.  
  776.  
  777.  
  778. if(i%9==0)
  779.  
  780. setDisplay=true;
  781.  
  782. }
  783.  
  784. }
  785.  
  786.  
  787. menuSelection = Serial.parseInt();
  788.  
  789.  
  790.  
  791. }
  792.  
  793. /*************************************************************************/
  794.  
  795. void Create_PumpIntervalMap()
  796.  
  797. {
  798.  
  799. intervalCount=0;
  800.  
  801. int j =0;
  802.  
  803.  
  804. for(int i=0;i<SeqsInUse_count;i++)
  805.  
  806. {
  807.  
  808.  
  809. for(j=0; j<=mySequences[i].repeat;j++)
  810.  
  811. {
  812.  
  813. PumpIntervalMap[intervalCount][0]=mySequences[i].ONpulse;
  814.  
  815. PumpIntervalMap[intervalCount][1]=1;//pump on message
  816.  
  817.  
  818. intervalCount++;
  819.  
  820.  
  821. PumpIntervalMap[intervalCount][0]=mySequences[i].OFFpulse;
  822.  
  823. PumpIntervalMap[intervalCount][1]=0;//pump off message
  824.  
  825.  
  826.  
  827. intervalCount++;
  828.  
  829. }
  830.  
  831.  
  832.  
  833. //add delay
  834.  
  835. PumpIntervalMap[intervalCount][0]=mySequences[i].AddDelay;
  836.  
  837. PumpIntervalMap[intervalCount][1]=0;//Add delay
  838.  
  839. intervalCount++;
  840.  
  841.  
  842. PumpIntervalMap[intervalCount][0]=mySequences[i].ONpulse;
  843.  
  844. PumpIntervalMap[intervalCount][2]=0;//Add delay
  845.  
  846. intervalCount++;
  847.  
  848.  
  849. }
  850.  
  851.  
  852.  
  853. }
  854.  
  855. /*************************************************************************/
  856. void motorSelection(){
  857.  
  858. Serial.println("Motor Selection Menu");
  859. Serial.println("Please select which motor(s) to use for cycle testing");
  860. Serial.println("Ex: 135 will power motors 1, 3 and 5 ");
  861. while(Serial.available()<=0);
  862.  
  863. motorChoices = Serial.parseInt();
  864.  
  865. }
  866.  
  867. /*************************************************************************/
  868. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement