"noise" on the Serial Monitor

I have written a function on my Arduino Nano board and I want to keep track on it's output.

Serial.println(BootSequence());
BootSequence()

is a function that gives back false until it has been completed. When I watch the Serial Monitor I get 0 most of the time but every I think 50 prints I get a charakter like "m", "n" etc. the appear in alphabetical order, after "z" I get "{" and so on, this is a "normal" behavior?

In the setup part I use:

  Serial.begin(9600);

Can you do a simple

Serial.print( "AaBbCc" );

and it transfers good?

Post all the code in code tags, we have no idea what BootSequence does.

yeah i also have other print lines for state changes etc. and they print normal.

the strange thing is that the printed statesof a bool function should only be 0 or 1 shouldn‘t they?

enum class BootState { PHASE_0,
                       PHASE_1,
                       PHASE_2,
                       FINISHED };

bool BootSequenceHelper = false;

bool BootSequence(bool reset = false) {
  static BootState currentState = BootState::PHASE_0;

  if (reset) {
    cycSwitchLedsOn(CYCLE_PHASE_0, NUM_LEDS_CYCLOTRON, CYCL_BRIGHT_MAX_0, true);
    cycIncreaseBrightness(CYCLE_PHASE_1, NUM_LEDS_CYCLOTRON, CYCL_BRIGHT_MAX_0, CYCL_BRIGHT_MAX, true);
    cycDecreaseBrightness(CYCLE_PHASE_2, NUM_LEDS_CYCLOTRON, CYCL_BRIGHT_MAX, true);

    for (int i = 0; i < NUM_LEDS_CYCLOTRON; i++) {
      cyclotron_leds[i] = CRGB::Black;  // Turn off all LEDs
    }
    FastLedNeedsUpdate = true;
    currentState = BootState::PHASE_0;
    BootSequenceHelper = false;
  }

  switch (currentState) {
    case BootState::PHASE_0:
      if (cycSwitchLedsOn(CYCLE_PHASE_0, NUM_LEDS_CYCLOTRON, CYCL_BRIGHT_MAX_0)) {
        currentState = BootState::PHASE_1;
        return false;
      }
      break;

    case BootState::PHASE_1:
      if (cycIncreaseBrightness(CYCLE_PHASE_1, NUM_LEDS_CYCLOTRON, CYCL_BRIGHT_MAX_0, CYCL_BRIGHT_MAX)) {
        currentState = BootState::PHASE_2;
        return false;
      }
      break;

    case BootState::PHASE_2:
      if (cycDecreaseBrightness(CYCLE_PHASE_2, NUM_LEDS_CYCLOTRON, CYCL_BRIGHT_MAX)) {
        currentState = BootState::FINISHED;
        return false;
      }
      break;

    case BootState::FINISHED:
      if (!BootSequenceHelper) {
        BootSequenceHelper = true;
        //return true;
      }
      return false;


    default:
      return false;
  }
}
bool FastLedNeedsUpdate = false;

//################################################################################
bool cycSwitchLedsOn(const NextExecutionTime &cycleDuration, const int numberOfLeds, const int MaxBrightness, const bool reset = false) {
  static int currentLedIndex = 0;  // Track the current LED index
  if (reset) {
    currentLedIndex = 0;
    return false;
  }

  if (cycleDuration.hasElapsed()) {
    if (currentLedIndex < numberOfLeds) {
      cyclotron_leds[currentLedIndex] = CRGB(MaxBrightness, 0, 0);  // maybe adjust the brightness of 50
      if (currentLedIndex == numberOfLeds - 1) return true;         // because 28 LEDs but adressing from 0 to 27
      currentLedIndex++;
      FastLedNeedsUpdate = true;
    }
  }
  return false;
}

