So here's another thing I'm wrestling with. I want to be able to have an input that will allow the bargraph to top out, but then be held there until that input changes. So I added a switch input to pin 6 and edited the code. When the switch goes high, the bargraph immediately climbs to the top, as intended. The issue I'm running into is that when the switch input goes back to low, the bargraph just stays static. Looking at the code, the line
if (BG_counter >= BG_total || BG_counter < 1) BG_direction = -BG_direction;
should switch the direction of the bargraph, and it should resume its normal up-down operation, but it doesn't. What am I missing here? The entire code is listed below.
#include <LedControl.h>
//Pin Assignments
const int SW_INT = 6;
//Bargraph Pin Assignments
LedControl lc=LedControl(12,11,10,1);
byte BG_value[7] = {B00000001,B00000001,B00000001,B00000001,B00000001,B00000001,B00000001};
byte BG_full = B11111111;
byte BG_counter = 1;
const byte BG_top = 8; //total segments
byte BG_total = 50; //total segments used
byte BG_digit = 0;
byte BG_segment = 0;
char BG_direction = 1; //-1 = down, 1 = up
//Constants
const long delayBG1 = 100;
//Timing
unsigned long currentMillis = 0;
unsigned long previousBG1 = 0;
//Variable Modes
byte modeINT = 0;
void setup() {
//Pin Modes
pinMode(6,INPUT);
//Bargraph Initialization
lc.shutdown(0,false); // turn off power saving, enables display
lc.setIntensity(0,7); // sets brightness (0~15 possible values)
lc.clearDisplay(0); // clear screen
}
void loop()
{
currentMillis = millis();
modeINT = digitalRead(SW_INT);
if (modeINT == 0)
{
if (currentMillis - previousBG1 >= delayBG1)
{
BG_display();
BG_counter += BG_direction;
if (BG_counter >= BG_total || BG_counter < 1) BG_direction = -BG_direction;
previousBG1 = currentMillis;
}
}
else if (modeINT == 1)
{
if (currentMillis - previousBG1 >= delayBG1)
{
BG_display();
if (BG_counter <= BG_total)
{
BG_counter += abs(BG_direction);
previousBG1 = currentMillis;
}
}
}
}
void BG_display()
{
BG_digit = ((BG_counter) / BG_top); //determine digit
BG_segment = (BG_counter % BG_top); //determine segment
for (byte d = 0; d < 7; d++)
{
if (d < BG_digit)
BG_value[d] = BG_full;
else if (d > BG_digit)
BG_value[d] = 0;
else
BG_value[d] = (1 << (BG_segment)) - 1;
lc.setRow(0, d, BG_value[d]);
}
}