I haven't actually done any C/C++/Arduino programming in about 6 months because I'd been taking non-programming classes at school and I guess I've forgotten how to correctly count backwards and looks like I'm corrupting my RAM in my MCU on my Arduino and for some reason I can't wrap my head around the logic to get it right.
What I'm doing is taking a 5x7 LED matrix and just moving the dot.
To light a LED, the Column pin needs to go low, and the Row pin needs to go high.
I'm using a tri-state setup to keep from lighting other leds unintentionally, so any pin not being used gets set to a high impedance input.
The arrays at the top are just mapping rows to actual pins. To make it simple, I'm taking pin 1 on the LED matrix and wiring to pin 1 on the arduino and so on.
Everything works fine until I go to the backwards section. I think it's corrupting the memory because each time it executes, it goes slower and slower, so I think it's overwriting the delayms variable, and sometimes the dot just goes into a random spot.
int msdelay = 10;
int ColTotal = 5;
int RowTotal = 7;
int col[] = {
1,3,10,7,8}; //Pull Low
int row[] = {
12,11,2,9,4,5,6}; // Pull High
void setup() {
for (int i=0; i < 12; i++){
pinMode(i, INPUT);
}
}
void loop() {
// This section moves the dot from L to R then goes to next line and repeats - This works fine
for (int r=0; r < RowTotal; r++)
{
for (int c=0; c < ColTotal; c++)
{
pinMode(col[c], OUTPUT);
pinMode(row[r], OUTPUT);
digitalWrite (col[c], LOW);
digitalWrite (row[r], HIGH);
delay(msdelay);
digitalWrite (row[r], LOW);
delay(msdelay);
pinMode(col[c], INPUT);
pinMode(row[r], INPUT);
}
}
// This section moves the dot from Top to Bottom then moves Right, also works fine
for (int c=0; c < ColTotal; c++)
{
for (int r=0; r < RowTotal; r++)
{
pinMode(col[c], OUTPUT);
pinMode(row[r], OUTPUT);
digitalWrite (col[c], LOW);
digitalWrite (row[r], HIGH);
delay(msdelay);
digitalWrite (row[r], LOW);
delay(msdelay);
pinMode(col[c], INPUT);
pinMode(row[r], INPUT);
}
}
// Reverse Section
// This section is supposed to start from bottom right and move the dot up
// It starts but skips the top row, after the 2nd or 3rd time the entire program loops the
// loops get progressively slower and occasionally the dot starts moving around on its own
for (int c=ColTotal; c > 0; c--)
{
for (int r=RowTotal; r > 0; r--)
{
pinMode(col[c], OUTPUT);
pinMode(row[r], OUTPUT);
digitalWrite (col[c], LOW);
digitalWrite (row[r], HIGH);
delay(msdelay);
digitalWrite (row[r], LOW);
delay(msdelay);
pinMode(col[c], INPUT);
pinMode(row[r], INPUT);
}
}
}