Project 1: Using Variables

In this project you will experiment with the use of variables. 

Introduction

The creation and use of variables is the most essential part of any programming language. Variables have a name and contain a value. Variables exist in a scope. The scope of a variable determines the places where it can be used. In this project you will see how a variable's scope is determined by where the variable is declared.

What should this program do?
  • Use cout 
  • Use both a global and a local variable. 

Part 1: Modify the Blink Sketch

Modify the blink program in the CS-11M template to add two variables: 

int onTime = 200;
int offTime = 400; 

Change the program so that the onTime variable controls how long the LED is ON and the offTime variable controls how long the LED is OFF. Name your program prog1_part1.ino

Part 2: Blink Fractions

Modify your program from Part 1 so that offTime is computed from onTime. The declaration of your variables should look exactly like this: 

int onTime = 200;
int offTime; 

In the setup() function assign offTime so that it is double the value of onTime. In loop() use the cout command to print the values of onTime and offTimeTest it to be sure that it looks right. Name your program prog1_part2.ino.

Questions

Answer these questions for both your program in Part 1 and Part 2.
  1. What scope are your onTime and offTime variables declared in? 
  2. Would your program work if the onTime and offTime variables were declared in loop()? Why or why not?

Turn In

  1. arithmetic.ino (The class exercise)
  2. prog1_part1.ino
  3. prog1_part2.ino
  4. Answers to the questions
Submit your homework on canvas.

Grading

  • 5 points for class exercise(s)
  • 8 points for each program
    • Style (2 points) 
      • Does your program have the correct comment header? 
      • Is your program properly formatted? 
    • Function  (6 points)
      • Your program must compile
      • Does it produce the correct output? 
  • 4 points for correct answers to the questions
Comments