Skip to content

Instantly share code, notes, and snippets.

@ButchAnton
Created July 23, 2014 23:13
Show Gist options
  • Save ButchAnton/c8f28ce18504c9195dd4 to your computer and use it in GitHub Desktop.
Save ButchAnton/c8f28ce18504c9195dd4 to your computer and use it in GitHub Desktop.
var b = require('bonescript');
//
// Set up the starting conditions
var awPin = ["P8_13", "P9_14", "P9_21"]; // these are PWM output pins
var awValue = [0.01, 0.99, 0.50]; // starting values for the pins - 0 < pin < 1.0
var awDirection = [1, - 1, - 1]; // used to set ascending or descending values
var awStep = [0.01, 0.005, 0.015]; // step sizes for PWM change in each loop
//
// Configure pins
for (var i = 0; i < 3; i++) {
b.pinMode(awPin[i], b.OUTPUT);
//
// Call the function to perform the fade every 10 ms
setInterval(fade, 10);
//
// Function to fade the RGB LED
function fade() {
for (var i = 0; i < 3; i++) {
b.analogWrite(awPin[i], awValue[i]); // set the pin value
awValue[i] += (awDirection[i] * awStep[i]);
if (awValue[i] > 1.0) {
awValue[i] = 1.0;
awDirection[i] = -1;
}
if (awValue[i] < 0.01) {
awValue[i] = 0.01;
awDirection[i] = 1;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment