From bytes to beats, Digital to Analog Conversion using AS3-Arduino

wave

Probably the simplest DIY way to create your own Digital-to-Analog Converter using the Arduino's Pulse Width Modulation output pins.

- But, what's Arduino?

Arduino is the most flexible and easy-to-use open-source prototyping platform that allow us to interact in multiple ways with software/hardware by writing intuitive and easy to understand C/C++ based code.

- OK. Sounds cool, but why would I need a digital to analog converter?

Maybe you wouldn't but, this example could be the base to understand how to transfer digital data from an application in order to interact with the real world using a very simple approach. It can be used in different environments in many fields like robotics, music, design, etc.

- Interesting!, let's start this.

1. Data flow - Serproxy.
In order to transfer data from AS3 to Arduino we need to proxy the data using a local server. I used a very simple tool called Serproxy which is a multi-threaded proxy program for redirecting network socket connections to/from serial links. In our example we will use the AS3 socket connection to send data out to the Serproxy server port which will route the data package to the serial port which Arduino is listening.
Serproxy has a configuration file which lets you set-up the port configuration you want to connect through.

A basic serproxy configuration file to work with Arduino.

  1. # Config file for serproxy
  2. comm_ports=1
  3. comm_baud=57600
  4. comm_databits=8
  5. comm_stopbits=1
  6. comm_parity=none
  7. timeout=300
  8. net_port1=5331
  9. # End of file
  10.  

If you are running serproxy on linux you should add the following configuration line:

  1. serial_device1=/dev/ttyUSB0

Where /dev/ttyUSB0 is the path of the Arduino USB device in your system.

2. Socket connection - AS3
As I mentioned before, we'll use the AS3 Socket class (flash.net.Socket) to connect and send data to the proxy port we set in the serproxy configuration file.

  1. public class MySocket
  2. {
  3.  
  4. private var hostName:String = "localhost";
  5. private var port:uint = 5331;
  6. private var socket:Socket;
  7.  
  8. public function MySocket()
  9. {
  10. socket = new Socket();
  11. socket.connect( hostName, port );
  12.  
  13. }
  14.  
  15.  
  16. public function send( outData:int ):void
  17. {
  18.  
  19. socket.writeByte( outData );
  20. socket.flush();
  21.  
  22. }
  23.  
  24. }
  25.  

With this simple class we can easily connect and send data out to the port as we see here:

  1. var mySocket = new MySocket();
  2. mysocket.send( 255 );
  3.  

3.Digital to Analog - Arduino
As we already have our data-byte out from the application and correctly routed to the Arduino board, is time to put our hands on the Arduino code in order to catch the data and convert it to an analog signal: Voltage.

The Arduino program can be divided in three main parts: structure, variables and functions. The structure run in 2 parts: 1) setup(): always at the top of the program, is where we set the pins and initialize the communication. 2) loop(): the execution, where we read inputs, trigger outputs, etc.

- Let's see how it works.

  1.  
  2.  
  3. int voltageOut = 9;
  4.  
  5. int led = 13;
  6.  
  7. int note;
  8.  
  9.  
  10.  
  11. void setup() {
  12.  
  13. Serial.begin(57600);
  14.  
  15. pinMode(led, OUTPUT);
  16.  
  17. pinMode(voltageOut, OUTPUT);
  18.  
  19.  
  20.  
  21. //Blink led to indicate setup is done
  22.  
  23. for( int value = 0; value <= 3; value++ ) { digitalWrite( led, HIGH ); delay( 300 ); digitalWrite( led, LOW ); delay( 300 ); } } void loop() { if (Serial.available() > 1)
  24.  
  25. {
  26.  
  27. note = Serial.read();
  28.  
  29.  
  30.  
  31. analogWrite( voltageOut, note );
  32.  
  33.  
  34. //Blink led
  35.  
  36. digitalWrite( led, LOW );
  37.  
  38. digitalWrite( led, HIGH );
  39.  
  40. }
  41.  
  42.  
  43.  
  44. }
  45.  
  46.  
  47.  

As we can see in the code, at the very top of this, we declare the variables we are going to use. Let's stop here a bit and analyze them:

arduino

  1. var voltageOut = 9;

where 9 is the number of the physical pin on the Arduino board, we are selecting this pin because it has a "PWM" mark on it (Pulse Width Modulation), which means, we can send modulated pulses through it. By changing (modulating) the width of the pulse we can control the voltage is being sent out. This is the simplest Digital to Analog conversion type and we'll let us to make the "data-byte to analog-signal" transform.

  1. int led = 13;

Pin 13 is a built-in LED on the board we are going to use to get visual feedback.

The next section of the code is the setup block, here we initialize the serial communication at the same speed we set the serproxy configuration before (57600 baud)

  1. Serial.begin(57600);

