Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [code]
- int RelayPin = 8;
- int RelayPin2 = 9;
- int motorChoices;
- #include <SD.h>
- struct Sequence
- {
- unsigned long ONpulse;
- unsigned long OFFpulse;
- unsigned long repeat;
- unsigned long AddDelay;
- unsigned long motorSelection;
- };
- const int MaxSequences=5;
- const int MaxIntervals=50;
- Sequence mySequences[MaxSequences];
- unsigned long PumpIntervalMap [MaxIntervals][2];
- int intervalCount=0;
- int SeqsInUse_count =0;
- int SeqtoRemove = 10;
- int menuSelection = 0;
- float TotalBrewSecs = 0;
- unsigned long TotalBrewTime = 0;
- /*************************************************************************/
- void setup()
- {
- //______
- pinMode(RelayPin, OUTPUT);
- pinMode(RelayPin2, OUTPUT);
- pinMode(10,OUTPUT);
- // pinMode(4,OUTPUT);
- // set default values for mySequences
- for(int i=0; i<MaxSequences;i++)
- {
- mySequences[i].ONpulse=0;
- mySequences[i].OFFpulse=0;
- mySequences[i].repeat=0;
- mySequences[i].AddDelay=0;
- mySequences[i].motorSelection=0;
- }
- //Initialize serial and wait for port to open:
- Serial.begin(9600);
- while (!Serial)
- {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- Serial.println("Motor Sequencing Tool");
- }
- /********************** Main *******************************************/
- void loop()
- {
- menuSelection = 0;
- clearAndHome();
- Serial.println("Motor Sequencing Tool");
- menu();
- switch (menuSelection)
- {
- case 1: Seq_Add();
- break;
- case 2: Seq_Remove();
- break;
- case 3: Seq_Mod();
- break;
- case 4: Seqs_Execute();
- default:
- break;
- }
- }
- /*************************************************************************/
- void clearAndHome()
- {
- }
- /************************************************************************/
- void menu()
- {
- TotalBrewTime = 0;
- Serial.println("\n-----Motor CYCLE SUMMARY-----\n");
- if(SeqsInUse_count <= 0)
- Serial.print("No Motor Cycle Sequences.");
- else
- Seq_dataDisplayAll();
- Create_PumpIntervalMap();
- for(int i=0;i<intervalCount;i++)
- TotalBrewTime += PumpIntervalMap[i][0];
- TotalBrewSecs= float(TotalBrewTime)/1000;
- Serial.print("\n\n\rTotal Motor Cycle Time(S) : "); Serial.print(TotalBrewSecs);
- Serial.println("\n\n\r--------- MAIN MENU --------\n");
- Serial.println(" 1 - Add new Motor sequence");
- Serial.println(" 2 - Remove Motor sequence");
- Serial.println(" 3 - Modify Motor sequence");
- Serial.println(" 4 - Execute Motor Cycle");
- Serial.println("\n----------------------------");
- Serial.print("\nEnter Selection: ");
- while(Serial.available()<=0);
- menuSelection = Serial.parseInt();
- Serial.println(menuSelection);
- if((menuSelection < 1)||menuSelection > 4)
- Serial.print("\nInvalid Choice.\n\r");
- // ^ Makes sure you selected from the choices given
- }
- /*************************************************************************/
- void Seq_dataDisplay(int Seq)
- {
- Serial.print("\n\rSEQUENCE "); Serial.print(Seq+1);
- Serial.print("\n\n\r1 - ON TIME(ms) : "); Serial.print(mySequences[Seq].ONpulse);
- Serial.print("\n\r2 - OFF TIME(ms) : "); Serial.print(mySequences[Seq].OFFpulse);
- Serial.print("\n\r3 - REPEAT : "); Serial.print(mySequences[Seq].repeat);
- Serial.print("\n\r4 - DELAY(ms) : "); Serial.print(mySequences[Seq].AddDelay);
- Serial.print("\n\r5 - Motor Selection: "); Serial.print(mySequences[Seq].motorSelection);
- }
- /*************************************************************************/
- void Seq_dataDisplayAll()
- {
- for(int j=0; j < SeqsInUse_count; j++)
- {
- Serial.print("\n\rSEQUENCE "); Serial.print(j+1);
- Serial.print(" ON TIME: "); Serial.print(mySequences[j].ONpulse);
- Serial.print(" | ");
- //Serial.print("\r\33[30C");
- Serial.print("OFF TIME: "); Serial.print(mySequences[j].OFFpulse);
- Serial.print(" | ");
- //Serial.print("\r\33[48C");
- Serial.print("REPEAT: "); Serial.print(mySequences[j].repeat);
- Serial.print(" | ");
- //Serial.print("\r\33[62C");
- Serial.print("DELAY: "); Serial.print(mySequences[j].AddDelay);
- Serial.print(" | ");
- Serial.print("Motor Selection(s): Motor(s) "); Serial.print(mySequences[j].motorSelection);
- }
- }
- /*************************************************************************/
- void Seq_Add()
- {
- menuSelection = 0;
- while(menuSelection != 6)
- {
- clearAndHome();
- Serial.println("Motor Sequencing Tool");
- Serial.print("\nAdd Sequence values.\n\r");
- Seq_dataDisplay(SeqsInUse_count);
- Serial.print("\n\r6 - DONE");
- Serial.print("\n\n\rSelect value to set: ");
- while(Serial.available()<=0);
- menuSelection = Serial.parseInt();
- Serial.print(menuSelection);
- if((menuSelection<1)||(menuSelection>6))
- Serial.print("\n\rInvalid Choice.");
- else
- {
- switch (menuSelection)
- {
- case 1:
- Serial.print("\n\rEnter ON TIME: ");
- while(Serial.available()<=0);
- mySequences[SeqsInUse_count].ONpulse = Serial.parseInt();
- break;
- case 2:
- Serial.print("\n\rEnter OFF TIME: ");
- while(Serial.available()<=0);
- mySequences[SeqsInUse_count].OFFpulse = Serial.parseInt();
- break;
- case 3:
- Serial.print("\n\rEnter REPEAT: ");
- while(Serial.available()<=0);
- mySequences[SeqsInUse_count].repeat = Serial.parseInt();
- break;
- case 4:
- Serial.print("\n\rEnter DELAY: ");
- while(Serial.available()<=0);
- mySequences[SeqsInUse_count].AddDelay = Serial.parseInt();
- break;
- default:
- SeqsInUse_count++;
- break;
- case 5:
- motorSelection();
- mySequences[SeqsInUse_count].motorSelection = motorChoices;//Serial.parseInt();
- break;
- }
- }
- }
- }
- /*************************************************************************/
- void Seq_Remove()
- {
- clearAndHome();
- Serial.println("Motor Sequencing Tool");
- Seq_dataDisplayAll();
- Serial.print("\n\n\rEnter Sequence number to remove: ");
- while(Serial.available()<=0);
- SeqtoRemove = Serial.parseInt();
- Serial.print(SeqtoRemove);
- if((SeqtoRemove > 0)&&(SeqtoRemove <= SeqsInUse_count))
- {
- for(int k=(SeqtoRemove-1); k<SeqsInUse_count; k++)
- {
- if(k == (SeqsInUse_count-1))
- {
- mySequences[k].ONpulse = 0;
- mySequences[k].OFFpulse = 0;
- mySequences[k].repeat = 0;
- mySequences[k].AddDelay = 0;
- mySequences[k].motorSelection = 0;
- break;
- }
- else
- {
- mySequences[k] = mySequences[k+1];
- }
- }
- SeqsInUse_count--;
- }
- }
- /*************************************************************************/
- void Seq_Mod()
- {
- int SeqtoMod=100;
- clearAndHome();
- Serial.println("Motor Sequencing Tool");
- Seq_dataDisplayAll();
- Serial.print("\n\n\rEnter Sequence number to modify: ");
- while(Serial.available()<=0);
- SeqtoMod = Serial.parseInt();
- Serial.print(SeqtoMod);
- if((SeqtoMod >0)&&(SeqtoMod <= SeqsInUse_count))
- {
- while(menuSelection != 6)
- {
- clearAndHome();
- Serial.println("Motor Sequencing Tool");
- Serial.print("\n\rModifying: \n\r");
- Seq_dataDisplay(SeqtoMod-1);
- Serial.print("\n\r6 - DONE");
- Serial.print("\n\n\rSelect value to set: ");
- while(Serial.available()<=0);
- menuSelection = Serial.parseInt();
- Serial.print(menuSelection);
- if((menuSelection<1)||(menuSelection>6))
- Serial.print("\n\rInvalid Choice.");
- else
- {
- switch (menuSelection)
- {
- case 1:
- Serial.print("\n\rEnter ON TIME: ");
- while(Serial.available()<=0);
- mySequences[SeqtoMod-1].ONpulse = Serial.parseInt();
- break;
- case 2:
- Serial.print("\n\rEnter OFF TIME: ");
- while(Serial.available()<=0);
- mySequences[SeqtoMod-1].OFFpulse = Serial.parseInt();
- break;
- case 3:
- Serial.print("\n\rEnter REPEAT: ");
- while(Serial.available()<=0);
- mySequences[SeqtoMod-1].repeat = Serial.parseInt();
- break;
- case 4:
- Serial.print("\n\rEnter DELAY: ");
- while(Serial.available()<=0);
- mySequences[SeqtoMod-1].AddDelay = Serial.parseInt();
- break;
- case 5:
- Serial.print("\n\rEnter New Motor Selection(s): ");
- while(Serial.available()<=0);
- motorChoices = Serial.parseInt();
- mySequences[SeqtoMod-1].motorSelection = motorChoices;//Serial.parseInt(),
- break;
- //default:
- // break;
- }
- }
- }
- }
- }
- /*************************************************************************/
- void Seqs_Execute()
- {
- unsigned long StartTime = 0;
- unsigned long CurrentTime=0;
- unsigned long currentInterval=0;
- TotalBrewSecs = 0;
- int i=0;
- boolean setDisplay=false;
- TotalBrewTime = 0;
- Create_PumpIntervalMap();
- clearAndHome();
- Serial.println("Motor Sequencing Tool");
- //Serial.print("\n\r");
- Serial.println("Motor Cycle Sequence Execution:");
- Seq_dataDisplayAll();
- Serial.println("\n\n\r(During Motor cycle, press any key to cancel)");
- for(i=0;i<intervalCount;i++)
- TotalBrewTime += PumpIntervalMap[i][0];
- TotalBrewSecs= float(TotalBrewTime)/1000;
- Serial.print("\n\rTotal Motor Test Cycle Time(S) : "); Serial.print(TotalBrewSecs);
- Serial.println("\n\n\rPRESS ANY KEY TO EXECUTE: ");
- while(Serial.available()<=0);
- menuSelection = Serial.parseInt();
- //Serial.print("\n\r");
- i=0;
- while((i<intervalCount)&&(Serial.available() <= 0))
- {
- CurrentTime = millis();
- if((i==0)||(setDisplay))
- {
- if(setDisplay)
- {
- setDisplay=false;
- }
- Serial.println("\rInterval(ms): ");
- Serial.println("\nMotor state: ");
- }
- if((StartTime+currentInterval)<=CurrentTime)
- {
- StartTime=millis();
- //operate pump
- //*****************************************************************
- if (motorChoices== 1){
- if(PumpIntervalMap[i][1]) {
- digitalWrite(RelayPin, HIGH);
- }
- else
- digitalWrite(RelayPin, LOW);
- }
- if (motorChoices== 2){
- if(PumpIntervalMap[i][1]) {
- digitalWrite(RelayPin2, HIGH);
- }
- else
- digitalWrite(RelayPin2, LOW);
- }
- if (motorChoices== 12){
- if(PumpIntervalMap[i][1]) {
- digitalWrite(RelayPin, HIGH),digitalWrite(RelayPin2,HIGH);
- }
- else
- digitalWrite(RelayPin, LOW),digitalWrite(RelayPin2,LOW);
- }
- Serial.print(PumpIntervalMap[i][0]);
- if(PumpIntervalMap[i][1]==1)
- Serial.print("|ON|");
- else
- Serial.print("|OFF|");
- currentInterval=PumpIntervalMap[i][0];
- i++;
- if(i%9==0)
- setDisplay=true;
- }
- }
- menuSelection = Serial.parseInt();
- }
- /*************************************************************************/
- void Create_PumpIntervalMap()
- {
- intervalCount=0;
- int j =0;
- for(int i=0;i<SeqsInUse_count;i++)
- {
- for(j=0; j<=mySequences[i].repeat;j++)
- {
- PumpIntervalMap[intervalCount][0]=mySequences[i].ONpulse;
- PumpIntervalMap[intervalCount][1]=1;//pump on message
- intervalCount++;
- PumpIntervalMap[intervalCount][0]=mySequences[i].OFFpulse;
- PumpIntervalMap[intervalCount][1]=0;//pump off message
- intervalCount++;
- }
- //add delay
- PumpIntervalMap[intervalCount][0]=mySequences[i].AddDelay;
- PumpIntervalMap[intervalCount][1]=0;//Add delay
- intervalCount++;
- PumpIntervalMap[intervalCount][0]=mySequences[i].ONpulse;
- PumpIntervalMap[intervalCount][2]=0;//Add delay
- intervalCount++;
- }
- }
- /*************************************************************************/
- void motorSelection(){
- Serial.println("Motor Selection Menu");
- Serial.println("Please select which motor(s) to use for cycle testing");
- Serial.println("Ex: 135 will power motors 1, 3 and 5 ");
- while(Serial.available()<=0);
- motorChoices = Serial.parseInt();
- }
- /*************************************************************************/
- [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement