I have been rooting thru my code all day trying to find the source/cause of an exception(9).
I think I just can't see the forest for the trees and I will feel really stupid when someone points out what I'm doing wrong.
I have a sketch for a portable pixel tester, originally written for an Arduino Pro Mini (Chinese clone) and after making a whole bunch of changes I uploaded it to the Mini and it bricked it! The D13 light flashes rapidly and I can't upload, at least via USB. I have an ICSP programmer but haven't tried that yet.
Instead I grabbed a Wemos Di mini Pro and made a few minor changes to port it to that platform. I was planning to move to that anyway.
Setup() calls SetPattern()
SetPattern() calls DrawBasic()
DrawBasic() calls DrawFill()
DrawFill() appears (from the serial.print debugging messages) to complete, but does not appear to return execution to DrawBasic(). Thus, I believe something I am doing is messin' up the stack and causing the exception. But I've gone cross-eyed trying to figure out what.
Here is a stripped down version of the sketch:
#include <FastLED.h>
#include <EEPROM.h>
#define pixelPin D3 // Pin Outputing Data Signal to Pixels
#define COUNT_STRIP 170
#define COUNT_FENCE 336
const byte MAX_LEDs = COUNT_FENCE;
byte NUM_LEDs = COUNT_STRIP;
CRGB LEDs[MAX_LEDs];
#define PAT_OFF 0 // Starting number for patterns, Equal to PAT_NONE
#define PAT_BASICCHASE 1
#define patternCount 2; // number of available patterns, includes pattern #0 thru #26
// NEW Dimmer Colors, will hopefully save wear-n-tear on Blue and Green LEDs
// and reduce early failures (hopefully!?)
#define COLOR_BLACK_OFF 0
#define RGB_BLACK_OFF 0x000000L
#define COLOR_RED 9
#define RGB_RED 0x990000L
#define COLOR_GREEN 10
#define RGB_GREEN 0x00B200L
#define COLOR_BLUE 11
#define RGB_BLUE 0x0000B2L
#define COLOR_WHITE 15
#define RGB_WHITE 0X333B3BL
#define REGULAR_COLOR_COUNT COLOR_WHITE + 1
#define COLOR_COUNT COLOR_WHITE + 1
CRGB colorsRGB[REGULAR_COLOR_COUNT];
CRGB stepColors[32];
bool patternSet = false;
byte curPattern = PAT_BASICCHASE; // Selected output pattern
byte refrRate = 53;
//byte whosOn[32];
float speedMult[8] = {4.3, 3.5, 3.0, 2.5, 2.0, 1.6, 1.3, 1.0};
unsigned long curMillis; // current millis() at start of this pass
unsigned long lastMillis;
unsigned long nextStep;
unsigned long stepDelay = 1000;
byte curSpeed = 0; // Output Speed
int curStep = 0; // Current step of the pattern
byte subStep = 0;
byte stepSize = 16;
int fencStep = 0;
void setup() {
Serial.begin(921600);
Serial.println();
Serial.println();
Serial.println(F("Pixel Tester - Started!"));
//EEPROM.begin(16);
pinMode(pixelPin, OUTPUT);
FastLED.addLeds<WS2811, pixelPin, GRB>(LEDs, MAX_LEDs);
initColors();
curMillis = millis();
curPattern = PAT_BASICCHASE;
//curPattern = EEPROM.read(3);
curPattern %= patternCount;
Serial.println(F("Here 1..."));
delay(500);
SetNewPattern(curPattern);
Serial.println(F("Here 2..."));
delay(1000);
//SetNewSpeed(curSpeed);
Serial.println(F("Setup complete."));
delay(1000);
}
void loop() {
if (curMillis > nextStep)
{
//Step(curPattern);
FastLED.show();
nextStep += stepDelay;
} // end nextStep
poll();
} // END void loop()
void SetNewPattern(byte p)
{
byte newPattern = p % patternCount;
Serial.print(F("->Pattern "));
//Serial.println(PATTERN_NAMES[newPattern]);
Serial.println(newPattern);
delay(1000);
//EEPROM.write(3, curPattern);
//EEPROM.commit();
nextStep = curMillis + stepDelay;
switch (newPattern)
{
case PAT_OFF:
allOff();
break;
case PAT_BASICCHASE:
DrawBasic();
break;
} // end switch(curPattern)
delay(500);
Serial.println(F(" Show_LEDs"));
delay(1000);
FastLED.show();
Serial.println(F(" LEDs_Shown"));
delay(500);
patternSet = true;
curPattern = newPattern;
nextStep = curMillis + stepDelay;
} // end SetNewPattern
void allOff() {
for (int i=0; i < MAX_LEDs; i++)
{
LEDs[i] = colorsRGB[COLOR_BLACK_OFF];
}
FastLED.show();
} // void allOff()
void poll()
{
curMillis = millis();
Serial.println(F("Poll"));
lastMillis = curMillis;
}
CRGB hex2color(long hexVal)
{
CRGB ret;
long l;
ret.b = hexVal & 0x0000ff;
l = (hexVal & 0x00ff00) >> 8;
ret.g = l;
l = (hexVal & 0xff0000) >> 16;
ret.r = l;
return ret;
} // end hex2color
void initColors()
{
// Color # 0
colorsRGB[COLOR_BLACK_OFF] = hex2color(RGB_BLACK_OFF);
// Colors # 9 thru whatever
colorsRGB[COLOR_RED] = hex2color(RGB_RED);
colorsRGB[COLOR_GREEN] = hex2color(RGB_GREEN);
colorsRGB[COLOR_BLUE] = hex2color(RGB_BLUE);
colorsRGB[COLOR_WHITE] = hex2color(RGB_WHITE);
for (byte c = 0; c < 30; c++)
{
stepColors[c] = colorsRGB[COLOR_BLACK_OFF];
}
} // End void InitColors()
void DrawBasic()
{
Serial.println(F("--->DrawBasic Started"));
delay(1000);
NUM_LEDs = COUNT_STRIP;
stepColors[0] = colorsRGB[COLOR_WHITE];
stepColors[4] = colorsRGB[COLOR_RED];
stepColors[8] = colorsRGB[COLOR_GREEN];
stepColors[12] = colorsRGB[COLOR_BLUE];
stepColors[16] = colorsRGB[COLOR_WHITE];
stepColors[20] = colorsRGB[COLOR_BLACK_OFF];
for (byte m=0; m< 6; m++)
{
byte o = m * 4;
for (byte n = 1; n < 4; n++)
{
byte p = o + n;
stepColors[p] = stepColors[o];
Serial.print(F(" Setting stepColor["));
Serial.print(p);
Serial.print(F("] to stepColor["));
Serial.print(o);
Serial.print("] =");
PrintColor(stepColors[p]);
Serial.println();
}
//delay(1000);
}
DrawFill(24);
Serial.println(F(" DrawBasic Finished"));
delay(500);
} // end DrawBasic()
void PrintColor(CRGB theColor)
{
Serial.print("#0x");
if (theColor.r < 16) Serial.print("0");
Serial.print(theColor.r, HEX);
if (theColor.g < 16) Serial.print("0");
Serial.print(theColor.g, HEX);
if (theColor.b < 16) Serial.print("0");
Serial.print(theColor.b, HEX);
}
void DrawFill(byte size)
{
Serial.print(F("----->DrawFill("));
Serial.print(size);
Serial.print(F(") SubStep:"));
Serial.println(subStep);
delay(1000);
subStep = 0;
stepSize = size;
for (int n = NUM_LEDs; n > 0; n--)
{
Serial.print(F(" LED["));
Serial.print(n-1);
Serial.print(F("] Color for subStep "));
Serial.print(subStep);
Serial.print(F(" Is "));
PrintColor(stepColors[subStep]);
Serial.println();
LEDs[n - 1] = stepColors[subStep];
subStep++;
subStep %= stepSize;
}
Serial.print(F(" Draw Filled"));
Serial.print(F(" SubStep:"));
Serial.println(subStep);
delay(500);
}