Audio, Mozzi: Using Pot to change Frequency.

Hi. I’m completely new to Arduino and electronics and coding so please excuse this being a very basic question. I’m using Mozzi and I’ve set up my board so that using a pot it works with the Volume_Knob sketch. My next goal is to add another pot to the circuit (which I’ve done) and to use that to control the frequency of the sine wave being outputted. What I’ve tried so far isn’t working, using the serial monitor I can use the second pot to go between 0 and 255 but it has no effect on the pitch/Hz. Here’s my code:

#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), look in .h file of table #included above
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);

const char INPUT_PIN = 0; // set the input for the knob to analog pin 0
const char INPUT_PIN7 = A7; 

// to convey the volume level from updateControl() to updateAudio()
byte volume;
byte pitch;


void setup(){
  //Serial.begin(9600); // for Teensy 3.0/3.1, beware printout can cause glitches
  Serial.begin(115200); // set up the Serial output so we can look at the piezo values // set up the Serial output so we can look at the input values
  aSin.setFreq(440);
  startMozzi(); // :))
}


void updateControl(){
  // read the variable resistor for volume
  int sensor_value = mozziAnalogRead(INPUT_PIN); // value is 0-1023
  int sensor_value7 = mozziAnalogRead(INPUT_PIN7);
  
  
  // map it to an 8 bit range for efficient calculations in updateAudio
  volume = map(sensor_value, 0, 1023, 0, 255);  
  pitch = map(sensor_value7, 0, 1023, 0, 255);
  
  // print the value to the Serial monitor for debugging
  Serial.print("volume = ");
  Serial.println((int)volume);
  Serial.print("pitch = ");
  Serial.println((int)pitch);
}


int updateAudio(){
  return ((int)aSin.next() * volume)>>8;  // shift back into range after multiplying by 8 bit value
  return ((int)aSin.next() * pitch)>>8;
}



void loop(){
  audioHook(); // required here
}
  // map it to an 8 bit range for efficient calculations in updateAudio
  volume = map(sensor_value, 0, 1023, 0, 255);  
  pitch = map(sensor_value7, 0, 1023, 0, 255);

If you were looking for efficiency, you'd notice that all that map() is accomplishing is dividing by 4. Dividing by 4 is far faster than what map is actually doing.

  Serial.print("volume = ");
  Serial.println((int)volume);
  Serial.print("pitch = ");
  Serial.println((int)pitch);

Why are you casting the byte values to int? The print() method is perfectly capable of printing a byte.

int updateAudio(){
  return ((int)aSin.next() * volume)>>8;  // shift back into range after multiplying by 8 bit value
  return ((int)aSin.next() * pitch)>>8;
}

You REALLY need to understand what return does. NOTHING after the (first) return statement is ever executed.

Hi

I think I have what you are looking for here

I am working on a modular synth based on Mozzi (in HIFI mode) :

http://elek101.blogspot.be/2015/01/mozmo-brilliant-arduino-mozzi-synth-in.html

and here is my collection of scripts (including a VCO that read a CV signal to change the pitch) :

http://elek101.blogspot.be/p/blog-page.html

Some sounds demo :