thanks for the encouragement!
I was able to condense my sketch to something much smaller, I used a 2 dimensional array to hold the pattern for three of the patterns. The fourth, the one I call 'twopattern' is really giving me fits. this pattern shows two 'bright' leds starting from each end of the LED series of 12. The bright LEDs appear to migrate towards the opposite ends, and then pass through or bounce off (depending on how your eyes see it) and each bright LED is trailed by two dimmer LEDS. Writing the original code was not hard, I was even able to replace 11 lines of code with ONE! however, that was only for the 'right' side set of six LEDs, the left side has been very troublesome. I cannot seem to find the proper loop code to replace the eleven lines (that work) for the left set.
Any ideas and I could REALLY appreciate it. I have tried many things, unfortunately they have not worked. The commented out line:
// if (actualleft >= (i+5)) newleftPwm[actualleft - i*ldir] = two[5-i];
Does cycle the left bright LED back and forth, but I cannot for the life of me get the trailing LEDs to work.
// Larson scanner with PWM
// Modified by Roger Ayotte
// code uses 12 LEDs and 4 different patterns of light
// modeled after a 'larson' type of pattern.
// https://forum.arduino.cc/index.php?topic=675980
#define DEBUG_UART 1
const uint8_t ledPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // PWM pins 2 through 13
const uint8_t totalNoLeds = sizeof(ledPin);
const int buttonpin0 = 0; // the number of the pushbutton pin
int button0pushed = 0; // variable for reading the pushbutton status
int pat = 0;
int i;
int j;
// patternin[0] is positive pattern, [1] is negative, and [2] is wave
int patternij[3][12] = {
{255, 75, 25, 10, 5, 0, 0, 0, 0, 0, 0, 0},
{0, 5, 25, 75, 255, 255, 255, 255, 255, 255, 255, 255},
{0, 5, 25, 255, 25, 5, 0, 5, 25, 255, 25, 5},
};
void pattern()
{
if (button0pushed == 0) j=0;
else if (button0pushed == 1) j=1;
else if (button0pushed == 2) return;
else if (button0pushed == 3) return;
static uint8_t actual = totalNoLeds;
static uint32_t previousMillis = 0;
const uint16_t myIntervall = 100;
static int8_t dir = 1;
if (millis() - previousMillis >= myIntervall)
{
byte newPwm[] = {0,0,0,0,0,0,0,0,0,0,0,0};
if (dir == 1)
{
if (actual >= totalNoLeds - 1) dir = -1;
}
else if (actual <= 0) dir = 1;
actual += dir;
if (actual <= totalNoLeds - 1)
for (int i = 0; i< totalNoLeds - 1; i = i+1)
{
if (actual >= i) newPwm[actual -i] = patternij[j][i];
if (actual + i < totalNoLeds) newPwm[actual + i] = patternij[j][i];
}
for (byte i = 0; i < totalNoLeds; i++)
{
analogWrite(ledPin[i], newPwm[i]);
}
previousMillis = millis();
}
}
void wavepattern()
{
static uint8_t actual = totalNoLeds;
static uint32_t previousMillis = 0;
const uint16_t myIntervall = 50;
static int8_t dir = 1;
if (millis() - previousMillis >= myIntervall)
{
byte newPwm[] = {0,0,0,0,0,0,0,0,0,0,0,0};
if (actual <= totalNoLeds - 1) actual = actual +1;
else if (actual >= (totalNoLeds-1)) actual = 0;
if (actual <= totalNoLeds - 1)
int i;
for (int i = 0; i< totalNoLeds - 1; i = i+1)
{
if (actual >= i) newPwm[actual -i] = patternij[2][i];
if (actual + i < totalNoLeds) newPwm[actual + i] = patternij[2][i];
}
#if DEBUG_UART
// Serial.println(button0pushed);
// delay(100);
#endif
for (byte i = 0; i < totalNoLeds; i++)
{
analogWrite(ledPin[i], newPwm[i]);
}
// delay(100);
previousMillis = millis();
}
}
void twopattern()
{
static uint8_t actualright = (totalNoLeds/2);
static uint8_t actualleft = (totalNoLeds/2);
static uint32_t previousMillis = 0;
const uint16_t myIntervall = 100;
static int8_t rdir = 1;
static int8_t ldir = 1;
if (millis() - previousMillis >= myIntervall)
{
byte newrightPwm[(totalNoLeds/2)];
memset(newrightPwm, 0, (totalNoLeds/2));
if (rdir == 1)
{
if (actualright >= (totalNoLeds/2) - 1)
rdir = -1;
}
else if (actualright <= 0)
rdir = 1;
actualright += rdir;
byte newleftPwm[(totalNoLeds/2)];
memset(newleftPwm, 6, (totalNoLeds));
if (ldir == 1)
{
if (actualleft >= (totalNoLeds) - 1)
ldir = -1;
}
else if ((actualleft) <= 6)
ldir = 1;
actualleft += ldir;
int two[] = {0, 0, 0, 5, 25, 255};
int i;
for (int i = 0; i<= (totalNoLeds/2 - 1); i = i+1)
if (actualright >= i) newrightPwm[actualright -i*rdir] = two[5-i];
// if (actualleft >= (i+5)) newleftPwm[actualleft - i*ldir] = two[5-i];
if (actualleft >= 5) newleftPwm[actualleft -5] = two[0];
if (actualleft + 5 < totalNoLeds) newleftPwm[actualleft + 5] = two[0];
if (actualleft >= 4) newleftPwm[actualleft -4] = two[1];
if (actualleft + 4 < totalNoLeds) newleftPwm[actualleft + 4] = two[0];
if (actualleft >= 3) newleftPwm[actualleft -3] = two[2];
if (actualleft + 3 < totalNoLeds) newleftPwm[actualleft + 3*ldir] = two[0];
if (actualleft >= 2) newleftPwm[actualleft - 2*ldir] = two[3];
if (actualleft + 2 < totalNoLeds) newleftPwm[actualleft + 2*ldir] = two[0];
if (actualleft >= 1) newleftPwm[actualleft + ldir] = two[4];
if (actualleft + 1 < totalNoLeds) newleftPwm[actualleft + ldir] = two[0];
if (actualleft + 0 < totalNoLeds) newleftPwm[actualleft] = two[5];
for (int i = 0, j = 11; i < (totalNoLeds/2); i++, j--)
{
analogWrite(ledPin[j], newleftPwm[j]);
analogWrite(ledPin[i], newrightPwm[i]);
}
#if DEBUG_UART
// Serial.println(actualleft);
// Serial.println(actualright);
// Serial.println(ledPin[i]);
#endif
previousMillis = millis();
}
}
void setup()
{
#if DEBUG_UART
Serial.begin(115200);
#endif
for (uint8_t i = 0; i < totalNoLeds; i++)
{
pinMode(i, OUTPUT);
}
// initialize the pushbutton pin as an input:
pinMode(buttonpin0, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonpin0), PATISR, FALLING);
}
void loop()
{
// Serial.println(button0pushed);
if (button0pushed == 2) wavepattern();
if (button0pushed == 3) twopattern();
else pattern();
}
void PATISR()
{
button0pushed = button0pushed +1;
if (button0pushed == 4 ) button0pushed = 0;
}