Schedule‎ > ‎

Lab 07: Functions, Constant and Static Variables

Objectives

  • You will practice writing functions.
  •  You will practice using Static Variables

^ top

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You must breadboard the circuits yourself, though you may get help from others if you get stuck.
  • You may discuss the code with other students for this lab.
  • You must enter all the code yourself and not copy from another student.

^ to

Part 1: SOS Revisited

In this lab you rework the SOS exercise from lesson 2B.

Parts

  • Arduino board
  • USB cable

Specifications:

  1. Download the sos.ino file to a convenient location like the Desktop.
  2. Start the Arduino IDE and load the sos.ino file.
  3. Save the sketch as sos_fun.
  4. Upload the sketch and observe the pattern of the built-in "L" LED blinks.
  5. Since pin is a constant,  declare it as a constant and make the name all caps with an underscore as is standard for a constant variable.
  6. You will need to change the code anywhere it uses pin to your new variable name.
  7. After the closing curly brace of the loop() function, start a new function with the following code:
    // New function definition
    void blinkLED(int numBlinks, int duration) {
      // add for-loop code here
    }
    

    The variables declared inside the parenthesis are known as parameters. Parameters receive information for use inside the function.

  8. Fill in the code that should go inside the curly braces of the blinkLED() function.  This will be a for loop which uses the parameter numBlinks for the loop limit and duration for one of the delays.  For the final delay you can code delay(250);  since it stays the same.                                  
  9. Delete the for-loops inside the loop() function and replace them with the following function calls:
    blinkLED(3, 250);
    blinkLED(you fill in the parameters here);
    blinkLED(you fill in the parameters here);
    delay(3000); // last part of original loop() code
    
  10. Upload your code to the Arduino and verify the built-in LED is still blinking with the same pattern.
  11. Now,  create a static variable named counter inside loop().  At the end of each loop,  add 1 to the counter using the post-increment operator and then print the value of counter to the Serial Monitor. Print the following:  number of times SOS has flashed:  1
  12. Your output should look like this:                                                                                                      
    number of times SOS has flashed: 1
    number of times SOS has flashed: 2
    number of times SOS has flashed: 3
    number of times SOS has flashed: 4
    number of times SOS has flashed: 5


  13. Submit your sos_fun.ino file as part of this lab.

Lab Questions - Answer and paste into the comment in Canvas

  1. True or false: the code with an added function is shorter than the original code.
  2. The special variables listed inside the function definition parenthesis are known as ________.
  3. The following code will cause the built-in LED to blink ________ times.
    blinkLED(10, 250);
    
  4. The pattern of blinks in this lab, which is the morse code pattern for SOS, is ________.
    1. 3 short, 3 long, 3 short
    2. 3 long, 3 short, 3 long
    3. 3 short, 3 short, 3 long
    4. 3 long, 3 short, 3 short
  5. What is wrong with using a global variable?

  6. What is a static variable and how does it help me avoid using a global variable?

  7. What is a const variable and why would I use it?

^ top

^ top

Grading Criteria(10pts):

10 points  (Header 1pt, Compiles 2 pts, Proper Formatting 1 pt,  Works as Specified 6 pts:   constant variable, function, function calls, static variable, questions answered)

ANSWERS TO QUESTIONS:


  1. True or false: the code with an added function is shorter than the original code.  TRUE
  2. The special variables listed inside the function definition parenthesis are known as _PARAMETERS____.
  3. The following code will cause the built-in LED to blink ____10___ times.
    blinkLED(10, 250);
    
  4. The pattern of blinks in this lab, which is the morse code pattern for SOS, is ____a____.
    1. 3 short, 3 long, 3 short
    2. 3 long, 3 short, 3 long
    3. 3 short, 3 short, 3 long
    4. 3 long, 3 short, 3 short
  5. What is wrong with using a global variable?  It is hard to debug because you don't know which function changed it.

  6. What is a static variable and how does it help me avoid using a global variable?  A static variable is a variable you declare static inside of a function.  It is not re-initialized every time the function is called,  so it retains its value across function calls.  This is useful for counting how many times a function is called and other uses.

  7. What is a const variable and why would I use it?  A const variable is constant and does not change.  You use it to indicate to the compiler that it can optimize storage for this variable knowing it will not require a change.

COMPLETED CODE:

Completed lab code
Subpages (3): blink_fun sos_fun tmp_fun