After running for a while, my sketch starts displaying symptoms of memory corruption. It is a simple sketch, using port manipulation, to control a multiplexed array of LEDs.
It will run correctly for a while, then appear to freeze, with a semi-random LED illuminated. However, the sketch is running, as I have debug code which displays the stts via an array LEDs[]. This LED will remain constant unless the sketch is changed. It is printing this array that shows corruption.
In inserted code to monitor and detect an out of bounds condition of my array indices, but it did not show anything. Can you take a look, and see if you can detetc anything that maight cause memory corruption.
// Column = PORTB = Pins 8-10
// Rows = PORTD = Pins 4-6
#define halt while(-1){}
#define notDone true
#define PatternDataFile "PatternsData.h"
#include PatternDataFile
#define maxColumnDelay 20
#define maxPatternRepeat 10
const String LEDs[8] {
"---",
"--X",
"-X-",
"-XX",
"X--",
"X-X",
"XX-",
"XXX"
};
// end global variable definitions
//********************************************************************************
void setup() {
// initialize console
Serial.begin(9600);
// initialize pseudo-random generator
randomSeed(analogRead(A0));
// initialize control pins; pattern
DDRD = DDRD | B01110000; // sets pins 4 to 6 as output
PORTD = PORTD & B10001111; // sets pins 4 to 6 to low
// initialize control pins; columns
DDRB = DDRB | B00000111; // sets pins 8 to 10 as output
PORTB = PORTB | 7; // sets pins 8 - 10 to high
}
// end setup()
//********************************************************************************
void loop() {
int mySequence; // variable containing the sequence currently displaying
int nextSequence; // variable containing the next requested sequence
int myPattern; // temporary variable containing the pattern for a given column
int myCell = 0; // variable containing the cell index
// in simulation, local definition failed!!!
while (notDone) {
// Choose sequence
mySequence = random(1, (maxSequence + 1));
// mySequence = 2;
for (myCell = 0; myCell < tableSequences[mySequence]; myCell++) {
for (int repeatPattern = 0; repeatPattern < maxPatternRepeat; repeatPattern++) {
for (int myColumn = 0; myColumn < 3; myColumn++) {
PORTB = PORTB | 7; // disable column output
myPattern = (pattern_x[mySequence][myCell][myColumn]);
// set pattern
PORTD = (
(PORTD & B10001111)
|
(myPattern << 4)
);
// illuminate LEDs
PORTB = (
(PORTB & B11111000)
|
(7 - (1 << myColumn))
);
displayLEDs(myPattern);
// Debug
if (mySequence > maxSequence |
myCell > maxCells |
myColumn > maxColumn ) {
Serial.print(mySequence);
Serial.print("\t");
Serial.print(myCell);
Serial.print("\t");
Serial.print(myColumn);
Serial.print("\t");
halt;
}
// Debug
} // end myColumn
} // end repeatPattern
// delay(maxColumnDelay);
} // end myCell
nextSequence = getSequence();
if ((nextSequence >= 0) & (nextSequence < maxSequence)) {
mySequence = nextSequence;
break;
}
if (nextSequence == 99) halt;
} // end 'while(notDone)' loop
} // end loop()
//********************************************************************************
void displayLEDs(int thePattern) {
static int counter = 0;
Serial.print(LEDs[thePattern]);
Serial.print("\t");
Serial.println(thePattern);
if (counter == 2) {
counter = 0;
Serial.println();
} else {
counter++;
}
} // end displayLEDs()
//********************************************************************************
int getSequence() {
static String inputStream = "";
int returnValue = 0;
int inputChar = 0;
while (Serial.available() > 0) { // read serial input
inputChar = Serial.read();
if (isDigit(inputChar)) { // add digit to string
inputStream += (char)inputChar;
} // end add digit to string
if (inputChar == '\n') { // on newline, return value of input digits
returnValue = inputStream.toInt();
inputStream = ""; // clear the string for new input:
return returnValue;
} // end return input stream value
} // end read serial input
} // end getSequence()
//djl
// PatternsData_v8
const int maxSequence = 6;
const int maxCells = 18;
const int maxColumn = 3;
// table of the lengths of the defined sequences
// each number represents the number of steps/cells in a sequence
int tableSequences[maxSequence] = { 2, 4, 4, 4, 8, 18 };
// table of the port values for each column for each pattern
// "}, {" separates SEQUENCE definitions
// "{a, b, c}" defines a pattern for each CELL
// "a-c" are the individual patterns for each COLUMN within a cell
const int pattern_x[maxSequence][maxCells][maxColumn] = {
{
// 0 - star
{2, 7, 2},
{5, 2, 5}
}, {
// 1 - horizontal bar
{1, 1, 1},
{2, 2, 2},
{4, 4, 4},
{2, 2, 2}
}, {
// 2 - vertical bar
{7, 0, 0},
{0, 7, 0},
{0, 0, 7},
{0, 7, 0}
}, {
// 3 - single
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
}, {
// 4 - round
{0, 4, 0},
{4, 0, 0},
{2, 0, 0},
{1, 0, 0},
{0, 1, 0},
{0, 0, 1},
{0, 0, 2},
{0, 0, 4}
}, {
// 5 - box
{0, 0, 0},
{1, 0, 0},
{3, 0, 0},
{7, 0, 0},
{7, 1, 0},
{7, 3, 0},
{7, 7, 0},
{7, 7, 1},
{7, 7, 3},
{7, 7, 7},
{3, 7, 7},
{1, 7, 7},
{0, 7, 7},
{0, 3, 7},
{0, 1, 7},
{0, 0, 7},
{0, 0, 3},
{0, 0, 1}
}
};
LED_Patterns_v8.ino (3.84 KB)
PatternsData.h (1.25 KB)