Screw it… you guys are smart so you can handle my bad variable names lol. Architect, you probably saw the below video before… it’s the Tron Easter Egg I wrote into a light/sound display I built for my brother in law. I’m just posting the video so you know what the heck I am trying to do. - YouTube
So, the thread below starts up when I start the Tron lightshow. It serves 3 functions… during normal light modes, _raining is false, and _beatPattern = 0. This just provides some slight variation of the intensity to the RGB LEDs. Then when _raining is true, the variation slows down and intensity variation increases. Lastly, there are 3 intensity patterns to go with different music beats.
I was originally going to vary the green level, which is why a bunch of variables are named things like _greenOffset. I ended up applying that change as a 0-1 multiplier for the intensity though.
This thread is always running… but the problem seems to occur during a long period where it is the only active thread and raining = true and pattern = 0 . At this point it is waiting for the musicshield.position to reach a certain number, then it moves on.
Maybe 1 out of 3 times everything locks up, the music shield stops, and watchdog resets after 10 seconds. Only happens when I’m not in debug… I had it running connected to my computer and it went all night with no crash.
I just noticed that I am declaring my Random variable inside the while loop… so I’m guessing that is causing the garbage collector to run a lot? I’ll move that outside the While loop when I get home and give it a try. I’m more of a hardware guy who is trying to learn software… I understand OOP a lot better since I started this code and I really need to go back through and improve the entire program, but for now I just want to fix this crash.
static double _greenOffset = 1;
static double _greenOffset2 = 1;
static double _greenVariationFactor = 2.25;
static bool _raining = false;
static long _beatSpacing = 0;
static int _beatPattern = 0;
static int _delayBaseline = 10;
static double _greenMin = .7;
static int[] beatArray = new int[16] { 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 };
public static void GreenRandomizer()
{
long beatStart = 0;
uint beatLength = 0;
while (_ChannelFiveRunning == true)
{
if (_beatPattern == 0)
{
Random G = new Random();
_greenOffset = ((double)1 - ((G.Next(5) * _greenVariationFactor) / 100));
if (_raining == true)
{
_delayBaseline = 25;
SetFloodLEDs();
SetLanternLEDs();
_greenVariationFactor = 6;
}
else
{
_greenVariationFactor = 2.25;
_delayBaseline = 10;
}
Thread.Sleep(G.Next(20) + _delayBaseline);
_greenOffset2 = _greenOffset;
}
else
{
if (_beatPattern == 1)
{
beatStart = MusicShield.positionRead;
beatLength = (uint)_beatSpacing / 6;
while (MusicShield.positionRead < beatStart + beatLength)
{
_greenOffset = 1;
_greenOffset2 = 1;
Thread.Sleep(10);
}
while (MusicShield.positionRead < beatStart + _beatSpacing)
{
_greenOffset = _greenMin;
_greenOffset2 = _greenMin;
Thread.Sleep(10);
}
}
if (_beatPattern == 2)
{
beatLength = (uint)_beatSpacing / 8;
for (int i = 1; i < 4 && _beatPattern == 2; i += 2)
{
beatStart = MusicShield.positionRead;
while (MusicShield.positionRead < beatStart + (beatLength * i) && _beatPattern == 2)
{ //on portion
_greenOffset = 1;
_greenOffset2 = 1;
Thread.Sleep(10);
}
while (MusicShield.positionRead < beatStart + beatLength + (beatLength * i) && _beatPattern == 2)
{ //off portion
_greenOffset = _greenMin;
_greenOffset2 = _greenMin;
Thread.Sleep(10);
}
}
while (MusicShield.positionRead < _beatSpacing + beatStart && _beatPattern == 2)
{ //rest
Thread.Sleep(10);
}
}
if (_beatPattern == 3)
{
int b = 0;
long pattern3pos = 0;
pattern3pos = MusicShield.positionRead;
beatLength = (uint)_beatSpacing / 4;
for (int i = 0; i < 16; i ++)
{
pattern3pos += beatLength;
if (i == 0)
Debug.Print("111111111111111111111111111111");
else
Debug.Print((i+1).ToString());
while (MusicShield.positionRead < pattern3pos + beatLength)
{
if (beatArray[i] == 1)
{
_greenOffset = 1;
_greenOffset2 = 1;
}
else
{
_greenOffset = _greenMin;
_greenOffset2 = _greenMin;
}
Thread.Sleep(5);
}
}
}
}
}
_greenOffset = 1;
_greenOffset2 = 1;
}