Schedule‎ > ‎

Lab 07: Decisions and Variables


  • Use variables of various data types.
  • Write code that gets user input using the Serial port.
  • Make more extensive use of if-statements.
  • Explore how to set colors in an RGB LED.

^ 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.
  • You may NOT look at another student's code until you complete and submit this assignment.
  • You may get help from people if you get stuck, but only if they do not show or tell you the code to type.

^ top

Project A: Variable Delay

In this project you use variables and Serial I/O to control the blink rate of the built-in LED.


  1. Start with the Blink sketch

    File > Examples > Basics > Blink

  2. Save the sketch as variable_delay.
  3. In the setup() function, print the string, "Enter the delay in milliseconds".
  4. In the loop() function, write code to:
    1. Get an integer value from the user
    2. Set the delay period
    3. Continuously blink the built-in LED for the value of the delay period
  5. At the end of the file comment block show the operation of your program for the following examples and at least two original examples.  Use Ctrl-c then Ctrl-v to copy+paste from the serial monitor.  The output should look like the Example Output below but with the required values entered.
  6. Submit your variable_delay.ino file as part of this assignment.


  • To remove excess input from the timeout, use a while() loop or other technique.

Example Output

Enter the delay in milliseconds
You entered 100
You entered 50

^ top

Project B: Color Control

In this project you get values from the user to set the RGB LED.


  1. Start with the RGB LED breadboard from Lab 1 Part 2.

    Remember that you are using a common anode RGB LED and substituting 560 Ohm resistors.

  2. Save the sketch as color_picker.
  3. In the setup() function, print the string, "Enter RGB integers separated by spaces".
  4. In the loop() function, write code to:
    1. Get three integer values from the user
    2. Assign the values to red, green and blue
    3. Color the RGB LED base on the three values entered.
  5. At the end of the file comment block show the operation of your program for the following examples and at least two original examples. The output should look like the Example Output below but with the required values entered.
    red=42, green=42, blue=0
    red=42, green=0, blue=42
  6. Submit your color_picker.ino file as part of this assignment.

Example Output

Enter RGB integers separated by spaces
You entered red=42, green=42, blue=0
You entered red=42, green=0, blue=42

^ top

Project C: Color Organ

In this project, you continue working with the RGB LED to develop a color organ!


  1. Start with the breadboard of the previous project, Project B: Color Control.

    Here is the updated Fritzing project: color_organ.fzz.

  2. Add the circuity shown in the breadloard layout shown below.

    Note that the recommended kit has only two pushbuttons, so you will need to move an existing pushbutton to check the circuit or borrow one from a classmate. Also, if you have a common cathode you will need to move the wire for the long LED pin from 5v to GND.

  3. Use the starter code to start development and verify your breadboard layout.
  4. Save the sketch as color_organ.
  5. In the setup() function, add code to set the pin mode for the green and blue pushbuttons.
  6. In the loop() function, write code to detect button presses, like the starter code for the red button.
  7. For each button press, increase the value of the colors red, green or blue.
  8. In addition, for each button press, check if the value is greater than 255. If so, set the value back to zero (0).
  9. Submit your color_organ.ino file as part of this assignment.

Parts List

  • Arduino board
  • USB cable
  • Solderless breadboard
  • RGB LED (common anode assumed)
  • 3x resistors, in the range of 220-600 Ohms
  • 3x pushbuttons
  • Jumper Wires

Breadboard Layout

Color organ breadboard

Starter Code

int redPin = 11;
int greenPin = 10;
int bluePin = 9;

int redButtonPin = 7;
int greenButtonhPin = 6;
int blueButtonPin = 5;

int red = 128;
int blue = 0;
int green = 0;

//uncomment this line if using a Common Anode LED

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(redButtonPin, INPUT_PULLUP);
  // set pin mode for green and blue pushbuttons here
  setColor(red, green, blue);

void loop() {
  if (digitalRead(redButtonPin) == LOW)
    cout << "Red button pressed. New values: " << red << " " << green << " " << blue <<  endl;
    // increase red count
    // check if red  > 255. If so, reset to 0
  // Add similar code for green and blue buttons
  setColor(red, green, blue);

void setColor(int red, int green, int blue) {
  red = 255 - red;
  green = 255 - green;
  blue = 255 - blue;
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);

^ top

Style Requirements

Remember to follow these style rules for full credit:

  1. File comment block (see: File Comment Block)
  2. Descriptive and meaningful variable names (see: Use Meaningful Names
  3. Using the variable naming conventions (see: Variable Names
  4. Indentation inside curly braces (see: Indentation)

^ top

Extra Credit

The following are worth extra credit points:

  1. Calculate the correct value of the red LED resistor and include the calculations in a README.txt file. This will require research into LED voltage drops. Include the source(s) of your information for credit. (2 points)

Make certain that you leave a comment in canvas about this extra credit.

^ top

Grading Criteria (20)

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus.

Lesson Exercises

  • 2: All lesson exercises attempted and turned in
  • 1: Some lesson exercises completed and turned in
  • 0: No lesson exercises completed or turned in

Projects (x3)

  • 5: Demonstrates mastery of the program
    • Applies concepts from the lessons appropriately
    • Meets all specifications
    • Runs as required with no abnormal error conditions
    • Generates correct output given correct input
    • Correct file name
  • 4: Has most of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Attempts to meet all but one of the specifications
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 3: Has some of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Meets at least 1/2 of the specifications
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 2: Serious functional problems but shows some effort and understanding
    • Meets less than 1/2 of the of the specifications
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
  • 1: Does not compile or wrong file turned in
  • 0: Not turned in or not original work

Project Coding Style

  • 3: Code is well-documented and follows the specified coding styles
  • 2: Code has a minor documentation error
  • 1: Code has some documentation errors
  • 0: No apparent attempt to follow documentation standards or write documentation comments

Total possible: 20, plus extra credit

^ top

How to Submit

Upload all project files to Canvas in the project folder that matches the name of this project. Include the following items for grading:

  1. variable_delay.ino
  2. color_picker.ino
  3. color_organ.ino

You must submit all the files needed to complete your assignment. Your assignment must work as submitted. Remember to test and double check your files before submitting them. If you make a mistake, you can resubmit up to the deadline, but must resubmit all your assignment files.

^ top