Arduino Sketch SoftBlink1

Arduino Sketch SoftBlink1 (1)

This sketch is used by Exercise: Soft Blink.

Full Source Code

The full code is all in one file SoftBlink1.ino.

Arduino Sketch SoftBlink1 (1)

// SoftBlink1 - fades the onboard LED on and off.
//
// Copyright (c) 2016, Garth Zeglin.  All rights reserved. Licensed under the
// terms of the BSD 3-clause license as included in LICENSE.
//
// The Arduino UNO we use has an onboard LED on pin 13.  This sketch varies the
// visible LED intensity using a form of "pulse width modulation" (PWM)
// implemented in software.  The example turns the LED on and off very fast
// using short delays within a loop; the average energy emitted is lower than
// constantly being ON.
//
// The output voltage signal looks something like this as a function of time:
//
//      ----      ----      ----     ----
//     |    |    |    |    |    |   | 
//  ---      ----      ----      ----
//
//  For even dimmer output, the proportion of 'ON' time can be reduced:
//
//      --        --        --       --
//     |  |      |  |      |  |     | 
//  ---    ------    ------    -----
//
// This sketch also introduces several conventional programming structures.
//
// ================================================================================
// Configure the hardware once after booting up.  This runs once after pressing
// reset or powering up the board.

void setup()
{
  // Initialize the hardware digital pin 13 as an output.  The 'OUTPUT' symbol
  // is pre-defined by the Arduino system.
  pinMode(LED_BUILTIN, OUTPUT);
}

// ================================================================================
// Define constant values.  The following lines defines "pre-processor macros"
// which can be used to replace one text symbol with a value as the first step
// in compiling the code.  In general, this a recommended practice for making
// the intent of the code more legible.

// Define the period of the PWM waveform in milliseconds.  E.g., the text
// 'PWMPERIOD' will be replaced by the text '10' in the code which follows.
const int PWMPERIOD = 10;

// Define the duration of each fade ramp in millisconds.
const int RAMPPERIOD = 1000;

// Note that constant values can include expressions.  The following line
// determines how many discrete steps will be required to complete the ramp in
// approximately the specified time:
const int RAMPSTEPS = RAMPPERIOD/PWMPERIOD;

// ================================================================================
// Run one iteration of the main event loop.  The Arduino system will call this
// function over and over forever.

Read more: Arduino Sketch SoftBlink1

Leave a Comment

Your email address will not be published. Required fields are marked *