What goes up should come down!

Evening!

I've got a 28 segment Bargraph hooked up with a MAX7219. It's all wired up fine, and I've tested the wiring with other code and every LED can be lit in sequence.

What I'm trying to do is, have it count up in sequence and then count back down again. However it will not ...

1.Go past the 24th LED in the sequence. Which is the ROW 4, COL 6.
I can see form the serial print the sequence is looping through the UP loop 7th time.

  1. Reset the loop and move onto the down loop
    I can see in the serial monitor that the state of Direction moves to false after the 7th loop. But it keeps looping through UP, rather than down.

  2. The sequence only lights the first time through the loop. In the serial monitor i can see the loop is still counting UP 1-7. No lights show until i reset the Arduino.

I know it MUST be something silly to work 6 out of 7 times through the loop. :o

Thanks

Roxanne

#include "LedControl.h"       //MAX7219 library for the bar graph
#define UP true
#define DOWN false
boolean direction = UP;
LedControl lc = LedControl(32, 36, 34, 1);
unsigned long previousMillisbar = 0;    //used for bar graph animation timing
int interval1 = 50;     //Timing for the bar graph


void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  lc.shutdown(0, false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0, 8);
  /* and clear the display */
  lc.clearDisplay(0);
}

void loop() {
  UPDOWN();
}

void UPDOWN() {
  // put your main code here, to run repeatedly:
  if (direction = UP)   //UP means filling the bar graph
  {
    int col = 0;          //Start at the first LED
    int row = 0;          //Start at the first LED
    while (col < 7)     //There are only seven columns, 0 - 6
    {
      while (row < 4)       //There are only four rows, 0 - 3
      {
        unsigned long currentMillisbar = millis();
        //Get current program time
        lc.setLed(0, row, col, true);                 //Set the LED at row and col to on
        if (currentMillisbar - previousMillisbar > interval1) //Check to see if the interval time has passed
        { //If enough time has passed:
          previousMillisbar = currentMillisbar;           //Store current program time
          row++;        //Increase row number by one
        }
      }     //row has passed 3, exit while loop
      row = 0;    //Reset row to zero
      col++; //Increase column number by one
      Serial.print("UP");
       Serial.print(col);
    }       //col value has passed 6, bargraph is full
    direction = DOWN; //updown changes from positive to negative value
    Serial.print(direction);
  }

  if (direction = DOWN)    //DOWN means emptying the bar graph
  {
    int col = 7;      //Start at last LED
    int row = 4;      //Start at last LED
    while (col > 0)   //This prevents this loop from doing anything with the first column
    { //The first four LEDs will always be lit.  Change this to -1 to empty completely
      while (row > -1) //Count the rows down to zero
      {
        unsigned long currentMillisbar = millis();      //Get current program time
        lc.setLed(0, row, col, false);                  //Set the LED at row and col to off
        if (currentMillisbar - previousMillisbar > interval1) //Check to see if the interval time has passed
        { //If enough time has passed:
          previousMillisbar = currentMillisbar;  //Store current program time
          row--;                              //Decrease row number by one
        }                                      
      }
      row = 4;              //Reset row to 4
      col--;                //Decrease column number by one
         Serial.print("down");
          Serial.print(col);
    }
    direction =  UP;     //updown changes from negative to positive value
  }                   //updown no longer negative, exit if loop
}
if (direction = UP)

Should that not be

if (direction == UP)

Same with the down.

Anyway the direction variable seems totally superfluous. The first if statement will be true when you have corrected it, then you go through all that and set direction to DOWN, come out of the first if and go straight into the second. Basically it dosn't change anything the way you have written it.

Okay so, the lights now goes up and down but only when Col is set to a maximum of 6!

When col<7, rather then col<6 it lights up to #24 which is column 6, and then goes blank.
I can see on the Serial monitor the loop is still going. However no lights.

While (col<7);

While (col<6);

Any ideas welcome.

#include "LedControl.h"       //MAX7219 library for the bar graph
boolean direction = 1;
LedControl lc = LedControl(32, 36, 34, 0);
unsigned long previousMillisbar = 0;    //used for bar graph animation timing
int interval1 = 100;     //Timing for the bar graph

