This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Fade.js
53 lines (43 loc) · 1.54 KB
/
Fade.js
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
// Copyright (c) 2016, Intel Corporation.
// Reimplementation of Arduino - Basics - Fade example
// - Fades LEDs in and out using PWM
// - IO3 uses normal polarity, IO5 uses reverse polarity, so they alternate
// Hardware Requirements:
// - One or two LEDs with resistors
// Wiring:
// For each external LED:
// - Wire its long lead to the PWM pin you choose below (IO3/IO5 default)
// - Wire its short lead to one end of a resistor
// - Wire the other end of the resistor to ground
// Note: For a completely safe resistor size, find the LED's actual forward
// voltage (or lowest reported), subtract from 3.3V, and divide by the
// desired current. For example:
// (3.3V - 1.8V) / 20 mA = 1.5 V / 0.02 A = 75 Ohms.
// Larger resistors will make the LED dimmer. Smaller ones could reduce its
// life.
console.log("Starting Fade example...");
var brightness = 0;
var fadeAmount = 5;
var delay = 30; // time between adjustments (in ms)
// import pwm module
var pwm = require("pwm");
var pins = require("arduino101_pins");
var led1 = pwm.open({
channel: pins.IO3
});
led1.setPeriodCycles(256);
var led2 = pwm.open({
channel: pins.IO5,
polarity: "reverse"
});
led2.setPeriodCycles(256);
// update the brightness every 30ms
setInterval(function () {
led1.setPulseWidthCycles(brightness);
led2.setPulseWidthCycles(brightness);
// adjust the brightness for next time
brightness += fadeAmount;
// reverse fade direction
if (brightness == 0 || brightness == 255)
fadeAmount = -fadeAmount;
}, delay);