Thank you everyone for your feedback. It really helped get the wheels turning in my mind and gave me some direction.
wildbill:
I would define a struct to hold the parameters that you pass to the impactNow function and declare an array of them, initializing it with the data you now pass to the function with all those calls.
Then keep a global to tell you which element of the array you have got to. Have trainingMode use millis to decide when it's time to move on to the next one. If it isn't, trainingMode will return to loop so it can read the serial port.
I did get this working, and have rewritten a lot of my code, fortunately reducing it quite a bit. I have used 2 arrays, 1 is a single dimension and the other is multi-dimensional. I separated out the values for the "gap" and put them in their own array as it was just easier to tweak them that way rather than line by line of the other array with the remaining 4 values.
I changed my if else if statements to a switch case as well.
Most of the strings that were present were strictly for troubleshooting, but I also removed the few that were part of the functional code.
/* PINS */
#define VAC_POS 12 //red positive 12v
#define VAC_SEAT 11 //black common negative
#define VAC_IGN 10 //orange seatswitch output
#define VAC_MOT 9 //brown ignition output
#define VAC_IMP_P 8 //blue motion output
#define VAC_IMP_Y 7 //green impact Y axis
#define VAC_IMP_X 6 //white impact X axis
#define CAL_INDICATOR 5 //calibration start/stop
#define CAL_FOCUS 4 //calibration focus
/* VARIABLES */
int count; //Count impact cycles
byte cal = 0; //Record the state of calibration
float xG = 84; //Defines the float for the X Axis impact Force
int xD; //Defines the integer fo rht X Axis impact duration
float yG = 84; //Defines the float for the Y Axis impact Force
int yD; //Defines the integer for the Y Axis impact duration
int long Gap ; //Defines the integer for the Gap between impacts
int cP = 0; //Defines the integer for updating the progress bar on display
int X = 0; //Defines the integer for the X Axis Algorithm
int Y = 0; //Defines the interger for the Y Axis Algorithm
unsigned long currentTime = 0;
unsigned long previousTime = 0; //Update and store Previous Time
unsigned long minWait = 0; //Minimum Wait time for next impact
unsigned long maxWait = 0; //Maximum Wait time for next impact
//byte VAC_IMP_X = 201;
//byte VAC_IMP_Y = 202;
int switches; //Defines the integer for the Switch Cases
/* ARRAYS */
float impWait[25] = { //Impact Delays
0, 1000, 1000, 2000, 1000, 1000, 2000, 2000, 2000, 1000, 1000, 1000, 5000,
1000, 1000, 2000, 1000, 1000, 2000, 2000, 2000, 1000, 1000, 1000, 5000,
};
float impParam [25][5] = { //Impact Parameters
{0.6, 6, 0, 0, 20000},
{0.6, 2, 0.6, 16, 2000},
{0.6, 22, 0.6, 12, 2000},
{1, 251, 0.6, 49, 212000},
{0.7, 252, 0, 0, 102000},
{0.7, 17, 0.6, 7, 192000},
{0.8, 40, 0.6, 13, 10000},
{1, 101, 0.6, 16, 38000},
{0, 35, 0.8, 38, 20000},
{0, 1, 0.6, 8, 8000},
{1.3, 114, 0, 3, 96000},
{0.8, 24, 0.6, 25, 14000},
{0, 0, 0.6, 18, 30000},
{0.8, 16, 1.1, 10, 10000},
{1, 28, 0, 0, 56000},
{0, 0, 0.7, 20, 90000},
{1.1, 76, 0.7, 39, 110000},
{1, 109, 0, 0, 12000},
{1.2, 78, 0.8, 20, 88000},
{1.8, 25, 0.7, 15, 72000},
{1, 174, 0, 0, 12000},
{1, 62, 0.7, 1, 42000},
{1.6, 13, 0.8, 19, 8000},
{0.8, 5, 0.6, 8, 200000},
{1, 16, 0.8, 23, 0},
};
void setup() {
Serial.begin(115200);
Serial.println("Ready to begin serial communication");
pinMode(VAC_POS, OUTPUT);
pinMode(VAC_SEAT, OUTPUT);
pinMode(VAC_IGN, OUTPUT);
pinMode(VAC_MOT, OUTPUT);
pinMode(VAC_IMP_P, INPUT);
pinMode(VAC_IMP_Y, OUTPUT);
pinMode(VAC_IMP_X, OUTPUT);
pinMode(CAL_INDICATOR, OUTPUT);
pinMode(CAL_FOCUS, OUTPUT);
}
void loop() {
if (Serial.available()) { //Check to see if Serial command is available
int data_from_display = 0;
delay(30); //30ms delay to catch entire string without double touch
while (Serial.available()) {
data_from_display += (Serial.parseInt()); //Collect data and append to itself
}
switches = data_from_display;
controls();
}
while (cal == 1 && count < 25) {
hitme(impParam[count][0], impParam[count][1], impParam[count][2], impParam[count][3], impWait[count]);
break;
}
}
Controls Code:
/* Switch Cases */
void controls() {
switch (switches) {
case 10:
Serial.println("Power On");
break;
case 11:
Serial.println("Power Off");
break;
case 20:
Serial.println("SeatSwitch On");
break;
case 21:
Serial.println("SeatSwitch Off");
break;
case 30:
Serial.println("Engine On");
break;
case 31:
Serial.println("VAC ONE: Electric VAC TWO: Electric");
break;
case 32:
Serial.println("VAC ONE: Electric VAC TWO: Gas");
break;
case 33:
Serial.println("VAC ONE: Gas VAC TWO: Electric");
break;
case 34:
Serial.println("VAC ONE: Gas VAC TWO: Gas");
break;
case 35:
Serial.println("VAC ONE: Shutdown VAC TWO: Shutdown");
break;
case 40:
Serial.println("Motion On");
break;
case 41:
Serial.println("Motion Off");
break;
case 50:
Serial.println("Calibration On");
cal = 1;
break;
case 51:
Serial.println("Calibration Off");
cal = 0;
count = 0;
Serial.println("Count has been reset! " + String(count));
break;
}
}
Impacts Code:
void hitme(float xG , int xD , float yG , int yD , int long Gap ) {
currentTime = millis();
minWait = currentTime - previousTime;
maxWait = Gap + 10;
/*Start Math*/
X = round(xG * 6) + 84; //take the G value from VP and multiply by 6 and use the internal round function to get to a whole number for X Axis
Y = round(yG * 6) + 84; //take the G value from VP and multiply by 6 and use the internal round function to get to a whole number for Y Axis
if (minWait > Gap ) {
if (minWait < maxWait) {
analogWrite (VAC_IMP_X, X); //Impact on X Axis is written to the PWM
analogWrite (VAC_IMP_Y, Y); //Impact on Y Axis is written to the PWM
if (xD < yD) { //Compare duration , then set them back to zero G in the proper order
delay(xD);
analogWrite(VAC_IMP_X, 84);
delay(yD - xD);
analogWrite(VAC_IMP_Y, 84);
}
else if (xD > yD) {
delay(yD);
analogWrite(VAC_IMP_Y, 84);
delay(xD - yD);
analogWrite(VAC_IMP_X, 84);
}
/*Update Progress Bar */
Serial.print("caltime.val=" + String(cP));
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
cP = cP + 4; //Update progress bar by 4%
}
previousTime = currentTime;
count++;
}
}