ArduinoTutorial: Sensorium Mozzi

How to use the Sensorium Mozzi library to play two sine waves demonstrating a beat frequency

Download

Download Sensorium Mozzie

Instructions for installing the library

Schematic

Speaker on pin 9

Sketch:

/*  Example playing two sinewaves at different frequencies
 *  using Mozzi sonification library.
 *
 *  Demonstrates the resultant beat frequency
 *
 *  Circuit: Audio output on digital pin 9 (on a Uno or similar), or 
 *  check the README or http://sensorium.github.com/Mozzi/
 *
 *  Mozzi help/discussion/announcements:
 *  https://groups.google.com/forum/#!forum/mozzi-users
 *
 *  Michael Shiloh 2013
 *  based on an example by Tim Barrass 2012.
 *  This example code is in the public domain.
 */

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

// use: Oscil <table_size, update_rate> oscilName (wavetable)
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> bSin(SIN2048_DATA);

// use #define for CONTROL_RATE, not a constant
#define CONTROL_RATE 64 // powers of 2 please

void setup(){
  startMozzi(CONTROL_RATE); // set a control rate of 64 (powers of 2 please)
  aSin.setFreq(440); // set the frequency
  bSin.setFreq(448); // set the frequency
}

void updateControl(){
  // put changing controls in here
}

int updateAudio(){
//  return (aSin.next());
  int foo=aSin.next()+bSin.next(); // 8+8=9 bits
  return foo>>2;
}

void loop(){
  audioHook(); // required here
}
Print Friendly
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Leave a Reply

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