TL;DR I think I'm passing my array into a function wrongly, and thus the data thats read from it is not right causing it to possibly mangle Arduino memory.
Full code can be found >here<
After a bit of reading, I'm still a tad confused the best way to go about passing an array into a function and modifying its data within that function.
So far these 2 questions sort of helped, and thus allowed my code to compile; but after a bit of testing I'm having issues whereby the data I would be expecting to see is not being read back correctly when I'm within the function.
Array/pointer/reference confusion
Passing an array by reference in C?
The basic program...
It lights up 3 LED strips with a base colour purple (after an initial fade each light one by one), then makes a sort of colours trail effect (7 pixels long) trace along the strip, and loop back from the beginning again.
Video can be seen here of the effect https://www.youtube.com/watch?v=S8tVfFfsiqI
I'm going to do the same effect but I have since tried to re-factor my code so that its easier for everyone to adjust the parameters of the colours.
Original source code can be found here >Click to View< (feel free to copy/modify/use the code how ever you want, its for anyone to use really, all in good fun)
What I'm trying to do now...
So the goal now is to re-factor the code from above so that its easier to set the Trail effect colour based on the user's preferences. Thus I want to define the colour of the trail elsewhere, and then have each instance of the trail just passed into function that handles updating it (this is done without using classes, just Structs and Arrays, as thats confusing for non programmery types which this code is aimed for)
//Setting up Trail effect length
#define TRAIL_LENGTH 7
typedef struct Color {
byte r;
byte g;
byte b;
};
typedef struct TrailPixel {
uint16_t position;
Color color;
};
//Function Prototypes
void trailEffectForward (Adafruit_NeoPixel &strip, struct TrailPixel (*trailArray)[TRAIL_LENGTH] );
//Make the arrays
TrailPixel trailLeft[TRAIL_LENGTH];
TrailPixel trailBottom[TRAIL_LENGTH];
TrailPixel trailRight[TRAIL_LENGTH];
So as you can see from the above, I create two Structs, and then make 3 arrays of those structs. I then populate the "position" value of each of the trail effects with...
for (int i = 0; i < TRAIL_LENGTH; i++) {
trailLeft[i].position = i + 5; //start just off the strip
trailBottom[i].position = 15 - i; //start off screen, but timed in a way so to look like the Left and Right trails converge onto the bottom at the same time
trailRight[i].position = i + 5; //start just off strip
}
Later on in the code, I call the function that I want to process the effect, and I hand off the details of the array to it. I want to have inside this function, to commands to update the pixel colour on the light strip and then update the position for next time.
BUT Things get mangled really fast to the point where my Arduino reboots every few seconds and colours aren't behaving as expected.
Here how I currently call the trail effect function...
trailEffectForward ( stripBottom , &trailBottom );
Once in there to try and figure out whats going on, I added some serial output to check the values.
void trailEffectForward(Adafruit_NeoPixel &strip, TrailPixel (*trailArray)[TRAIL_LENGTH]) {
Serial.println("---------------------");
Serial.println(trailArray[0]->position);
Serial.println(trailArray[1]->position);
Serial.println(trailArray[2]->position);
Serial.println(trailArray[3]->position);
Serial.println(trailArray[4]->position);
Serial.println(trailArray[5]->position);
Serial.println(trailArray[6]->position);
I would EXPECT if things worked according to plan, I would see the numbers
---------------------
15
14
13
12
11
10
9
But what I end up having is this :(
---------------------
15
5
5
43
1000
0
0
The full code that is currently in a state of Work In Progress can be found http://chiggenwingz.com/quads/ledcode/quad_leds_v0.2workinprogress.ino
Note: I've commented out a lot of the meat that applies colour to the pixels as I trying to narrow down what was going wrong. Basically I would be expecting the output as listed above to stop happening.
Once again feel free to use any of the code in your own projects :)