Loading...
Searching...
No Matches
LedFade.ino

Fades the green LED (D1) in and out with PWM while the red LED (D2) does the opposite, so one brightens as the other dims. One full cycle, in and out, takes about 2 s.

Jumpers
JP3 at 3.3 V.
Output
The LEDs only; nothing is printed.
Notes
Both LEDs are active low: the anode goes to VDD and the pin sinks the current, so a lower analogWrite() value gives a brighter LED. setBrightness() writes 255 - brightness, so 0 is off and 255 is full brightness.
/*
* LedFade
* Fades the green LED (D1) in and out with PWM while the red LED (D2) does
* the opposite, so one brightens as the other dims. One full cycle, in and
* out, takes about 2 s.
*
* Jumpers: JP3 at 3.3 V.
* Output: the LEDs only; nothing is printed.
*
* Both LEDs are active low: the anode goes to VDD and the pin sinks the
* current, so a lower analogWrite() value gives a brighter LED. setBrightness()
* writes 255 - brightness, so 0 is off and 255 is full brightness.
*/
#include <AnalogShield.h>
using namespace AnalogShieldPins;
static AnalogShield Shield;
constexpr uint32_t STEP_MS = 4; // time per brightness step; 2 × 256 steps ≈ 2 s
// Sets an active-low LED from 0 (off) to 255 (full brightness)
void setBrightness(uint8_t pin, uint8_t brightness) {
analogWrite(pin, 255 - brightness);
}
void setup() {
Shield.begin();
}
void loop() {
// D1 fades in, D2 fades out
for (int brightness = 0; brightness <= 255; ++brightness) {
setBrightness(LED1, brightness);
setBrightness(LED2, 255 - brightness);
delay(STEP_MS);
}
// D1 fades out, D2 fades in
for (int brightness = 255; brightness >= 0; --brightness) {
setBrightness(LED1, brightness);
setBrightness(LED2, 255 - brightness);
delay(STEP_MS);
}
}
Library for the U of T Physics Analog Shield (Introduction to Electronics).
Encoder, switch, temperature signals, potentiometer and LEDs on the Analog Shield.
Definition AnalogShield.h:64
void begin()
Sets pin modes, turns both LEDs off and starts tracking the encoder and its switch.
Definition AnalogShield.cpp:56
Shield signals by Arduino pin, from the Rev-C schematic.
Definition AnalogShield.h:26
constexpr uint8_t LED1
D1 green LED, active low (anode to VDD).
Definition AnalogShield.h:44
constexpr uint8_t LED2
D2 red LED, active low (anode to VDD).
Definition AnalogShield.h:43