Hey guys,
I'm working on a project that uses a python GUI to take user inputs that then control a bunch of outputs on the arduino. The outputs are time sensitive and time specific, so due to possible unreliableness/speed issues of the serial port I thought it necessary to store all python instructions on the arduino first before running.
Currently what I have going are 4 arrays, one for either PWM or normal 5V output in each of the B or D registers. My python instructions arrive and are interpreted in the following way:
PWM:
struct pwmData {
unsigned long startMillis;
unsigned long time_on;
unsigned long time_off;
int freq_hz;
byte pins;
};
union pcpwm {
pwmData data;
byte pcpwmData[15];
};
normal 5V output:
struct outData {
unsigned long time_to_trigger;
byte pins;
};
union pcout {
outData data;
byte pcoutData[5];
};
where I store the pins as bytes for port manipulation.
I then declare the 4 arrays:
pcpwm pwm_d07[
pcpwm pwm_b813[
pcout out_d07[
pcout out_b813[
My issue now is that depending on user input I may have different size requirements for each of the arrays. I'm extremely new to both python and arduino (and honestly coding in general), and I realize dynamic arrays are probably in over my head especially for a memory restricted system, so one option for me is to just declare the max size for each array (about 50 items for each array for 2kb memory? since the pcpwm arrays are 15 bytes each item and the pcout are 5 bytes each).
Still, I'm not too keen on this solution and was wondering if there's a better way to go about it. Thanks! 
What are the unions for? Why are you creating an array of unions?
I may have different size requirements for each of the arrays.
What does one element of the array control? Why do you need an array of them?
PaulS:
What are the unions for? Why are you creating an array of unions?
What does one element of the array control? Why do you need an array of them?
The unions convert serial input from python to the proper format defined in the struct. This was the best way I could find through google, is there a better option? The arrays resulting would be 2D arrays in the following form: e.g.
{{instructions1},{instructions2},...}
each element of the 2D array would, for example for the PWM struct, define a time interval (time on/time off) during which to PWM at a specific (freq_hz) for a certain set of pins in a specific register. (e.g. if I wanted to PWM pins 8 and 11 between 10 and 20 seconds at 2800 Hz, my python instructions would arrive then unpacked to something like:
{{0, 10000, 20000, 2800, B00001001},{etc},{etc2}...}
these would be accessed using array[q][w] in void loop().
Hopefully I was clear! It's a very convoluted first project. Thanks 
The unions convert serial input from python to the proper format defined in the struct.
Typically, one would create an instance of a union, and an array of structs. The one instance of the union would be used to collect the data that would then be stored in the array.
each element of the 2D array would
That's not a 2D array. That's a 1D array of structs.
It's a very convoluted first project.
Yes, it is.
You apparently know, in the python code, how many elements need to be in the array. Send that information, and dynamically allocate the array. Since the array never goes away, memory fragmentation won't be a problem.
Running out of memory might be.
I recommend that you allocate an array in the Arduino code that is big enough to hold the maximum amount of data and then your Python code can send any amount up to that size.
Having a large Arduino array partly unused will not affect the Arduino performance but dynamically allocating an array where there is no space for it will.
With a pre-allocated pace for the data the programming will probably be simpler.
...R