Flag changes after function call

Sorry to be posting another question, but I’ve spent 2 days trying to figure out where my error is.

I have a Boolean flag named ResetHourglass. It is initially set to be false.

My variable MinCount increases by 1 every MinInterval and when it reaches 60, the flag changes to true so that my led hourglass can be reinitialized. The flag is then returned to false.

That works fine, but as soon as MinDisp() (after the reinitialization is processed), RestHourglass takes on the value of 255 which causes MinCount to increase to one number more than it should be after MinInterval has passed.

I cannot figure out why ResetHourglass doesn’t stay false.

I am using an UNO.

Thank you.

 [code]

// FastLED - Version: Latest
/*
     Shift register SN74hc595 pin 1 to digital display 9
                               2                    8
                               3                    6
                               4                    7
                               5                    4
                               6                    1
                               7                    not used
                               8                    grnd
                               9                    not used
                               10 to +5V
                               11 to arduino 4
                               12 to arduino 7
                               13 to grnd
                               14                   not used
                               15 to digital display 3
                               16 to +5V
*/

#include <FastLED.h>
#define LED_Pin 2
#define NUM_LEDS 120
CRGB leds[NUM_LEDS];
#define color1  CRGB::Red;
#define color2  CRGB{230, 126, 34};
#define color3  CRGB::Yellow;
#define color4  CRGB::Green;
#define color5  CRGB::Blue;
#define color6 CRGB::Indigo;
#define color7 CRGB::Black;

int HourTen = 0; // ten digit for hours
int HourOne = 1; // one digit for hours
unsigned long prevTime  = 0;
unsigned long currentTime = millis();
unsigned int MinInterval = 1000;
unsigned MinCount = 0;
int y = 0;
boolean ResetHourglass = false;

int DATA = 3; // Data Pin (pin 14 on the 74HC595)
int LATCH = 7; // Latch Pin (pin 12 on the 74HC595)
int CLOCK = 4; // Clock Pin (pin 11 on the 74HC595)
int MinAdjPin = 10;
int HrAdjPin = 9;
int TensDigit = 5; // tens digit for hours (HourTen)
int OnesDigit = 6; // one digit for hours (HourOne)

int Digit[10] = {64, 121, 36, 48, 25, 18, 2, 120, 0, 24};