int col = 0;          //Start at the first LED
int row = 0;          //Start at the first LED

void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  lc.shutdown(0, false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0, 8);
  /* and clear the display */
  lc.clearDisplay(0);
}

void loop() {
  // put your main code here, to run repeatedly:
UPDOWN();

}


void UPDOWN() {
//  // put your main code here, to run repeatedly:
  if (direction == 1)   //Positive value of "updown" means filling the bar graph
 {
  col=0;
  row=0;
    while (col<6)     //There are only seven columns, 0 - 6
    {
      while (row<4)       //There are only four rows, 0 - 3
     {
        unsigned long currentMillisbar = millis();
        
        lc.setLed(0, row, col, true);                 //Set the LED at row and col to on
        
        if (currentMillisbar - previousMillisbar > interval1) //Check to see if the interval time has passed
        { 
          previousMillisbar = currentMillisbar;           //Store current program time
          row++;        //Increase row number by one
             Serial.print("row");
       Serial.print(row);
        }
      }     //row has passed 3, exit while loop
      row = 0;    //Reset row to zero
      col++; //Increase column number by one
      Serial.print("UP");
       Serial.print(col);
   }      
   Serial.print("row");
       Serial.print(row);
        Serial.print("col");
       Serial.print(col);
    direction = 0; 
   Serial.print(direction);
  
 }
 else 

 if (direction == 0)    //Negative value of "updown" means emptying the bar graph
  {
     //col = 7;      //Start at last LED
    // row = 4;      //Start at last LED
    while (col > -1)   //This prevents this loop from doing anything with the first column
    { //The first four LEDs will always be lit.  Change this to -1 to empty completely
      while (row > -1) //Count the rows down to zero
      {
        unsigned long currentMillisbar = millis();      //Get current program time
        lc.setLed(0, row, col, false);                  //Set the LED at row and col to off
        if (currentMillisbar - previousMillisbar > interval1) //Check to see if the interval time has passed
        { //If enough time has passed:
          previousMillisbar = currentMillisbar;  //Store current program time
          row--;                              //Decrease row number by one
        }                                       //Decrease row number by one
      }
      row = 4;              //Reset row to 4
      col--;                //Decrease column number by one
         Serial.print("down");
          Serial.print(col);
    }
    direction =  1;
    //updown changes from negative to positive value
  }      
 }

Why are you still using the direction variable.
Also if its value is up then there is no need for that second 'if' after the else. But as there is no need for the first 'if' it is irrelevant.

It is a waste of time patching up code that is bad in the first place.

Grumpy_Mike:
It is a waste of time patching up code that is bad in the first place.

I am sorry my learner code isn't up to your standard.

How is critisism without direction actually helping me work out where I'm going wrong?

I doubt the oddity with the 6vs7 wouldn't be impacted by my substandard if statement. As the loop works with up to 6.

I'm fairly sure I'm missing something obvious. As I've probably gone blind to my own code. Hence asking for help after hours of playing with combinations to no avail.

If you want to assist I'm happy to listen. But condensing tones are drowning out any help being offered.

//
//  upanddown01
//
byte point;
const int interval = 500;

void setup ()
{
  Serial.begin(9600);
}
void loop ()
{
  goingUP();
  goingDN();
}
void goingUP ()
{
  for(point=0; point<7; point++)  // 0 to 6
  {
    Serial.print(point, DEC);
    Serial.print(" ");
    delay(interval);
  }
}
void goingDN ()
{
  for(point=5; point>0; point --)  // 5 to 1
  {
    Serial.print(point, DEC);
    Serial.print(" ");
    delay(interval);
  }
  Serial.print("\r\n");    // new line
}

How is critisism without direction actually helping me work out where I'm going wrong?

In what way is "get rid of the direction variable and its associated if statements" not constructive?

I am telling you what to do. Just remove them they are not contribuitions anything, or affecting anything. Once you have got rid of the clutter you stand a better chance of spotting where you have gone wrong with what you have written.

It is like you have a number and you multiply it by five and then immediately dividing it by five. You have in effect done nothing to the number. It is he same with your code.

I am trying to help you if you would let me.