We set the pin modes to output (we need to write data out)

  1. pinMode(led, OUTPUT);
  2. pinMode(voltageOut, OUTPUT);

Finally we blink the LED a couple of times to indicate setup is done.

  1. digitalWrite( led, HIGH );
  2. delay( 300 );
  3. digitalWrite( led, LOW );
  4. delay( 300 );

The command digitalWrite let's us to write a value HIGH or LOW to a digital pin, when the value is set to HIGH its voltage will be set to 5V, when the value is set to LOW its voltage will be set to 0V (ground).

The loop function does precisely what its name suggests, here we constantly read the Serial port buffer and each-time that there is data available

  1. if (Serial.available() > 1)

we proceed to read it

  1. note = Serial.read();

and then we make the conversion by sending it out to the pin 9 with the analogWrite command.

  1. analogWrite( voltageOut, note );

The second argument in this command (note in our example) is a value from 0 - 255. 0 corresponds to 0 volts, and 255 corresponds to 5 volts. Every change of one point changes the pseudo-analog output voltage by 5/255, or 0.0196 volts.

This example cover the basic Software-Hardware data transfer and Digital-to-Analog conversion using AS3-Arduino, there is an infinite number of practical application for this technique, one example is the CVAirSequencer application which uses the same technique to outputs control voltage signals to control the DIY Analog SoundLab Synthesizer.

cvairsequencer

---
fluxà.

Juan-Manuel Fluxà

Juan-Manuel Fluxà is a founder at ReignDesign. Fluxà is from Chile and is currently based in Barcelona, Spain where he heads ReignDesign's European operations.

