Skip to main content

JavaScript


JavaScript

JavaScript is one of the core technologies used by the World Wide Web. 98% of all websites use JavaScript. Websites can now use the provided DUELink JavaScript library to access the physical world.

Here is a website demo to demonstrate how JavaScript can control actuators and read sensors: demo.duelink.com. It is covered in the Getting Started page.


Setup

This page assumes the user is already familiar with JavaScript and there is a development machine that is already setup to build and run JavaScript programs. We'll be running our program on a local machine using Node.js.

Install the duelink package:

npm install duelink

If using serialport, dlserialusb package is needed:

npm install dlserialusb
tip

Make sure your hardware is updated with the latest firmware. The Console can help with that!

Start a new project with a simple line of code to test that the project is running.

console.log("Hello World");

Blinky!

Our program will blink the on-board status LED, on for 200ms then it shuts off for 800ms, and does this 20 times. We will be using SerialUSB() here. If running on a website use WebSerial() instead.

const { SerialUSB } = require("dlserialusb");
const { DUELinkController } = require("duelink");

async function Blinky() {
let duelink = new DUELinkController(new SerialUSB());

await duelink.Connect();

// Flash the LED (on for 200ms, off for 800ms, 20 times)
await duelink.system.statled(200, 800, 20);
}

Blinky();
tip

You are accessing hardware in real-time. Things must run in order and using await is required with anything DUElink!

The JavaScript library is open source: DUELink Libraries Repo.