Hey guys, I'm currently working on something which I'm having quite a lot of trouble with and seem to struggle finding any more information on how to do this.
Using the Mozzi synthesis library, I'm trying to make my Arduino play 2 different sine waves at different frequencies. This I believe I have already done. What I'm struggling on is making one potentiometer for the volume of one wave, another for the volume of the second wave, and finally one for master volume. After that, I want to make it so that I can attach a button to turn on/off each of the waves (so this will require 2 buttons). Could anyone please help me? This is my code so far:
#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator
// use #define for CONTROL_RATE, not a constant
#define CONTROL_RATE 64 // powers of 2 please
// 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);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin2(SIN2048_DATA);
const int8_t INPUT_PIN = 0; // Set the input for the knob to analog pin 0
// To convery the volume level from updateControl() to updateAudio()
uint8_t volume;
void setup()
{
Serial.begin(9600); // Set up the Serial output so we can look at the input values
startMozzi(CONTROL_RATE); // set a control rate of 64 (powers of 2 please)
aSin.setFreq(440); // set the frequency
aSin2.setFreq(880); // second oscillator frequency
}
void updateControl()
{
// Read the variable resistor for volume
int sensor_value = mozziAnalogRead(INPUT_PIN); // Value is 0-1023
// Map it to an 8-bit range for efficient calculations in updateAudio
volume = map(sensor_value, 0, 1023, 0, 255);
// Print the value to the Serial monitor for debugging
Serial.print("volume = ");
Serial.println((int)volume);
}
int updateAudio()
{
return ((int)aSin.next() * volume)>>8; // Return an int signal centred around 0 + shift back into range after multiplying by 8-bit value
return ((int)aSin2.next() * volume)>>8; // Return an int signal centred around 0 + shift back into range after multiplying by 8-bit value
return 0;
}
void loop()
{
audioHook(); // Required here
}
Thanks in advance!