Because what I want to do with the button presses in order to control certain components, is there a way to incorporate this into the current version of the code?
Yes, you can place what is usually presented in the loop() function, in one of the three functions for which I gave you the framework,
Would I need to write this out as some sort of array for this to work using my current sketch? Or is there a better way to do it?
You can write it out completely, you can use arrays or you can use structs.
If we continue with structs, you can start by combining a PWM value and a pin number in a struct.
// information for a TLC pin/value pair
struct TLCPINVALUE
{
byte pin;
int value;
};
And next you can define three structs that make use of the above; demonstrated for airflow
// airflow control information
struct AIRFLOWCONTROL
{
TLCPINVALUE leds[6];
TLCPINVALUE motor;
};
According to your description, you have 6 leds (3x2) that you want to control and one motor. You can create similar structs HOTCOLDCONTROL and POWERLEVELCONTROL; they can use the same TLCPINVALUE struct.
Next you can adjust the controlAirFlow function. First add an array of AIRFLOWCONTROL structures, one for each count.
bool controlAirFlow(byte counter)
{
AIRFLOWCONTROL ctrl[] =
{
// counter == 0
{
// array of TLC for all leds related to airflow
{
// 1 2 3 4 5 6
{2, 2048}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0},
},
// single TLC for airflow motor
{16, 0}
},
// counter == 1
{
{
{2, 0}, {3, 0}, {4, 2048}, {5, 0}, {6, 0}, {7, 0},
},
{16, 512}
},
// counter == 2
{
// L1 L2 L3 L4 L5 L6 M
{{2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 2048}, {7, 0},}, {16, 2048}
},
};
...
...
For clarity (or confusion), I have shown three ways that are the same, just spread over more lines or less lines. The first one is the easiest to follow with documentation, the second one is a bit shorter (less documentation) and the last one is what I usually use because it's shorter (see full code at end).
Next we can modify the print of the module to take into account the number of elements in the ctrl array.
Serial.print("Air flow; counter modulo X: ");
Serial.println(counter % NUMELEMENTS(ctrl));
The above is purely for debugging and can later be removed; the print result should be 0, 1, 2, else something is wrong.
In the last step you can control the leds and the motor. The variable counter that is passed to the function is now an indicator which indicates which element of the ctrl array needs to be accessed. To limit the index we can use a modulo to access the correct element in the array.
ctrl[counter % NUMELEMENTS(ctrl)]
This way there is never a risk that you try to access an element outside the ctrl array and it's easy to add an element to the ctrl array without having to adjust the rest of the code.
For the leds, we we loop through the led array that is part of the ctrl that is indicated by counter. For the motor, we don't need a loop.
// set the leds
for (byte ledCnt = 0; ledCnt < NUMELEMENTS(ctrl[counter % NUMELEMENTS(ctrl)].leds); ledCnt++)
{
Serial.print("Led# "); Serial.print(ctrl[counter % NUMELEMENTS(ctrl)].leds[ledCnt].pin);
Serial.print(", PWM "); Serial.println(ctrl[counter % NUMELEMENTS(ctrl)].leds[ledCnt].value);
}
// set the motor
Serial.print("Motor# "); Serial.print(ctrl[counter % NUMELEMENTS(ctrl)].motor.pin);
Serial.print(", PWM "); Serial.print(ctrl[counter % NUMELEMENTS(ctrl)].motor.value);
You can replace the serial prints by the actual controlling of the leds and the motor. The serial prints are useful for the debugging.
Full code for controlAirFlow
// information for a TLC pin/value pair
struct TLCPINVALUE
{
byte pin;
int value;
};
// airflow control information
struct AIRFLOWCONTROL
{
TLCPINVALUE leds[6];
TLCPINVALUE motor;
};
/*
control air flow
In:
counter
Returns:
true when finished, else false
*/
bool controlAirFlow(byte counter)
{
AIRFLOWCONTROL ctrl[] =
{
{{{2, 2048}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0},}, {16, 0}}, // leds 1..6, motor
{{{2, 0}, {3, 0}, {4, 2048}, {5, 0}, {6, 0}, {7, 0},}, {16, 512}},
{{{2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 2048}, {7, 0},}, {16, 2048}},
};
Serial.print("Air flow; counter modulo X: ");
Serial.println(counter % NUMELEMENTS(ctrl));
// set the leds
for (byte ledCnt = 0; ledCnt < NUMELEMENTS(ctrl[counter % NUMELEMENTS(ctrl)].leds); ledCnt++)
{
Serial.print("Led# "); Serial.print(ctrl[counter % NUMELEMENTS(ctrl)].leds[ledCnt].pin);
Serial.print(", PWM "); Serial.println(ctrl[counter % NUMELEMENTS(ctrl)].leds[ledCnt].value);
}
// set the motor
Serial.print("Motor# "); Serial.print(ctrl[counter % NUMELEMENTS(ctrl)].motor.pin);
Serial.print(", PWM "); Serial.print(ctrl[counter % NUMELEMENTS(ctrl)].motor.value);
return true;
}
One improvement can be made if necessary; you might not want to control the leds and motor again if the counter did not change.
I have not been able to test this but it does compile.
PS
Because the ctrl array is local to the function, you can name the arrays in the other two functions ctrl as well and there will be no conflict. If ctrl was global (basically defined before setup), you would have to give them different names.