Serial Control Blink
 

Step 1 - Build the Project

Using the serial monitor provides you with a way to send information to and receive information from the Maker Board. Here you send in a variable’s value and the code updates as soon as Maker Board receives the input.

Note about serial monitor: first click "Upload to Maker Board", complete the upload, then click the "Monitor" button in the top toolbar to open the serial monitor. This allows the data to be sent from the Maker Board to the serial monitor. You can enter messages within the serial monitor and click 'send' to send serial messages to Maker Board.

Step 2 - Upload the Code

/* * Control the blink rate of the LED attached to pin 13 on the Maker Board. * After upload, open the serial monitor by pressing the magnifying glass. * Type the value of the delay in the space next to "Send" and press send. */ unsigned long blinkRate = 1000; //Default 1000 milliseconds between blinks void setup() { pinMode(13,OUTPUT); // LED on pin 13 Serial.begin(9600); //Start the serial port Serial.println("Enter the blink delay length in milliseconds (try 100)"); } void loop() { //If there is anything on the serial port for the Maker Board to read... if (Serial.available() > 0){ //set blinkRate equal to the value you typed in the serial monitor blinkRate = Serial.parseInt(); Serial.println(blinkRate); } digitalWrite(13,HIGH); delay(blinkRate); digitalWrite(13,LOW); delay(blinkRate); } //Use Serial.println to make a message appear when you press enter. Format: //Serial.println("message here"); // Need help? http://www.letsstartcoding.com/help // (c) 2016 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Step 3 - Read the Walkthrough

The first thing you’ll do is create a variable of the type ‘unsigned long’. You can read up on C++ variable types, but this one allows for very large inputs, so you have have a delay of over 4,000,000,000 milliseconds if you want to. This variable is what your inputs will actually update.

To use the serial monitor, you have to tell the Maker Board to be ready to send and receive information from it. The Serial.begin(9600); command in setup does that and it sets the communication rate at 9600. When you open the serial monitor, make sure that the number in the bottom right hand corner is also 9600. If it isn’t, your communication will be garbled.

Serial.print and Serial.println (pronounced print-line) are the easiest ways to get text onto the serial monitor. The starter message in the setup() will print once, then jump to a new line.

The loop() section is using an if statement to ask “Is there any new information from the serial monitor?” and if there is, the if statement takes that information and puts it into the variable.

Next, that variable is used as the delay for a simple blink of the LED using digitalWrite. After the first blink has ended, the loop checks for new information from the serial monitor.