Arduino.js prototype (JavaScript + Arduino interface)

The Hallowe’en season set the theme for this demo, of which I had a larger-scale variant running black lights and floodlamps for a party last weekend. In case it isn’t obvious, this is going to get really nerdy. Ready? Go.

In brief:

JavaScript is controlling volume and pan of music (via SoundManager 2) based on live analog data from potentiometers connected to an Arduino device. Canvas-based graphs at left show the data coming in from the pots (ranging from 0-1023); as an example of writing to the device, LED lights respond to music frequencies as reported from JavaScript.

Nerdier:

I’ve been tinkering with a JavaScript wrapper for the “AS3Glue” Flash library in order to get a JS-based API for talking to Arduino devices. The JS talks to Flash, which talks to a socket/serial proxy program running on the host computer, which talks to the Arduino device over USB that is running “Standard Firmata” controller code. It’s a slightly Rube Goldberg-esque technology stack in order to pass bits back and forth, but it works. (TODO: Look at using WebSockets, which can eliminate the Flash layer.)

Analog and digital input pins can use onRecieveData-style event listeners, which is much more efficient than a polling-based approach; thus, you can be notified when a pin goes high or when an analog input changes. (The latter is always shifting slightly due to its nature, of course.)

Basic spectrum analysis data comes from SoundManager 2, and this is fed into a digital output for blinking one LED roughly in step with the bass line, and another PWM-style output (varying output voltages, thus varying LED brightness) with the rhythm.

I couldn’t be arsed to really sync the video with the screencast bit, so the LEDs look like they’re blinking a bit out-of-step.

Setup: Browser-based control, JavaScript <-> Flash 9 <-> Socket/serial proxy (“Processing” IDE running SerialNet example script at 115200 baud) <-> Arduino Uno (USB) device with “Standard Firmata” 2.0 + prototyping breadboard, LEDs etc.

I am running at an interval of ~25 fps here for updating the LEDs, and the socket/serial proxy + Arduino seem to be able to handle the data rate generated from this without issue.

A small code example (JavaScript):

arduino.onAnalogReceiveData = function(pinNumber, value) {
// we got some data from one of the pots connected to the Arduino
// and it will be between 0 and 1023
if (pinNumber === 1) {
var volume = parseInt(value/1023*100);
soundManager.setVolume(mySound, volume);
}
}

You can also write to digital output pins.

// Somewhat-related example: blink an LED on pin 13 using writeDigitalPin()

function blink(pin, value, delay) {
arduino.writeDigitalPin(pin, value);
setTimeout(function() {
blink(pin, !value, delay); // wait “delay” msec, then repeat
}, delay);
}

blink(13, 1, 1000);

via: schillmania.com

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s