@@ -9,49 +9,42 @@ In the following example, the Tessel will change the color of an RGB LED by chan
99![ PWM RGB LED Circuit] ( https://s3.amazonaws.com/technicalmachine-assets/tutorials/hardware-api/pwm-rgb-circuit.png )
1010
1111``` js
12- var tessel = require (' tessel' ); // Import tessel
12+ const tessel = require (' tessel' );
1313
14- var portA = tessel .port .A ; // Select port A
15- var portB = tessel .port .B ; // Select port B
16-
17- var redPin = portA .pwm [0 ]; // Select the first PWM pin on port A, equivalent to portA.pin[5]
18- var greenPin = portA .pwm [1 ];
19- var bluePin = portB .pwm [0 ];
14+ // Unpack and name pwm pins from port A
15+ const [ red , green ] = tessel .port .A .pwm ;
16+ // Unpack and name pwm pin from port B
17+ const [ blue ] = tessel .port .B .pwm ;
2018
2119// The starting values of each color out of 255
22- var red = 0 ;
23- var green = 0 ;
24- var blue = 0 ;
20+ red . value = 0 ;
21+ green . value = 0 ;
22+ blue . value = 0 ;
2523
2624// Use this to increment the color value without exceeding 255
27- function stepColor ( value , step ) {
28- value += step; // Add the step count to the existing value
29-
25+ function stepColor ( led , step ) {
26+ led . value += step; // Add the step count to the existing value
27+
3028 // If the value exceeds 255, then reset it to 0
31- if (value > 255 ) {
32- value = 0 ;
29+ if (led . value > 255 ) {
30+ led . value = 0 ;
3331 }
3432
35- return value;
33+ // return a fractional value
34+ return led .value / 255 ;
3635}
3736
3837// Set the signal frequency to 1000 Hz, or 1000 cycles per second
39- // This the rate at which Tessel will send the PWM signals
38+ // This the rate at which Tessel will send PWM signals
4039// This is program specific
4140tessel .pwmFrequency (1000 );
4241
43- // Create a loop to run a function at a set interval of time
44- setinterval (function () {
45- // Increment each color at a unique step
46- red = stepColor (red, 10 );
47- bluE = stepColor (blue, 5 );
48- green = stepColor (green, 20 );
49-
50- // Set how often each pin is turned on out of 100%
51- // Divide the value by 255 to get a value between 0 and 1
52- redPin .pwmDutyCycle (red / 255 );
53- greenPin .pwmDutyCycle (blue / 255 );
54- bluePin .pwmDutyCycle (green / 255 );
42+ // Create an interval to run a function which sets the color of all three LEDs
43+ setInterval (() => {
44+ // Increment each LED color at a unique step
45+ red .pwmDutyCycle (stepColor (red, 10 ));
46+ green .pwmDutyCycle (stepColor (green, 5 ));
47+ blue .pwmDutyCycle (stepColor (blue, 20 ));
5548}, 500 ); // Set this function to be called every 500 milliseconds, or every half a second
5649```
5750
0 commit comments