You are on page 1of 7

/* Example playing a sinewave at a set frequency,

using Mozzi sonification library.

Demonstrates the use of Oscil to play a wavetable.

Circuit: Audio output on digital pin 9 on a Uno or similar, or

DAC/A14 on Teensy 3.1, or

check the README or http://sensorium.github.com/Mozzi/

Mozzi help/discussion/announcements:

https://groups.google.com/forum/#!forum/mozzi-users

Tim Barrass 2012, CC by-nc-sa.

*/

#include <MozziGuts.h>

#include <Oscil.h> // oscillator template

#include <tables/sin2048_int8.h> // sine table for oscillator

const int trigPin = 7;

const int echoPin = 8;

// defines variables

long duration;

int distance;

float FTHREE = 174.61;

float FSHARPTHREE = 185.00;

float GTHREE = 196.00;

float GSHARPTHREE = 207.65;


float ATHREE = 220.00;

float ASHARPTHREE = 233.08;

float BTHREE = 246.94;

float CFOUR = 261.63;

float CSHARPFOUR = 277.18;

float DFOUR = 293.67;

float DSHARPFOUR = 311.13;

float EFOUR = 329.63;

float FFOUR = 349.23;

float FSHARPFOUR = 369.99;

float GFOUR = 392.00;

float GSHARPFOUR = 415.30;

float AFOUR = 440.00;

float ASHARPFOUR = 466.16;

float BFOUR = 493.88;

float CFIVE = 523.25;

float CSHARPFIVE = 554.37;

float DFIVE = 587.33;

float DSHARPFIVE = 622.25;

float EFIVE = 659.26;

float FFIVE = 693.46;

// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included
above

Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);

// use #define for CONTROL_RATE, not a constant


#define CONTROL_RATE 128 // powers of 2 please

void setup(){

startMozzi(CONTROL_RATE); // set a control rate of 64 (powers of 2 please)

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(9600); // Starts the serial communication

void updateControl(){

// put changing controls in here

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance= duration*0.034/2;

if (distance> 4 && distance< 6){


aSin.setFreq(FTHREE);

else if(distance> 6 && distance< 8){

aSin.setFreq(FSHARPTHREE);

else if(distance> 8 && distance< 10){

aSin.setFreq(GTHREE);

else if(distance> 10 && distance< 12){

aSin.setFreq(GSHARPTHREE);

else if(distance> 12 && distance< 14){

aSin.setFreq(ATHREE);

else if(distance> 14 && distance< 16){

aSin.setFreq(ASHARPTHREE);

else if(distance> 16 && distance< 18){

aSin.setFreq(BTHREE);

else if(distance> 18 && distance< 20){

aSin.setFreq(CFOUR);

else if(distance> 20 && distance< 22){

aSin.setFreq(CSHARPFOUR);

else if(distance> 22 && distance< 24){


aSin.setFreq(DFOUR);

else if(distance> 24 && distance< 26){

aSin.setFreq(DSHARPFOUR);

else if(distance> 26 && distance< 28){

aSin.setFreq(EFOUR);

else if(distance> 28 && distance< 30){

aSin.setFreq(FFOUR);

else if(distance> 30 && distance< 32){

aSin.setFreq(FSHARPFOUR);

else if(distance> 32 && distance< 34){

aSin.setFreq(GFOUR);

else if(distance> 34 && distance< 36){

aSin.setFreq(GSHARPFOUR);

else if(distance> 36 && distance< 38){

aSin.setFreq(AFOUR);

else if(distance> 38 && distance< 40){

aSin.setFreq(ASHARPFOUR);

else if(distance> 40 && distance< 42){


aSin.setFreq(BFOUR);

else if(distance> 42 && distance< 44){

aSin.setFreq(CFIVE);

else if(distance> 44 && distance< 46){

aSin.setFreq(CSHARPFIVE);

else if(distance> 46 && distance< 48){

aSin.setFreq(DFIVE);

else if(distance> 48 && distance< 50){

aSin.setFreq(DSHARPFIVE);

else if(distance> 50 && distance< 52){

aSin.setFreq(EFIVE);

else if(distance> 52 && distance< 54){

aSin.setFreq(FFIVE);

else{

aSin.setFreq(FTHREE);

}
int updateAudio(){

return aSin.next(); // return an int signal centred around 0

void loop(){

audioHook(); // required here

You might also like