bool cycIncreaseBrightness(const NextExecutionTime &cycleDuration, const int numberOfLeds, const int setMaxBrightness, const int BrightnessIncreaseMax, const bool reset = false) {
  static int currentBrightness = setMaxBrightness;  // depending on brightness of Phase0
  if (reset) {
    currentBrightness = setMaxBrightness;
    return false;
  }

  if (cycleDuration.hasElapsed()) {
    for (int currentLedIndex = 0; currentLedIndex < numberOfLeds; currentLedIndex++) {
      cyclotron_leds[currentLedIndex] = CRGB(currentBrightness, 0, 0);  // maybe adjust the brightness of 50
    }
    if (currentBrightness == BrightnessIncreaseMax) return true;
    FastLedNeedsUpdate = true;
    currentBrightness++;
  }
  return false;
}

bool cycDecreaseBrightness(const NextExecutionTime &cycleDuration, const int numberOfLeds, const int setMaxBrightness, const bool reset = false) {
  static int currentBrightness = setMaxBrightness;
  if (reset) {
    currentBrightness = setMaxBrightness;
    return false;
  }

  if (cycleDuration.hasElapsed()) {
    for (int currentLedIndex = 0; currentLedIndex < numberOfLeds; currentLedIndex++) {
      cyclotron_leds[currentLedIndex] = CRGB(currentBrightness, 0, 0);  // maybe adjust the brightness of 50
    }
    if (currentBrightness == 0) return true;
    currentBrightness--;
  }
  return false;
}

I just don’t get it.

When I execute the function alone, all three sequences are played one after another as intended.

But when I call the function to check its return value, I get true immediately.

I tried introducing a helper variable to check if “finished” gets called, and “finished” gets called immediately too.

I am presuming you mean just calling:

BootSequence();

in void loop??

Sorry, no idea what is meant by that? How/where are you calling the function to check its return value?

I get the idea but since we can't see your updated code how are we to know what to expect?

Did you try this:

Did it print consistently?

I presume that 'reset' is initialised somewhere at the start of the sketch prior to running Bootsequence( )?

It is rather difficult to troubleshoot when there are only snippets and one can't see the whole picture.

yes normal printing executes without flaws.
I cut all the return false statements in the switch case so that the function Bootsequence only consists how this code and this seems to work.

bool BootSequence(bool reset = false) {
  static BootState currentState = BootState::PHASE_0;

  if (reset) {
    cycSwitchLedsOn(CYCLE_PHASE_0, NUM_LEDS_CYCLOTRON, CYCL_BRIGHT_MAX_0, true);
    cycIncreaseBrightness(CYCLE_PHASE_1, NUM_LEDS_CYCLOTRON, CYCL_BRIGHT_MAX_0, CYCL_BRIGHT_MAX, true);
    cycDecreaseBrightness(CYCLE_PHASE_2, NUM_LEDS_CYCLOTRON, CYCL_BRIGHT_MAX, true);

    for (int i = 0; i < NUM_LEDS_CYCLOTRON; i++) {
      cyclotron_leds[i] = CRGB::Black;  // Turn off all LEDs
    }
    FastLedNeedsUpdate = true;
    currentState = BootState::PHASE_0;
    BootSequenceHelper = false;
  }

  switch (currentState) {
    case BootState::PHASE_0:
      if (cycSwitchLedsOn(CYCLE_PHASE_0, NUM_LEDS_CYCLOTRON, CYCL_BRIGHT_MAX_0)) {
        currentState = BootState::PHASE_1;
      }
      break;

    case BootState::PHASE_1:
      if (cycIncreaseBrightness(CYCLE_PHASE_1, NUM_LEDS_CYCLOTRON, CYCL_BRIGHT_MAX_0, CYCL_BRIGHT_MAX)) {
        currentState = BootState::PHASE_2;
      }
      break;

    case BootState::PHASE_2:
      if (cycDecreaseBrightness(CYCLE_PHASE_2, NUM_LEDS_CYCLOTRON, CYCL_BRIGHT_MAX)) {
        currentState = BootState::FINISHED;
      }
      break;

    case BootState::FINISHED:
      return true;
  }
  return false;
}

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