SoftBlink2 Arduino Sketch

This sketch is used by Exercise: Soft Blink.

Full Source Code

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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
  }
}

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