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
}
Read more: SoftBlink2 Arduino Sketch
