Skip to content

Instantly share code, notes, and snippets.

@rtreffer
Created October 12, 2013 09:01
Show Gist options
  • Save rtreffer/6947685 to your computer and use it in GitHub Desktop.
Save rtreffer/6947685 to your computer and use it in GitHub Desktop.
fade 3 leds with bonescript
var b = require('bonescript');
// setup starting conditions
var awStep1 = (Math.random() + 1) * 0.01;
var awValue1 = awStep1;
var awDirection1 = 1;
var awPin1 = "P9_14";
var awStep2 = awStep1;
while (Math.abs(awStep2 - awStep1) < 0.002) {
awStep2 = (Math.random() + 1) * 0.01;
}
var awValue2 = awStep2 * 10;
var awDirection2 = 1;
var awPin2 = "P9_21";
var awStep3 = awStep1;
while (Math.abs(awStep3 - awStep1) < 0.002 || Math.abs(awStep3 - awStep2) < 0.002) {
awStep3 = (Math.random() + 1) * 0.01;
}
var awValue3 = awStep3 * 20;
var awDirection3 = 1;
var awPin3 = "P9_22";
// configure pin
b.pinMode(awPin1, b.OUTPUT);
b.pinMode(awPin2, b.OUTPUT);
b.pinMode(awPin3, b.OUTPUT);
// call function to update brightness every 10ms
setInterval(fade, 10);
// function to update brightness
function fade() {
b.analogWrite(awPin1, awValue1);
awValue1 = awValue1 + (awDirection1*awStep1);
if(awValue1 > 1.0) { awValue1 = 1.0; awDirection1 = -1; }
else if(awValue1 <= awStep1) { awValue1 = awStep1; awDirection1 = 1; }
b.analogWrite(awPin2, awValue2);
awValue2 = awValue2 + (awDirection2*awStep2);
if(awValue2 > 1.0) { awValue2 = 1.0; awDirection2 = -1; }
else if(awValue2 <= awStep2) { awValue2 = awStep2; awDirection2 = 1; }
b.analogWrite(awPin3, awValue3);
awValue3 = awValue3 + (awDirection3*awStep1);
if(awValue3 > 1.0) { awValue3 = 1.0; awDirection3 = -1; }
else if(awValue3 <= awStep3) { awValue3 = awStep3; awDirection3 = 1; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment