Arduino Sketch SoftBlink2

This sketch is used by Exercise: Soft Blink.

Full Source Code

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

// SoftBlink2 - 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.
//
// This builds upon the SoftBlink1 example to demonstrate more programming
// technique.  Please see SoftBlink1 for the basic commentary.

// ================================================================================
// Define the period of the PWM waveform in milliseconds.
const int PWMPERIOD = 10;

// ================================================================================
// Configure the hardware once after booting up.  This runs once after pressing
// reset or powering up the board.

void setup()
{
  // Initialize the LED on pin 13 for output.
  pinMode(LED_BUILTIN, OUTPUT);
}

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

void loop()
{
  // Perform a pattern of brightness ramps, both up and down and with different duration.
  // These use the LED_ramp() function defined below.

  // basic ramp up and down, full brightness
  LED_ramp( 1000,  0, 10 );
  LED_ramp( 1000, 10,  0 );

  // now in double-time
  LED_ramp(  500,  0, 10 );
  LED_ramp(  500, 10,  0 );
  LED_ramp(  500,  0, 10 );
  LED_ramp(  500, 10,  0 );

  // now in triple-time, using the loop to avoid repetition
  for (int i = 0; i < 4; i += 1) {
    LED_ramp(  333,  0, 10 );
    LED_ramp(  333, 10,  0 );
  }

  // back to a second, but dimmer
  for (int i = 0; i < 2; i += 1) {
    LED_ramp(  1000,  0,  4 );
    LED_ramp(  1000,  4,  0 );
  }

  // keep off for a second
  delay(1000);

  // and repeat
}

// ================================================================================
// Define a function to perform a single brightness ramp according to the
// specified parameters.  It will not return until the full ramp has been
// performed, i.e., it will delay for the full ramp duration.  This condenses
// the redundant code found in SoftBlink1 into a modular software building
// block.
//
//   duration - ramp duration in milliseconds
//   start    - initial pulse duration, in milliseconds, must be no greater than PWMPERIOD
//   end      - final pulse duration, in milliseconds, must be no greater than PWMPERIOD
//
// The basic logic of the loop is the same as in SoftBlink1.

void LED_ramp(int duration, int start, int end)
{
  int num_steps = duration / PWMPERIOD;
  
  for(int i = 0; i < num_steps; i = i + 1) {
    int time_on = map(i, 0, num_steps, start, end);
    int time_off = PWMPERIOD - time_on;

    digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on
    delay(time_on);                   // wait the specified number of milliseconds
    digitalWrite(LED_BUILTIN, LOW);   // turn the LED off
    delay(time_off);                  // wait the specified number of milliseconds
  }
}

// ================================================================================

Source: SoftBlink2 Arduino Sketch


About The Author

Muhammad Bilal

I am a highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Leave a Comment

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

Scroll to Top