Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code update for Node.js 6 capabilities: Tutorials/Pulse_Width_Modulation.md #159

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 42 additions & 29 deletions Tutorials/Pulse_Width_Modulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,67 @@ In the following example, the Tessel will change the color of an RGB LED by chan

![PWM RGB LED Circuit](https://s3.amazonaws.com/technicalmachine-assets/tutorials/hardware-api/pwm-rgb-circuit.png)

```js
var tessel = require('tessel'); // Import tessel

var portA = tessel.port.A; // Select port A
var portB = tessel.port.B; // Select port B

var redPin = portA.pwm[0]; // Select the first PWM pin on port A, equivalent to portA.pin[5]
var greenPin = portA.pwm[1];
var bluePin = portB.pwm[0];
```js
const tessel = require('tessel');

// Unpack and name pwm pins from port A
const [ red, green ] = tessel.port.A.pwm;
// Unpack and name pwm pin from port B
const [ blue ] = tessel.port.B.pwm;

// The starting values of each color out of 255
var red = 0;
var green = 0;
var blue = 0;
red.value = 0;
green.value = 0;
blue.value = 0;

// Use this to increment the color value without exceeding 255
function stepColor (value, step) {
value += step; // Add the step count to the existing value
function stepColor(led, step) {
led.value += step; // Add the step count to the existing value

// If the value exceeds 255, then reset it to 0
if (value > 255) {
value = 0;
if (led.value > 255) {
led.value = 0;
}

return value;
// return a fractional value
return led.value / 255;
}

// Set the signal frequency to 1000 Hz, or 1000 cycles per second
// This the rate at which Tessel will send the PWM signals
// This the rate at which Tessel will send PWM signals
// This is program specific
tessel.pwmFrequency(1000);

// Create a loop to run a function at a set interval of time
setinterval(function() {
// Increment each color at a unique step
red = stepColor(red, 10);
bluE = stepColor(blue, 5);
green = stepColor(green, 20);

// Set how often each pin is turned on out of 100%
// Divide the value by 255 to get a value between 0 and 1
redPin.pwmDutyCycle(red / 255);
greenPin.pwmDutyCycle(blue / 255);
bluePin.pwmDutyCycle(green / 255);
// Create an interval to run a function which sets the color of all three LEDs
setInterval(() => {
// Increment each LED color at a unique step
red.pwmDutyCycle(stepColor(red, 10));
green.pwmDutyCycle(stepColor(green, 5));
blue.pwmDutyCycle(stepColor(blue, 20));
}, 500); // Set this function to be called every 500 milliseconds, or every half a second
```

Note: the `pwmFrequency` function *must* be called before `pwmDutyCycle`. Re-setting
`pwmFrequency` will disable PWM output until `pwmDutyCycle` is called again.

[More information on pulse-width modulation.](https://learn.sparkfun.com/tutorials/pulse-width-modulation)

--------------------------------------------------------------

If you're Tessel 2 is up-to-date, it will be be running Node.js 6.10.3 or newer. You can check that by running `t2 version` and comparing the output to the following:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: you're > your

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to tie ourselves to listing a specific version here (which will need to be updated?) Is there a future-looking way to accomplish both this and tessel/tessel.io#117


```
$ t2 version
INFO Looking for your Tessel...
INFO Connected to maria.
INFO Tessel Environment Versions:
INFO t2-cli: 0.1.5
INFO t2-firmware: 0.1.0
INFO Node.js: 6.10.3
```

If your output doesn't match, you should run `t2 update` to get the latest OS and firmware. Once that's done, you're ready to run the program in this tutorial.


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HipsterBrown I've added this note about updating Tessel 2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwaldron Should this be added to every tutorial page?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe adding one line comment at the start of code block inform users, like this:

// Compatible firmware version : greater than 0.1.0.