okay, well, mostly complete success! I have been able to pass an array to the function using a template function. The arrays are PWM values to set LEDs and light in patterns (aka larson etc.).
Then I went ahead and used switch case to switch between five patterns every five seconds and then restart the first pattern.
Trouble happens after about two or so passes through the cycle, the LEDs go crazy! Then apparently come back to 'normal'.
I used a few techniques to see where things went wrong. Evidently the linearIndex (converted from array) would go beyond the end point and continue to increment. To help fix, I added a 10ms buffer to the if millis() - looptime and it helped, but did not completely remove the occasional loss of synchrony.
If anyone can help find out why this happens randomly, well not completely randomly, only one the forth or fifth 'case' and usually when reset to first case, usually resets to proper pattern.
Here is code. I am calling this about 95% succedssful, and really appreciate teh help.
// Larson scanner with PWM
#define DEBUG_UART 0
const uint8_t leds[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; // PWM pins 2 through 13
int totalNoLeds = sizeof(leds);
int pulse = 0;
static uint32_t loopMillis = 0;
void display_pattern(const int *pattern, size_t rows, size_t cols)
{
static uint32_t previousMillis = 0;
const uint16_t myInterval = 50;
static uint8_t indexI = 0;
static uint8_t indexJ = 0;
for (indexI = 0; indexI <= cols; indexI++)
{
uint16_t linearIndex = indexJ * cols + indexI;
analogWrite(leds[indexI], pattern[linearIndex]);
#if DEBUG_UART
Serial.println(linearIndex);
//Serial.println(millis() - loopMillis);
//Serial.println(patternd[indexI]);
delay(10);
#endif
}
if (millis() - previousMillis >= myInterval)
{
indexJ++;
// if indexJ has reached max value
if (indexJ == rows)
{
// reset indexJ
indexJ = 0;
}
previousMillis = millis();
}
}
int pattern1[17][12] = {
{255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255},
{75, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 75},
{50, 75, 255, 0, 0, 0, 0, 0, 0, 255, 75, 50},
{25, 50, 75, 255, 0, 0, 0, 0, 255, 75, 50, 25},
{10, 25, 50, 75, 255, 0, 0, 255, 75, 50, 25, 10},
{5, 10, 25, 50, 75, 255, 255, 75, 50, 25, 10, 5},
{0, 5, 10, 25, 50, 255, 255, 50, 25, 10, 5, 0},
{0, 0, 5, 10, 255, 75, 75, 255, 10, 5, 0, 0},
{0, 0, 0, 255, 75, 50, 50, 75, 255, 0, 0, 0},
{0, 0, 255, 75, 50, 25, 25, 50, 75, 255, 0, 0},
{0, 255, 75, 50, 25, 10, 10, 25, 50, 75, 255, 0},
{255, 75, 50, 25, 10, 5, 5, 10, 25, 50, 75, 255},
{75, 50, 25, 10, 5, 0, 0, 5, 10, 25, 50, 75},
{50, 25, 10, 5, 0, 0, 0, 0, 5, 10, 25, 50},
{25, 10, 5, 0, 0, 0, 0, 0, 0, 5, 10, 25},
{10, 5, 0, 0, 0, 0, 0, 0, 0, 0, 5, 10},
{5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
};
int pattern2[17][12] = {
{255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{75, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{50, 75, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{25, 50, 75, 255, 0, 0, 0, 0, 0, 0, 0, 0},
{10, 25, 50, 75, 255, 0, 0, 0, 0, 0, 0, 0},
{5, 10, 25, 50, 75, 255, 0, 0, 0, 0, 0, 0},
{0, 5, 10, 25, 50, 75, 255, 0, 0, 0, 0, 0},
{0, 0, 5, 10, 25, 50, 75, 255, 10, 5, 0, 0},
{0, 0, 0, 5, 10, 25, 50, 75, 255, 0, 0, 0},
{0, 0, 0, 0, 5, 10, 25, 50, 75, 255, 0, 0},
{0, 0, 0, 0, 0, 5, 10, 25, 50, 75, 255, 0},
{0, 0, 0, 0, 0, 0, 5, 10, 25, 50, 75, 255},
{0, 0, 0, 0, 0, 0, 0, 5, 10, 25, 50, 75},
{0, 0, 0, 0, 0, 0, 0, 0, 5, 10, 25, 50},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 10, 25},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 10},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
};
int pattern3[16][12] = {
{16, 32, 64, 128, 255, 128, 64, 32, 16, 8, 4, 2},
{8, 16, 32, 64, 128, 255, 128, 64, 32, 16, 8, 4},
{4, 8, 16, 32, 64, 128, 255, 128, 64, 32, 16, 8},
{2, 4, 8, 16, 32, 64, 128, 255, 128, 64, 32, 16},
{0, 2, 4, 8, 16, 32, 64, 128, 255, 128, 64, 32},
{2, 0, 2, 4, 8, 16, 32, 64, 128, 255, 128, 64},
{4, 2, 0, 2, 4, 8, 16, 32, 64, 128, 255, 128},
{8, 4, 2, 0, 2, 4, 8, 16, 32, 64, 128, 255},
{16, 8, 4, 2, 0, 2, 4, 8, 16, 32, 64, 128},
{32, 16, 8, 4, 2, 0, 2, 4, 8, 16, 32, 64},
{64, 32, 16, 8, 4, 2, 0, 2, 4, 8, 16, 32},
{128, 64, 32, 16, 8, 4, 2, 0, 2, 4, 8, 16},
{255, 128, 64, 32, 16, 8, 4, 2, 0, 2, 4, 8},
{128, 255, 128, 64, 32, 16, 8, 4, 2, 0, 2, 4},
{64, 128, 255, 128, 64, 32, 16, 8, 4, 2, 0, 2},
{32, 64, 128, 255, 128, 64, 32, 16, 8, 4, 2, 0},
};
int pattern4[10][12] = {
{2, 0, 2, 8, 32, 128, 255, 128, 32, 8, 2, 0},
{8, 2, 0, 2, 8, 32, 128, 255, 128, 32, 8, 2},
{32, 8, 2, 0, 2, 8, 32, 128, 255, 128, 32, 8},
{128, 32, 8, 2, 0, 2, 8, 32, 128, 255, 128, 32},
{255, 128, 32, 8, 2, 0, 2, 8, 32, 128, 255, 128},
{128, 255, 128, 32, 8, 2, 0, 2, 8, 32, 128, 255},
{32, 128, 255, 128, 32, 8, 2, 0, 2, 8, 32, 128},
{8, 32, 128, 255, 128, 32, 8, 2, 0, 2, 8, 32},
{2, 8, 32, 128, 255, 128, 32, 8, 2, 0, 2, 8},
{0, 2, 8, 32, 128, 255, 128, 32, 8, 2, 0, 2},
};
int pattern5[16][12] = {
{16, 32, 64, 128, 255, 128, 64, 32, 16, 8, 4, 2},
{8, 16, 32, 64, 128, 255, 128, 64, 32, 16, 8, 4},
{4, 8, 16, 32, 64, 128, 255, 128, 64, 32, 16, 8},
{2, 4, 8, 16, 32, 64, 128, 255, 128, 64, 32, 16},
{0, 2, 4, 8, 16, 32, 64, 128, 255, 128, 64, 32},
{2, 0, 2, 4, 8, 16, 32, 64, 128, 255, 128, 64},
{4, 2, 0, 2, 4, 8, 16, 32, 64, 128, 255, 128},
{8, 4, 2, 0, 2, 4, 8, 16, 32, 64, 128, 255},
{16, 8, 4, 2, 0, 2, 4, 8, 16, 32, 64, 128},
{32, 16, 8, 4, 2, 0, 2, 4, 8, 16, 32, 64},
{64, 32, 16, 8, 4, 2, 0, 2, 4, 8, 16, 32},
{128, 64, 32, 16, 8, 4, 2, 0, 2, 4, 8, 16},
{255, 128, 64, 32, 16, 8, 4, 2, 0, 2, 4, 8},
{128, 255, 128, 64, 32, 16, 8, 4, 2, 0, 2, 4},
{64, 128, 255, 128, 64, 32, 16, 8, 4, 2, 0, 2},
{32, 64, 128, 255, 128, 64, 32, 16, 8, 4, 2, 0},
};
void setup()
{
for (uint8_t i = 0; i < totalNoLeds; i++)
{
pinMode(leds[i], OUTPUT);
}
}
template <size_t rows, size_t cols> void display_pattern(const int (&pattern)[rows][cols])
{
display_pattern(&pattern[0][0], rows, cols);
}
void loop()
{
if (millis() - loopMillis < 5000)
{pulse = 1;}
if ((millis() - loopMillis) >= 5010 && (millis() - loopMillis) <10000)
{pulse = 2;}
if (millis() - loopMillis >= 10010 && (millis() - loopMillis) < 15000)
{pulse = 3;}
if (millis() - loopMillis >= 15010 && (millis() - loopMillis) < 20000)
{pulse = 4;}
if (millis() - loopMillis >= 20010 && (millis() - loopMillis) < 25000)
{pulse = 5;}
switch (pulse) {
case 1:
display_pattern(pattern1);
return;
case 2:
display_pattern(pattern2);
return;
case 3:
display_pattern(pattern3);
return;
case 4:
display_pattern(pattern4);
return;
case 5:
display_pattern(pattern5);
if (millis() - loopMillis > 25010)
loopMillis = millis();
return;
}
}