Okay, not to necropost, but I dusted off (literally) this project the other day, and completely forgot what my thought process was.
The goal is to run an array of 50 LEDs from a MAX7219. The physical array is 1x50, but broken into 7 groups (DIG0-DIG6) of 8 segments (SEGDP-SEGG). It should illuminate from 1 to 50 and back down.
My physical circuit functions as needed, but after I started to debug my code and observing the serial monitor, I'm getting odd results I can't decipher. It's so close to working, I'm just wholly frustrated. The only thing that works at intended is the switch from 1-50 to 50-1.
I'm fairly certain there's a much more efficient way to code this, so I'm open to suggestions.
#include <binary.h>
#include <LedControl.h>
//Bargraph Pin Assignments
LedControl lc=LedControl(12,11,10,1);
int BG_value0 = B00000001; //bottom segment DIG 0
int BG_value1 = B00000001; //bottom segment DIG 1
int BG_value2 = B00000001; //bottom segment DIG 2
int BG_value3 = B00000001; //bottom segment DIG 3
int BG_value4 = B00000001; //bottom segment DIG 4
int BG_value5 = B00000001; //bottom segment DIG 5
int BG_value6 = B00000001; //bottom segment DIG 6
int BG_full = B11111111;
int BG_counter = 1;
int const BG_top = 8; //total segments
int const BG_total = 50; //total segments used
int BG_digit = 0;
int BG_direction = 1; //0 = down, 1 = up
//Timing
unsigned long currentMillis = 0;
unsigned long previousBG1 = 0;
const long delayBG1 = 200;
void setup() {
//Bargraph Initialization
lc.shutdown(0,false); // turn off power saving, enables display
lc.setIntensity(0,12); // sets brightness (0~15 possible values)
lc.clearDisplay(0); // clear screen
Serial.begin(9600);
}
void loop()
{
currentMillis = millis();
if(currentMillis - previousBG1 >= delayBG1)
{
Serial.println(BG_digit);
Serial.println(BG_value0, BIN);
Serial.println(BG_value1, BIN);
Serial.println(BG_value2, BIN);
Serial.println(BG_value3, BIN);
Serial.println(BG_value4, BIN);
Serial.println(BG_value5, BIN);
Serial.println(BG_value6, BIN);
Serial.print("--------");
Serial.println(BG_counter);
if(BG_counter < BG_total && BG_direction == 1)
{
BG_up();
}
else if(BG_direction == 0)
{
BG_down();
}
if (BG_counter == BG_total)
{
BG_direction = 0;
}
if (BG_counter == 1)
{
BG_direction = 1;
}
previousBG1 = currentMillis;
}
}
void BG_up()
{
BG_digit = (BG_counter-1) / BG_top; //determine digit
if (BG_digit == 0 && BG_counter <= 8) //digit 0
{
lc.setRow(0,BG_digit,BG_value0);
BG_value0 = ((BG_value0 << 1)|1);
BG_counter++;
}
if (BG_digit == 1 && BG_counter <= 16) //digit 0+1
{
lc.setRow(0,0,BG_full);
lc.setRow(0,BG_digit,BG_value1);
BG_value1 = (BG_value1 << 1)|1;
BG_counter++;
}
if (BG_digit == 2 && BG_counter <= 24) //digit 0+1+2
{
lc.setRow(0,0,BG_full);
lc.setRow(0,1,BG_full);
lc.setRow(0,BG_digit,BG_value2);
BG_value2 = (BG_value2 << 1)|1;
BG_counter++;
}
if (BG_digit == 3 && BG_counter <= 32) //digit 0+1+2+3
{
lc.setRow(0,0,BG_full);
lc.setRow(0,1,BG_full);
lc.setRow(0,2,BG_full);
lc.setRow(0,BG_digit,BG_value3);
BG_value3 = (BG_value3 << 1)|1;
BG_counter++;
}
if (BG_digit == 4 && BG_counter <= 40) //digit 0+1+2+3+4
{
lc.setRow(0,0,BG_full);
lc.setRow(0,1,BG_full);
lc.setRow(0,2,BG_full);
lc.setRow(0,3,BG_full);
lc.setRow(0,BG_digit,BG_value4);
BG_value4 = (BG_value4 << 1)|1;
BG_counter++;
}
if (BG_digit == 5 && BG_counter <= 48) //digit 0+1+2+3+4+5
{
lc.setRow(0,0,BG_full);
lc.setRow(0,1,BG_full);
lc.setRow(0,2,BG_full);
lc.setRow(0,3,BG_full);
lc.setRow(0,4,BG_full);
lc.setRow(0,BG_digit,BG_value5);
BG_value5 = (BG_value5 << 1)|1;
BG_counter++;
}
if (BG_digit == 6 && BG_counter <= BG_total) //digit 0+1+2+3+4+5+6
{
lc.setRow(0,0,BG_full);
lc.setRow(0,1,BG_full);
lc.setRow(0,2,BG_full);
lc.setRow(0,3,BG_full);
lc.setRow(0,4,BG_full);
lc.setRow(0,5,BG_full);
lc.setRow(0,BG_digit,BG_value6);
BG_value6 = (BG_value6 << 1)|1;
BG_counter++;
}
}
void BG_down()
{
BG_digit = (BG_counter - 1) / BG_top; //determine digit
if (BG_digit == 0 && BG_counter <= 8) //digit 0
{
lc.setRow(0,BG_digit,BG_value0);
BG_value0 = (BG_value0 >> 1)|1;
BG_counter--;
}
if (BG_digit == 1 && BG_counter <= 16) //digit 0+1
{
lc.setRow(0,0,BG_full);
lc.setRow(0,BG_digit,BG_value1);
BG_value1 = (BG_value1 >> 1)|1;
BG_counter--;
}
if (BG_digit == 2 && BG_counter <= 24) //digit 0+1+2
{
lc.setRow(0,0,BG_full);
lc.setRow(0,1,BG_full);
lc.setRow(0,BG_digit,BG_value2);
BG_value2 = (BG_value2 >> 1)|1;
BG_counter--;
}
if (BG_digit == 3 && BG_counter <= 32) //digit 0+1+2+3
{
lc.setRow(0,0,BG_full);
lc.setRow(0,1,BG_full);
lc.setRow(0,2,BG_full);
lc.setRow(0,BG_digit,BG_value3);
BG_value3 = (BG_value3 >> 1)|1;
BG_counter--;
}
if (BG_digit == 4 && BG_counter <= 40) //digit 0+1+2+3+4
{
lc.setRow(0,0,BG_full);
lc.setRow(0,1,BG_full);
lc.setRow(0,2,BG_full);
lc.setRow(0,3,BG_full);
lc.setRow(0,BG_digit,BG_value4);
BG_value4 = (BG_value4 >> 1)|1;
BG_counter--;
}
if (BG_digit == 5 && BG_counter <= 48) //digit 0+1+2+3+4+5
{
lc.setRow(0,0,BG_full);
lc.setRow(0,1,BG_full);
lc.setRow(0,2,BG_full);
lc.setRow(0,3,BG_full);
lc.setRow(0,4,BG_full);
lc.setRow(0,BG_digit,BG_value5);
BG_value5 = (BG_value5 >> 1)|1;
BG_counter--;
}
if (BG_digit == 6 && BG_counter <= BG_total) //digit 0+1+2+3+4+5+6
{
lc.setRow(0,0,BG_full);
lc.setRow(0,1,BG_full);
lc.setRow(0,2,BG_full);
lc.setRow(0,3,BG_full);
lc.setRow(0,4,BG_full);
lc.setRow(0,5,BG_full);
lc.setRow(0,BG_digit,BG_value6);
BG_value6 = (BG_value6 >> 1)|1;
BG_counter--;
}
}
I should add that I'm not dead set on moving bits right to left. In fact, left to right may work more easily, having thought about it.