Firmware Tips and Tricks

##Pause your application to make a Serial over USB connection##
Here is a quick trick that makes opening a serial connection over USB on Windows easy and foolproof.

// The following line is for Local Builds, 
// but works fine in the (Web IDE)
#include "application.h"

void setup() {
  // Make sure your serial terminal is closed before starting your Core.
  // Start up your Spark Core, you should hear a USB driver connection
  // being made after the following line runs.
  Serial.begin(115200);

  // Now it's ok to open your serial terminal software, and connect to the
  // available COM port.  The following line effectively pauses your
  // application waiting for a character to be received from the serial
  // terminal.  While it's waiting it processes the background tasks to
  // keep your Core connected to the Cloud.  Press ENTER in your 
  // serial terminal to start your application.
  while(!Serial.available()) Spark.process();

  // Try commenting out the above line and see if it's hard to make a 
  // serial connection over USB ;-)  On Windows it will likely tell you
  // that your serial port is in use already.  That's because the RX buffer
  // on the computer is getting hammered with data.  Pausing the 
  // transmissions from the Spark Core solves this problem.
}

void loop() {
  // Just something to test print
  Serial.println("Hello Sparky!");
  delay(100);
}
3 Likes