Go Down

Topic: (SOLVED) Running a multi-stage bargraph via MAX7219 (Read 938 times) previous topic - next topic

Prop-Forge

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
Code: [Select]
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.

Code: [Select]
#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]);
  }
}


PaulRB

Maybe pin 6 is floating. Did you add a pull-up or -down resistor?

Prop-Forge

Maybe pin 6 is floating. Did you add a pull-up or -down resistor?
It has a 10k pulldown resistor, yes.

Grumpy_Mike

#18
Jul 13, 2019, 11:40 pm Last Edit: Jul 13, 2019, 11:45 pm by Grumpy_Mike
Quote
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.
Yes it does.
Insert
Code: [Select]
Serial.print(BG_counter); Serial.print(" "); Serial.println((int)BG_direction);
 
After that line, and you will see it count correctly both up and down. Use Serial.begin in the setup function to set the baud rate. Note the cast in the printing of the BG_direction variable is just to make it show up as 1 and -1, otherwise it shows up as a null ( not printable ) and an upside down question mark 0xFF, but it still counts correctly.

Using Serial.print is a good way of seeing your program variables and program flow.

This is what I see :-
Code: [Select]
42 1
43 1
44 1
45 1
46 1
47 1
48 1
49 1
50 -1
49 -1
48 -1
47 -1
46 -1
45 -1
44 -1

Prop-Forge

Thanks, Mike. I screwed around a bit based on what my Serial was telling me, and tweaked some bounds so it's working properly now. Until I tweak something else and give myself more issues, that is.

Go Up