void setup() {
  int DATA = 3;
  int LATCH = 7;
  int CLOCK = 4;
  unsigned  int HourTen = 0; // ten digit for hours
  unsigned  int HourOne = 1; // one digit for hours


  pinMode(LATCH, OUTPUT);
  pinMode(DATA, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(TensDigit, OUTPUT);
  pinMode(OnesDigit, OUTPUT);

  FastLED.addLeds<WS2812, LED_Pin, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(10);
  FastLED.clear();
  FastLED.show();

  pinMode(LATCH, OUTPUT);
  pinMode(DATA, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(TensDigit, OUTPUT);
  pinMode(OnesDigit, OUTPUT);

  //Initializing the Hourglass
  for (int x = 0; x < 10; x++) {
    leds[x] = CRGB::Red;
    leds[x + 10] = CRGB{230, 126, 34};
    leds[x + 20] = CRGB::Yellow;
    leds[x + 30] = CRGB::Green;
    leds[x + 40] = CRGB::Blue;
    leds[x + 50] = CRGB::Indigo;
  }
  for (int x = 60; x < 120; x++) {
    leds [x] = CRGB::Black;
  }
  FastLED.show();
  Serial.begin(9600);
}

void loop() {
  currentTime = millis();
  HrDisp();

  if (currentTime - prevTime >= MinInterval == true) {
    if (MinCount <= 59) {
      MinCount++;
    }
    if (MinCount >= 60) {
      MinCount = 0;
      ResetHourglass = true;

    }
    prevTime = currentTime;
    if (ResetHourglass == true) {
      //Initializing the Hourglass
      for (int x = 0; x < 10; x++) {
        leds[x] = CRGB::Red;
        leds[x + 10] = CRGB{230, 126, 34};
        leds[x + 20] = CRGB::Yellow;
        leds[x + 30] = CRGB::Green;
        leds[x + 40] = CRGB::Blue;
        leds[x + 50] = CRGB::Indigo;
      }
      for (int x = 60; x < 120; x++) {
        leds [x] = CRGB::Black;
      }
      FastLED.show();
      ResetHourglass = false;

      prevTime = currentTime;
      HourOne++;
    }
    Serial.print("Entering MinDisp: MinCount/ResetHourglass/HourOne =  ");
    Serial.print (MinCount);
    Serial.print(" ");
    Serial.print(ResetHourglass);
    Serial.print(" ");
    Serial.println(HourOne);

    MinDisp(); // #1
  }
  Serial.print("Just left MinDisp: MinCount/ResetHourglass/HourOne =  ");
  Serial.print (MinCount);
  Serial.print(" ");
  Serial.print(ResetHourglass);
  Serial.print(" ");
  Serial.println(HourOne);

  HrDisp();

  if (digitalRead(MinAdjPin) == HIGH) {
    MinInterval = 200;
  }
  else {
    MinInterval = 1000;
  }
  if (digitalRead(HrAdjPin) == HIGH) {
    HourOne++;
    delay(500);
    HrDisp();
  }
  //  HrDisp();

}
void MinDisp() {

  leds[60 - MinCount] = CRGB::Black;
  y = 10 - MinCount;
  if (y >= 0) {
    leds[y] = color2;
    leds[120 - MinCount] = color1;
    FastLED.show();
  }

  y = 20 - MinCount;
  if (y >= 0) {
    leds[y] = color3;
    if (MinCount > 10 && MinCount < 21) {
      leds[120 - MinCount] = color2;
    }
    FastLED.show();
  }

  y = 30 - MinCount;
  if (y >= 0) {
    leds[y] = color4;
    if (MinCount > 20 && MinCount < 31) {
      leds[120 - MinCount] = color3;
    }
    FastLED.show();
  }

  y = 40 - MinCount;
  if (y >= 0) {
    leds[y] = color5;
    if (MinCount > 30 && MinCount < 41) {
      leds[120 - MinCount] = color4;
    }
    FastLED.show();
  }
  y = 50 - MinCount;
  if (y >= 0) {
    leds[y] = color6;
    if (MinCount > 40 && MinCount < 51) {
      leds[120 - MinCount] = color5;
    }
    FastLED.show();
  }

  if (MinCount > 50) {
    leds[120 - MinCount] = color6;
    FastLED.show();
  }
}

void HrDisp() { //Displays the hour
  if (HourOne == 10) { // Reset to 0 after counting for 9 hours.
    HourOne = 0;
    HourTen = 1;
  }
  if (HourTen == 1 and HourOne == 3) { //13:00 becomes 1:00
    HourTen = 0; // Resets hours ten digit to 0
    HourOne = 1;
  }

  digitalWrite(LATCH, LOW);
  shiftOut(DATA, CLOCK, MSBFIRST, Digit[HourOne]);
  digitalWrite(LATCH, HIGH);
  display1();
  delay(5);

  if (HourTen != 0) {
    digitalWrite(LATCH, LOW);
    shiftOut(DATA, CLOCK, MSBFIRST, Digit[HourTen]);
    digitalWrite(LATCH, HIGH);
    display2();
    delay(5);
  }
}
//End of Void HrDisp()

void display1()
{
  digitalWrite(TensDigit, 1);
  digitalWrite(OnesDigit, 0);
}
void display2()
{
  digitalWrite(TensDigit, 0);
  digitalWrite(OnesDigit, 1);
}

[/code]

At a glance:

  if (currentTime - prevTime >= MinInterval == true) {

= means assignment

== means comparison

What is your intent with this line of code?

This is a problem:

    leds[120 - MinCount] = color1;

When MinCount resets to 0 then you are writing past the end of the leds array. This corrupts the memory locations after the leds array. I suspect ResetHourglass is one of those locations.

Thank you, dougp,

if (currentTime - prevTime >= MinInterval == true) {
lets me know that MinInterval has passed. That seems to working as it should. I got rid of the == (leaving if (currentTime - prevTime >= MinInterval){, but that did not help solve my proglem.
It appears that MinDisp() is running twice after the reset, but I cannot find out why.

Thank you, ToddL1962,

YOU ARE A GENIUS!

I changed "#define NUM_LEDS 120" to "#define NUM_LEDS 121"
and that seems to have taken care of the problem!!

Thank you so very very much!

Michael

But if you truly have 120 leds in your code, that is not the best way to fix your problem. If you have 120 leds, then you index from 0 to 119, not just bump up the size of your array to avoid logic problems elsewhere in the code.

blh64:
But if you truly have 120 leds in your code, that is not the best way to fix your problem. If you have 120 leds, then you index from 0 to 119, not just bump up the size of your array to avoid logic problems elsewhere in the code.

Yes, and when an ordinal size is specified, that is how it will probably be used (as a count), sometimes in many places in the code. Changing the definition arbitrarily could have bad side effects in some of those other places.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.