Pages: [1]   Go Down
Author Topic: Turning serial communications on with a switch...  (Read 185 times)
0 Members and 1 Guest are viewing this topic.
Sussex, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

// 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");
  }
 
 
}

Logged

France
Offline Offline
God Member
*****
Karma: 34
Posts: 978
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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... smiley
« Last Edit: December 13, 2012, 04:42:08 am by guix » Logged

Pages: [1]   Go Up
Jump to: