Turning serial communications on with a switch...

Would it save processing power if serial communications were determined by the position of a switch? I know every piece of data that would need to be outputted to the serial monitor would need to be enclosed in an if statement and in order to turn serial back on you'd need to reboot the Arduino

// digital pin 2 has an on/off switch
int pushButton = 2;
boolean buttonState = LOW;
// the setup routine runs once when you press reset:
void setup() {

  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);

  // read the input pin:
  buttonState = digitalRead(pushButton);
  if(buttonState == HIGH){
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
  }

}

// the loop routine runs over and over again forever:
void loop() {

  if(buttonState == HIGH){
  Serial.println("Serial communications are running");
  }
  
  
}

Hello,

Yes it will probably make your program a little faster when disabling those Serial.print's

I'm not sure what will be faster, a if around each Serial.print's, or a single if that do Serial.end / Serial.begin

bool SerialEnabled;

void loop()
{
  if ( digitalRead( button_toggleSerial ) )
  {
    if ( !SerialEnabled )
    {
      SerialEnabled = true;
      Serial.begin( 9600 );
      Serial.println( "Serial enabled" );
    }
  }
  else
  {
    if ( SerialEnabled )
    {
      SerialEnabled = false;
      Serial.println( "Serial disabled" );
      Serial.end( );
    }
  }

  ...
}

Untested... :slight_smile: