Go Down

Topic: Turning serial communications on with a switch... (Read 591 times) previous topic - next topic


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

Code: [Select]

// 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:


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

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


Dec 13, 2012, 10:14 am Last Edit: Dec 13, 2012, 10:42 am by guix Reason: 1

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

Code: [Select]

bool SerialEnabled;

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


Untested... :)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131