Bypass Serial.println in Code?

hello,
I would like to have a way to bypass Serial.println and serial.print (perhaps triggered by a boolean condition???)

"if potato = 1, bypass this stuff" type deal, but without if statements everywhere.

Im curious if it can be done mid program - psudo real world application, like when a switch is flipped.

its fine the way it is, ive just always wondered if this can be done.
im sure its something to the effect of pre defining some function that interlocks print line to a variable, im just not sure how to do it.

and i dont want to stop all serial, i want to still be able to take an input.

Have you looked at Case/Switch?

if (potato == 1)

well obviously dude - just getting my point across via simple example

That would be
if( potato != 1 ){
// stuff to bypass
}

I have not - I will look into it.
I use heavy conditional functions (sub-routines) so its not like its a program speed problem or anything.

would be just a nice thing to be able to do since i cant find anything on it.
now people talk about debug, but as i have understood it its something thats defined ahead of upload, not triggered on the fly while program is already looping.

I actually display serial on a screen when connected via BT, but when not connected, I dont need to be filling a BT buffer. not that its an issue - just always been curious about this topic.

if (hungUp_onSomethingIRRELEVANT== true) {
moveOn = true;
Serial.println(F("See ya"));
}

all in good nerd fun here btw lol :smiley:

Code would not compile... needed definitions...

bool hungUp_onSomethingIRRELEVANT, moveOn;

void setup() {
  Serial.begin(115200);
  if (hungUp_onSomethingIRRELEVANT == true) {
    moveOn = true;
    Serial.println(F("See ya"));
  } else {
    Serial.println(F("I can't live without you!"));
  }
}

void loop() {}

Would an interrupt do?

LOL!
Permanently says this. :rofl:

The simplest form of debugging is to use print statements to signal the code is in a certain place and the variables are what you expect them to be. This has to be set up before hand and can be done by simply writing print statements in the code, and then comment them out when you are finished with them.

There is also a system called "conditional completion" where typically you have a system variable (starting with a hash) call it #debug and define it as being true when you want all your debug prints included or not define it at all if you don't.
See Conditional compilation in C programming language | Programming Simplified
for details. Again this is set up ahead of time.

However, there is another form of debugging, which you can specify if you have the correct hardware tools, that builds in the code with certain break points, than when the code reaches the break point the program stops and then you can use other tools to look at memory locations and even single step through the code to see what happens next, and then run the code until the next break point.

Sometimes you can use a key on the keyboard which will stop the code where it is currently and let you use these tools. So for that one there is no need to prepare that in advance. This sort of thing can be known as Jtag system after the name of the hardware used to do it, but there are many other systems that will let you do that.

But the C build has to be done as a debug version. Then when it is all working you compile a release version which is often a lot shorter because it doesn't have the extra code needed for the debugging software.

do you just want to skip it, like an optional debug, or do you want to directly send data to the UART, bypassing the "Serial" code or use some other mechanism?

if (debug)
    Serial.println (msg);

No problem. Assign a port pin with an external switch to enable debug prints.

if (digitalRead(debug_pin)) Serial.println("Bug found here!"); //if pin is HIGH print debug message

okay, let me back up - im not interested in debug. im only asking because when the user connects with their app (i wrote) via BT, theres telemetry that they can see and interact with - change target settings, and so on.
theres really no reason to print any serial when the user isnt connected - again, not that it matters as i dont have loop time issues - this is just out of curiosity.

just want to be able to send a signal which, when received, i set a boolean value to true (all thats easy no problem), and then if this value is true, it would be neat to then only print serial data.

basically otherwise it keeps going to BT buffer, which right now on connection I basically scrub the buffer via the app, then start displaying telemetry. (takes 0.5 seconds on connection)
works great, but its one of those things that no one sees, but me the programmer doesnt like it because it "feels" like a workaround. functionally, its completely inconsequential.

just has to be a better way to enable and disable serial output without if statements everywhere which i wouldnt even consider doing.

Create 2 functions, one that does nothing and one that does the printing or whatever and use a function pointer to call them. To disable the call, point to the "do_nothing" function and to enable the call, point to the "do_the_job" function.

Try using Serial.end() to stop the serial port and Serial.begin() to start it up again.
as per
Serial.end

still need to be able to receive serial or I wouldnt know when to enable or disable it

the dumbest but simplest way i can think to do it is some junk like this - which IMO is more backwards than just scrubbing a buffer.

void serialFunction() {
if (yo_iSaidPRINT == 1) {
Serial.println(newMessage);
}
}

void loop() {
codey code code;
s'more code;
the code doing some code things like a happy little code do;
newMessage[x] = "this what codey McCode Face just did";
serialFunction();
}

you could write your own print class /structure which accepts write (which will be used in all kind of print/println) and switch between a pointer to either Serial or nullptr during runtime.


struct: Print {
  virtual size_t write(uint8_t) {
    return 1;
  }
} dummy;

Print *debug = nullptr;

void setup()
{
  Serial.begin(9600);
  Serial.println("Start");

  debug = &Serial;
  debug->println("Debug 1 is on");

  debug = &dummy;
  debug->println("Debug 2 is off");

  debug = &Serial;
  debug->println("Debug 3 is on again");

  debug = &dummy;
  debug->println(F("Debug 4 is off"));

  debug = &Serial;
  debug->println(F("Debug 5 iss on again"));

  float afloat = 123.45;
  debug->println(afloat);

  char achararray [] = {"bla bla\0"};
  debug->println(achararray);

  String anArduinoStringObject = "bla blub";
  debug->println(anArduinoStringObject);

  //debugPrint("Pin x auf HIGH"); 
}

void loop()
{

}

another variant is a class which takes care of the if statement

/* Optionale Debug.print Ausgabe zu Serial - Veränderbar zur Laufzeit
   http://forum.arduino.cc/index.php?topic=575044.0
   https://forum.arduino.cc/t/how-to-create-a-seriallog-function-that-can-be-switched-off/1057755/10
 */

class debugPrint : public Print {
    uint8_t _debugLevel = 1;
  public:

    void on()
    {
      setDebugLevel(1);
    }

    void off()
    {
      setDebugLevel(0);
    }
    void setDebugLevel(byte newLevel) {
      _debugLevel = newLevel;
    }
    size_t write (uint8_t value)
    {
      if (_debugLevel)
      {
        Serial.write(value);
      }
      return 1;
    }
} Debug;  // this instance starts with a capital letter to make it look similar to "Serial" 


void setup() {
  // put your setup code here, to run once:

  Serial.begin(4800);
  Serial.println(F("Debug Print Test"));

  Debug.println("Debug 1 is on");

  Debug.setDebugLevel(0);
  Debug.println("Debug 2 is off");

  Debug.setDebugLevel(1);
  Debug.println("Debug 3 is on again");

  Debug.off();
  Debug.println(F("Debug 4 is off"));

  Debug.on();
  Debug.println(F("Debug 5 is on again"));

  float afloat = 123.45;
  Debug.println(afloat);

  char achararray [] = {"bla bla\0"};
  Debug.println(achararray);

  String anArduinoStringObject = "bla blub";
  Debug.println(anArduinoStringObject);

}

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

}

both examples use the Print.h and all functionalities of Print.h remain when the output is activated

P.S.: please post your code in code tags

Try commenting out out..

Serial.begin(bla bla);

-jim lee