arduinOSC first release

Unfiltered PWM and the filter output (440 Hz sine wave)
arduinOSC is a library which transforms your arduino into a simple arbitrary waveform audio oscillator.
It is based upon the internal 16-bit programmable timer and generates audio using pulse width modulation (PWM) and an external low-pass filter (square waves don't even need the external filter).
I'm using a simple passive low-pass filter, but in the next days I'll try an active low-pass filter based solution.
The library includes a sine wave lookup table (LUT) and a user-definable lookup table which can be found in the file named "user_waveform.h" under arduinOSC folder: if you whish to change this waveform just edit the file and then delete "arduinOSC.o", so the next time you will compile your sketch the library will be rebuilt.
Square waves are generated without a LUT, and in the next version also triangle, ascending sawtooth and descending sawtooth will be generated in this way, so the LUT will be used just for sine and user defined waveforms.
So far the library has been tested with Arduino Nano and Arduino Diecimila with the new ATMega328P.
Waveforms:
  • Sine (WAVEFORM_SINE)
  • Square (WAVEFORM_SQUARE)
  • User Defined (WAVEFORM_USER)

You can use the setOSCFrequency function to configure the waveform frequency, either by entering a float value (eg. arduinOSC::setOSCFrequency(354.54)  ) or a NOTE constant as shown below:

NOTE constants (change "x" with the octave, ranging from 0 to 8):
  • Cx (NOTE_Cx)
  • Cx# / Dxb (NOTE_CSx)
  • Dx (NOTE_Dx)
  • Dx# / Exb (NOTE_DSx)
  • Ex (NOTE_Ex)
  • Fx (NOTE_Fx)
  • Fx# / Gxb (NOTE_FSx)
  • Gx (NOTE_Gx)
  • Gx# / Axb (NOTE_GSx)
  • Ax (NOTE_Ax)
  • Ax# / Bxb (NOTE_ASx)
  • Bx (NOTE_Bx)

In order to build the examples below, just download the library and unzip its content into /arduino-0xx/hardware/library/ folder, then build your sketch and connect a mono audio jack to arduino digital pin #9 and arduino GND.

#include <arduinosc.h>

void setup(void){
  arduinOSC::initOSC(WAVEFORM_SINE);
  arduinOSC::setOSCFrequency(NOTE_A4);
}

void loop(){

}


#include <arduinosc.h>

int frequency = 100;
int startFrequency = 200;
int stopFrequency = 2000;
int maxDist = 500;
int minDist = 80;
int distance = 0;
int distanceMap = 0;
int frequencyStep = 10;

bool increasing = true;

void setup(void){
  arduinOSC::initOSC(WAVEFORM_SQUARE);
}

void loop(){
  distance = analogRead(0);
  if(distance>maxDist)maxDist = distance;
  if(distance   stopFrequency = map(distance,minDist,maxDist,2000,220);
  if(stopFrequency>1600){
     arduinOSC::setOSCFrequency(0);
  }
  else
  {
      arduinOSC::setOSCFrequency(frequency);
    
      for(int i=0;i<1000;i++){
        __asm__("nop");
      }
      if(frequency>stopFrequency&&increasing)increasing=false;
      if(frequency       if(!increasing) frequency-=frequencyStep;
      else frequency+=frequencyStep;
  }
}

comments powered by Disqus