Arduino LESSON 4: Printing Over the Serial Port

Arduino LESSON 4: Printing Over the Serial Port

Arduino LESSON 4: Printing Over the Serial Port

This circuit will allow you to independently control two Light Emitting Diodes from the arduino microcontroller

In Lesson 3, you learned about for loops and how for loops can make your life as a programmer easier. You have written a program in which a red LED and then a yellow LED flashes as often as the program indicates. , we use the same 2LED circuit. If you haven't assembled it yet, now is the time to do it.

In order to independently blink the two LED’s you were probably using code similar to this:

int redLEDPin=9; //Declare redLEDPin an int, and set to pin 9 
int yellowLEDPin=10; //Declare yellowLEDPin an int, and set to pin 10 
int redOnTime=250; //Declare redOnTime an int, and set to 250 mseconds 
int redOffTime=250; //Declare redOffTime an int, and set to 250 
int yellowOnTime=250; //Declare yellowOnTime an int, and set to 250
int yellowOffTime=250; //Declare yellowOffTime an int, and set to 250
int numYellowBlinks=5; //Number of times to blink yellow LED
int numRedBlinks=5;  //Number of times to blink red LED

void setup() {
  pinMode(redLEDPin, OUTPUT);  // Tell Arduino that redLEDPin is an output pin
  pinMode(yellowLEDPin, OUTPUT);  //Tell Arduino that yellowLEDPin is an output pin
}

void loop() {

for (int j=1; j<=numRedBlinks; j=j+1) {     // Start our for loop
    digitalWrite(redLEDPin,HIGH); //Turn red LED on
    delay(redOnTime);             //Leave on for redOnTime
    digitalWrite(redLEDPin,LOW);  //Turn red LED off
    delay(redOffTime);            //Leave off for redOffTime
}
 
for (int j=1; j<=numYellowBlinks; j=j+1) {     // Start our for loop
    digitalWrite(yellowLEDPin,HIGH); //Turn yellow LED on
    delay(yellowOnTime);             //Leave on for yellowOnTime
    digitalWrite(yellowLEDPin,LOW);  //Turn yellow LED off
    delay(yellowOffTime);            //Leave off for yellowOffTime
}
  
}

As you learned in Lesson 3, for loops make your program a lot easier to write and a lot easier to modify. In the code above, all you need to do is change two lines of code to change how often the LEDs blink:

int numYellowBlinks=5; //Number of times to blink yellow LED
int numRedBlinks=5;  //Number of times to blink red LED

By changing either of these variables at the top of your program, you effectively adjust how many times each LED will blink in each cycle.

Now it would be nice to pass on information directly to the user in addition to the flashing LED. It would be nice to have the Arduino printing information on your computer screen. You can do this by working with the serial port. and the serial monitor.

In order to use the Serial Port, the first thing you have to do is turn it on inside your program. Since this is something you would only need to do once, you do it in the void setup(). The code below, when placed inside the void setup() will turn your serial port on:

Serial.begin(9600);

You want to keep whatever else you have in your void () configuration and just add the above line of code. Notice that this line of code tells the Arduino to turn on the serial port. It tells the Arduino to communicate at 9600 baud, which is basically as fast as it works. The higher the number, the faster data is sent and received via the serial interface. This can be set to different numbers, but the important thing is that everyone is speaking and listening at the same speed. So if you tell the Arduino to run at 9600 baud, then when you later open your serial monitor you need to make sure it is set to the same speed.

OK, now that you've started your serial monitor, you're ready to begin sending and receiving data. The first and easiest thing is to send you data. We do this with the Serial.print and Serial.println commands. These commands send data to the Arduino serial monitor. We'll show you how to open the serial monitor in a moment, but now let's add some print statements to our program.

for (int j=1; j<=numRedBlinks; j=j+1) {     // Start our for loop
    Serial.println(j);
    digitalWrite(redLEDPin,HIGH); //Turn red LED on
    delay(redOnTime);             //Leave on for redOnTime
    digitalWrite(redLEDPin,LOW);  //Turn red LED off
    delay(redOffTime);            //Leave off for redOffTime
}

