SoftBlink1 Arduino Sketch¶
This sketch is used by Exercise: Soft Blink.
Full Source Code¶
The full code is all in one file SoftBlink1.ino.
1// SoftBlink1 - 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// The Arduino UNO we use has an onboard LED on pin 13. This sketch varies the
7// visible LED intensity using a form of "pulse width modulation" (PWM)
8// implemented in software. The example turns the LED on and off very fast
9// using short delays within a loop; the average energy emitted is lower than
10// constantly being ON.
11//
12// The output voltage signal looks something like this as a function of time:
13//
14// ---- ---- ---- ----
15// | | | | | | |
16// --- ---- ---- ----
17//
18// For even dimmer output, the proportion of 'ON' time can be reduced:
19//
20// -- -- -- --
21// | | | | | | |
22// --- ------ ------ -----
23//
24// This sketch also introduces several conventional programming structures.
25//
26// ================================================================================
27// Configure the hardware once after booting up. This runs once after pressing
28// reset or powering up the board.
29
30void setup()
31{
32 // Initialize the hardware digital pin 13 as an output. The 'OUTPUT' symbol
33 // is pre-defined by the Arduino system.
34 pinMode(LED_BUILTIN, OUTPUT);
35}
36
37// ================================================================================
38// Define constant values. The following lines defines "pre-processor macros"
39// which can be used to replace one text symbol with a value as the first step
40// in compiling the code. In general, this a recommended practice for making
41// the intent of the code more legible.
42
43// Define the period of the PWM waveform in milliseconds. E.g., the text
44// 'PWMPERIOD' will be replaced by the text '10' in the code which follows.
45const int PWMPERIOD = 10;
46
47// Define the duration of each fade ramp in millisconds.
48const int RAMPPERIOD = 1000;
49
50// Note that constant values can include expressions. The following line
51// determines how many discrete steps will be required to complete the ramp in
52// approximately the specified time:
53const int RAMPSTEPS = RAMPPERIOD/PWMPERIOD;
54
55// ================================================================================
56// Run one iteration of the main event loop. The Arduino system will call this
57// function over and over forever.
58
59void loop()
60{
61 // Ramp from off to on. The next code line begins a 'for loop' which will
62 // iterate the block delineated by curly braces, with details as follows:
63 //
64 // int i = 0; Declare a new integer variable named 'i' and assign it an initial value of zero.
65 // i < RAMPSTEPS; This condition determines whether to keep iterating.
66 // i = i+1 Increment the value of i after the completion of each iteration.
67 //
68 // The net effect is that the loop with execute RAMPSTEPS times, with the
69 // value of i counting up on each iteration: 0, 1, 2, ... On the last
70 // iteration i will be equal to RAMPSTEPS-1.
71
72 for(int i = 0; i < RAMPSTEPS; i = i+1) {
73
74 // Declare a new local integer value to hold the duration of time the LED should remain on.
75 // This uses the highly convenient map() function which remaps a value from one range to another.
76 // i has values over range (0,RAMPSTEPS) which are remapped to the range (0,PWMPERIOD).
77 int time_on = map(i, 0, RAMPSTEPS, 0, PWMPERIOD);
78
79 // Similarly for the off time. Note that (time_on + time_off) always equals PWMPERIOD.
80 int time_off = PWMPERIOD - time_on;
81
82 digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
83 delay(time_on); // wait the specified number of milliseconds
84 digitalWrite(LED_BUILTIN, LOW); // turn the LED off
85 delay(time_off); // wait the specified number of milliseconds
86 }
87
88 // Ramp from on to off. Note the code is nearly identical; the only
89 // difference is that the roles of time_on and time_off are switched.
90
91 for(int i = 0; i < RAMPSTEPS; i++) {
92 int time_off = (i * PWMPERIOD) / RAMPSTEPS; // equivalent to map() above
93 int time_on = PWMPERIOD - time_off;
94
95 digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
96 delay(time_on);
97 digitalWrite(LED_BUILTIN, LOW); // turn the LED off
98 delay(time_off);
99 }
100
101 // After this event loop iteration exits, the loop() function will be immediately called again.
102}
103// ================================================================================