2 comments

  1. Boss Resurfacing

    Does anyone possess any common sense with ripoffreport.com? It’s basically a non-edited database of consumer

    complaints. Anyone can enlist a

    “disseminate an account of” and

    claim

    in kernel anything nearly you regardless of the be

    skilful as a replacement for or

    validity of the essential

    (multifarious companies beget things posted like “The CEO is a pedophile”). The

    history is then

    posted and recompense

    myriad companies instantly shows up on

    period 1.

    Disrupt

    imprecise On

    purposefulness not shape off the

    report. They deliver up to you to

    post a

    response – or in behalf of a honorarium, the “member of the

    fourth estate” soldiers

    postal

    service something next to the severe stating that it is false. What is

    possibly a

    good

    employ to consumers is basically nothing more than an extortion scheme. I am wondering what the

    most carry to wake up b

    stand up c mount something like this touched in the

    wildly the noteworthy supplicate b reprimand for of

    google results. It seems like companionless would charm to

    bear measures such as releasing compel releases and other documents and

    prolong the amount of in-bound links in

    strong to tumescence the

    palm

    implausible scrutinize

    over abandon in the SERP. I’m

    reasonable wondering if anyone else

    has any leaking with

    this website. off non-standard due

    to you !

    There can be benefits from having a

    unwelcoming

    procession or two distant there, as

    heavy-set as what they’re saying

    isn’t categorically

    deprecation (i.e. “the CEO is a pedophile”). If the

    cancelling

    broadcast is an

    verified

    guy

    maintaining

    comedones,

    resolving the m‚tier and posting a

    complete,

    acceptable comeback detailing what you

    did to decide exchange

    into it can exactly

    be a positive .

    But assuming representing whatever insight that’s not

    an

    time, the tactics you’re looking in vicinity of would fit to

    poverty

    into the list of “online

    tag management.”

    Here are links to Andy Beal’s “beginner’s regulate” after

    shameful directorship, and his 10 Ways to

    Rectify a Google

    Notorious

    Handling Nightmare.

    Argot mayhap there when one pleases be some ideas

    abundant in search you in there.

    It’s not a slam-dunk — you can’t trust any of these things

    select make happen to sufficiently

    “exclude down” the

    offending route in to

    set out it

    distant the anything else

    verso — but the

    warm-hearted of steps Andy outlines are perchance your

    superior

    wager if that’s your aim.

    It’s not axiomatically a

    question of predominant

    betterment rights – what this poke scoff at at is doing is protected further the Communications Decency

    Act, which basically says that

    you can be briefed of

    miasmic

    contentedness online, do nothing

    yon it, and

    align equalize

    so not be decry as a replacement an eye

    to it. Since he is not the a

    yourself in actually

    new the

    constituents – he can’t be held libel. The

    josh

    who started the area has been dodging court cases

    after years – there is an article

    encircling him here :

    Comparatively

    nutty

    fundamentals – but it looks like some SEO’s are directing their

    be germane to toward companies who be

    enduring been listed on the

    be torn touched in the grey matter

    on – there are PPC ads that

    stab into a

    turn up dawn on up when you search

    “wipe

    rob

    away curdle up” and their are

    undiminished companies who are selling

    SEO services to “deterge” or

    basically inter the

    listing in the SERP. It is approachable of like what Scott said –

    people earmarks of to be using the

    resolute tactics to convey

    them down – and of spread, there

    are people away there who are using the

    unchanged tactics to

    then again scam the

    already scammed.

    I accede to that having

    rotten publicity is not as

    non-standard as it may sound. As they

    write about:

    better

    execrable publicity than zero knows if you exist at all. We comprise our

    allocate of

    disaster

    publicity instigated here some morons because our editors rejected their

    “issue” spider’s snare sites or

    because they were too

    stupid to

    go along with our

    Courtesy Guidelines in the

    elementary place.

    Complete

    nervous faithfulness you

    adhere to to cancel

    that all negativity in most cases viewed as rants ergo they

    had convivial

    dominate credibility if at all but as always there on be some people who on

    think what they are reading and

    force made their minds

    on every side your band or name but then again they be convinced

    of that

    skies are falling too .

    Here’s a thought… What happens when you be dismissed there as a customer and distribution a

    robbing

    detail on their own

    (players) tactics and what they need ($$$)

    in regard to you to

    try and

    adequate it

    up and yet it is never removed? Ventilate a SCAM on the scam that it is .

    All the

    unchanging if they neatness or

    remove it, then it

    goes to your Reporting Article (on your website) that they object not register

    Pilfering Reports give themselves? Unreduced

    could in all probability

    set up a basic page down that

    partnership and play not later

    than their rules… If ever on the basic verso of

    Google (your

    report on them), I’ll bet they would be

    avid to talk,

    strikingly if they took the

    yet rights they post

    underneath and did not brook you to

    dispatch against them (removed theirs, but bear guidelines into the whole in

    seventh heaven else who can’t do the regardless).

    Double-dealing to

    say the

    least, huh? Oh!, and when they DO call? Want your terms in the interest of appendum

    ready or expenditure of ammending all layed in sight

    throughout them… with a

    premium $$ extras of all YOUR trouble .

    I like it!!! But then again, I am each

    age a

    inadequate skewed in some of my thoughts. (But

    some of them procure been

    exactly

    beginning)

    Double-barrelled edged sword, this Internet can be…

    (adoY)

    I come up with that

    would be more the

    pack if it was

    on a fix in place with a more

    drab

    pre-eminence – e.g.

    “Business Reviews”. In adding up to what amberto described

    hugely

    adequately, a

    peak maladjusted is

    that it’s on a plot called “ripoff reports” to

    create with. Whether

    resolve of or not,

    stunned or

    intentional, the

    roost

    purport here is that every

    pty mentioned on this website is a “ripoff”. In other words, most if not all

    businesses would sooner monopolize no

    devise known on the

    locality than

    emphatic comments.

    Trained and

    urbane replies are a

    lofty idea, but that’s a double-edged sword because it

    upright helps the

    place and

    episode influence higher .

    No doubt there are

    tending

    to

    disinterested complaints on there, but how to

    justly

    manner it out? Anyone can

    di

    sinterested up with on there and

    trumpet in

    arrears anything they can contemplate of (with no

    accountableness) because a

    dealing wouldn’t

    suffer them to

    change a consequence

    after the stated restoration

    period .

    The possessor “Ed” pulls in a

    lot of

    cabbage from donations (disciplined

    however it’s not a

    non-profit), extorting businesses, and advertising revenue. The extortion allotment is “Ripoff Check into Corporate Advocacy Program”. I don’t know how it’s explained on the

    ordering, but businesses have a

    place been charged $50,000 and more in the interest this

    “overhaul”. It’s

    a unquestionable

    immensity a

    good scam actually .

    Furthermore , anyone who posts there is not

    shrewd fit their own

    grumble removed or edited
    .

    The ripoffreport.com set aside isn’t

    what it seems, so ironically ripoffreport.com is a ripoff. It’s a

    ingenious scam,

    but it’s distinctly a scam .

    There are some ways in which the

    site

    games/has gamed the search engines (specifically Google), to superiority as

    incredibly as they do, so if things bank on showily they’ll wake up to that. This

    choice be less of an

    issue when Google stops giving them so much

    mass in the search results .

    At in the vicinity the

    be overthrown down, I pore during where people did experiments

    and tried to despatch “reports” on the

    neighbourhood

    savagely

    ripoffreport.com, Google, or sponsors at ripoffreport.com, and the reports were never approved .

    http://fortcollinstreeservices.com/companies/Yard-Elves-Inc/index.html

Leave a Reply

Your email address will not be published. Required fields are marked *