Notice that the loop above is our loop for blinking the red LED. We have added a new line to the start of the loop . . . Serial.println(j). After you add the line of code, download the code to the arduino, and then pop open your serial monitor. You do this by clicking on the magnifier icon at the upper right corner of the arduino IDE.

Click on Magnifier to Open the Arduino Serial Monitor

When you click on the icon you should see the serial monitor pop open, and you should see the values of j being printed out. It should look like this:

Serial Monitor shows what you are printing

You should see the numbers print out as your program goes through the for loop, make sure the baud rate in the lower right corner of the serial monitor is set to the same value you specified in the program. Choice.

In the example above, we output the variable j every time the program goes through the for loop for the red LED. We can also print a text string. To print a text string, enclose the string in quotation marks. To print a string you need to do something like Serial.println ("red LED blink"). The words between the quotation marks are printed. Let's add this to our code, but let's add it before the for loop so that it only prints once in each loop. Your code for the red LED should look like this:

Serial.println("The Red LED is Blinking");
  for (int j=1; j<=numRedBlinks; j=j+1) {     // Start our for loop
    Serial.println(j);
    digitalWrite(redLEDPin,HIGH); //Turn red LED on
    delay(redOnTime);             //Leave on for redOnTime
    digitalWrite(redLEDPin,LOW);  //Turn red LED off
    delay(redOffTime);            //Leave off for redOffTime
}

Now download and run the code and look at your serial monitor. You should see these words print each time before the red light flashes. Now go ahead and add a similar code to your yellow LED loop to make it announce which LED is blinking and then the blink counter. Your code should look like this for the two for loops:

 Serial.println("The Red LED is Blinking");
  for (int j=1; j<=numRedBlinks; j=j+1) {     // Start our for loop
    Serial.println(j);
    digitalWrite(redLEDPin,HIGH); //Turn red LED on
    delay(redOnTime);             //Leave on for redOnTime
    digitalWrite(redLEDPin,LOW);  //Turn red LED off
    delay(redOffTime);            //Leave off for redOffTime
}
  Serial.println("The Yellow LED is Blinking");
  for (int j=1; j<=numYellowBlinks; j=j+1) {     // Start our for loop
    Serial.println(j);
    digitalWrite(yellowLEDPin,HIGH); //Turn yellow LED on
    delay(yellowOnTime);             //Leave on for yellowOnTime
    digitalWrite(yellowLEDPin,LOW);  //Turn yellow LED off
    delay(yellowOffTime);            //Leave off for yellowOffTime
}

So this code will announce which LED is blinking, and will then give a count of which blink you are on. Pretty cool! Your Serial Monitor should now be looking something like this as it tells the user which LED is blinking and which blink you are on:

Serial Monitor shows what values the arduino is printing

So we can see that we can print both variables, like j, and strings. When printing strings, it is important to include the text in quotes, so that the arduino knows you are printing the string of text, and not a variable.

Note that every time you use Serial.println you go to the next line. If you want to print on the same line without going to the next line, you should use the Serial.print command. In the example above, instead of just printing the number j, we say you want some text in front of the number that says you are on the flashing number, and then the number you can change your code and the for loops another serial Add .print command. I'll do it for you on the red ribbon, but you have to discover the yellow one for yourself:

 Serial.println("The Red LED is Blinking");
  for (int j=1; j<=numRedBlinks; j=j+1) {     // Start our for loop
    Serial.print("   You are on blink number: ");
    Serial.println(j);
    digitalWrite(redLEDPin,HIGH); //Turn red LED on
    delay(redOnTime);             //Leave on for redOnTime
    digitalWrite(redLEDPin,LOW);  //Turn red LED off
    delay(redOffTime);            //Leave off for redOffTime
}

You should get something that looks like this:

Nicely formatted Arduino Serial Monitor

Make sure you play with spaces when printing strings so your output is clean and readable. Notice in mine that with spaces I get an indentation that makes the scrolling text easier to read.

So, we need to carefully consider when it suitable to use Serial.print vs. when we should use Serial.println.

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0