Night Light Mode: Selecting the Modes

Home

Current Objective: Select the Light Sculpture Modes

Requirements

  1. Ensure you understand each step before moving on.
  2. Write code for each step as you go.
  3. Cleanly format your code.
  4. Test your code at the end of each page.

Selecting the Modes

Single-line if statements are useful for writing clean code. Remember the Golden Rule:

if (codeAppearance == junk) code = junk;

Switches such as the switchPin need a default state, so we use INPUT_PULLUP to make the default HIGH with an internal pull-up resistor.

In the definition of setup:

  1. Set the pinMode of switchPin to INPUT_PULLUP with the statement

In the definition of loop:

  1. Replace the call to pwmUsingKnob with a call to updateMode with the statement
  2. Use a single-line if statement to call turnOffAll if the mode equals offMode with the statement
  3. Use a single-line if statement to call runLightShow if the mode equals lightShowMode with the statement
  4. Use a single-line if statement to call runNightLight if the mode equals nightLightMode with the statement

Under a new Section Header named Mode Selection:

  1. Open the definition of updateMode with the statement
    1. Call the function updateKnobValue with the statement
    2. Call the function updateOffSwitch with the statement
    3. Assign mode to be offMode if switchIsOff with the statement
    4. else if knobValue is less than nightLightMinValue, assign mode to be lightShowMode with the statement
    5. else assign mode to be nightLightMode with the statement
  2. Open the definition of updateOffSwitch with the statement
    1. Assign the switchIsOff variable to be the return value of using digitalRead on switchPin with the statement
  3. Open the definition of runLightShow with the statement
    1. Call the function turnOnAll with the statement
  4. Open the definition of runNightLight with the statement
    1. Call the function pwmUsingKnob with the statement

Make sure you test your code before continuing!

Back Next Page