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// SoftBlink2 - fades the onboard LED on and off.
2//
3// Copyright (c) 2016, Garth Zeglin. All rights reserved. Licensed under the
4// terms of the BSD 3-clause license as included in LICENSE.
5//
6// This builds upon the SoftBlink1 example to demonstrate more programming
7// technique. Please see SoftBlink1 for the basic commentary.
8
9// ================================================================================
10// Define the period of the PWM waveform in milliseconds.
11const int PWMPERIOD = 10;
12
13// ================================================================================
14// Configure the hardware once after booting up. This runs once after pressing
15// reset or powering up the board.
16
17void setup()
18{
19 // Initialize the LED on pin 13 for output.
20 pinMode(LED_BUILTIN, OUTPUT);
21}
22
23// ================================================================================
24// Run one iteration of the main event loop. The Arduino system will call this
25// function over and over forever.
26
27void loop()
28{
29 // Perform a pattern of brightness ramps, both up and down and with different duration.
30 // These use the LED_ramp() function defined below.
31
32 // basic ramp up and down, full brightness
33 LED_ramp( 1000, 0, 10 );
34 LED_ramp( 1000, 10, 0 );
35
36 // now in double-time
37 LED_ramp( 500, 0, 10 );
38 LED_ramp( 500, 10, 0 );
39 LED_ramp( 500, 0, 10 );
40 LED_ramp( 500, 10, 0 );
41
42 // now in triple-time, using the loop to avoid repetition
43 for (int i = 0; i < 4; i += 1) {
44 LED_ramp( 333, 0, 10 );
45 LED_ramp( 333, 10, 0 );
46 }
47
48 // back to a second, but dimmer
49 for (int i = 0; i < 2; i += 1) {
50 LED_ramp( 1000, 0, 4 );
51 LED_ramp( 1000, 4, 0 );
52 }
53
54 // keep off for a second
55 delay(1000);
56
57 // and repeat
58}
59
60// ================================================================================
61// Define a function to perform a single brightness ramp according to the
62// specified parameters. It will not return until the full ramp has been
63// performed, i.e., it will delay for the full ramp duration. This condenses
64// the redundant code found in SoftBlink1 into a modular software building
65// block.
66//
67// duration - ramp duration in milliseconds
68// start - initial pulse duration, in milliseconds, must be no greater than PWMPERIOD
69// end - final pulse duration, in milliseconds, must be no greater than PWMPERIOD
70//
71// The basic logic of the loop is the same as in SoftBlink1.
72
73void LED_ramp(int duration, int start, int end)
74{
75 int num_steps = duration / PWMPERIOD;
76
77 for(int i = 0; i < num_steps; i = i + 1) {
78 int time_on = map(i, 0, num_steps, start, end);
79 int time_off = PWMPERIOD - time_on;
80
81 digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
82 delay(time_on); // wait the specified number of milliseconds
83 digitalWrite(LED_BUILTIN, LOW); // turn the LED off
84 delay(time_off); // wait the specified number of milliseconds
85 }
86}
87
88// ================================================================================