Go Down

Topic: Using Two Potentiometers (Read 1 time) previous topic - next topic

LiamNaughton

Hi

I am trying to set up two potentiometers so that one controls the volume of a 440Hz sinewave and the other controls the volume of a 880Hz sinewave. Currently I can only get the 440Hz potentiometer working while the other has no response. I am using the Mozzi sound synthesis library and I have attached my code. Can anyone help me with this?

MorganS

You declare INPUT_PIN1 but then don't use it anywhere in the sketch and there's no code to set the volume of the second sinewave.

Try to add that code, try to compile it. If you get compiler errors you don't understand, you can ask here. If it compiles with no errors but still doesn't work, post the updated code.

Most people prefer if you paste the code between [ code ] tags instead of attaching it.
"The problem is in the code you didn't post."

LiamNaughton

Thanks for the help. I have changed the code as shown below but this only controls 440Hz sinewave on the second potentiometer while I get no response from the first?

#include <MozziGuts.h>
 #include <Oscil.h>
 #include <tables/sin2048_int8.h>
 
 #define CONTROL_RATE 128
 Oscil <2048, AUDIO_RATE> aSin(SIN2048_DATA);
 Oscil <2048, AUDIO_RATE> bSin(SIN2048_DATA);
 
 const char INPUT_PIN = 0;
 const char INPUT_PIN1 = 1;
 
 byte volume;
 
 void setup(){
   aSin.setFreq(440);
   bSin.setFreq(880);
   startMozzi(CONTROL_RATE);
 }
 
void updateControl(){
  // read the variable resistor for volume
  int sensor_value = mozziAnalogRead(INPUT_PIN); // value is 0-1023
  int sensor_value1 = mozziAnalogRead(INPUT_PIN1); // value is 0-1023
 
  // map it to an 8 bit range for efficient calculations in updateAudio
  volume = map(sensor_value, 0, 1023, 0, 255);
  volume = map(sensor_value1, 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 ((int)bSin.next() * volume)>>8; // shift back into range after multiplying by 8 bit value
}


void loop(){
  audioHook(); // required here
}

Henry_Best

#3
Jun 25, 2015, 04:34 am Last Edit: Jun 25, 2015, 04:36 am by Henry_Best
You are trying to control two different volumes, yet you only have one volume variable.
Code: [Select]
volume = map(sensor_value, 0, 1023, 0, 255);  //volume gets immediately overwritten by
volume = map(sensor_value1, 0, 1023, 0, 255); //this line.

LiamNaughton

I have changed it as below but the second potentiometer still does nothing?

volume = map(sensor_value, 0, 1023, 0, 255);
volume1 = map(sensor_value1, 0, 1023, 0, 255);

AWOL

So, post all your code.
Don't forget the code tags.

LiamNaughton

Code: [Select]


#include <MozziGuts.h>
 #include <Oscil.h>
 #include <tables/sin2048_int8.h>
 
 #define CONTROL_RATE 128
 Oscil <2048, AUDIO_RATE> aSin(SIN2048_DATA);
 Oscil <2048, AUDIO_RATE> bSin(SIN2048_DATA);
 
 const char INPUT_PIN = 0;
 const char INPUT_PIN1 = 1;
 
 byte volume;
 byte volume1;
 
 void setup(){
   aSin.setFreq(440);
   bSin.setFreq(880);
   startMozzi(CONTROL_RATE);
 }
 
void updateControl(){
  // read the variable resistor for volume
  int sensor_value = mozziAnalogRead(INPUT_PIN); // value is 0-1023
  int sensor_value1 = mozziAnalogRead(INPUT_PIN1); // value is 0-1023
 
  // map it to an 8 bit range for efficient calculations in updateAudio
  volume = map(sensor_value, 0, 1023, 0, 255);
  volume1 = map(sensor_value1, 0, 1023, 0, 255);
 
  // print the value to the Serial monitor for debugging
  Serial.print("volume = ");
  Serial.println((int)volume);
  Serial.print("volume1 = ");
  Serial.println((int)volume1);
}


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


void loop(){
  audioHook(); // required here
}


AWOL

#7
Jun 25, 2015, 11:46 am Last Edit: Jun 25, 2015, 11:47 am by AWOL
Code: [Select]
int updateAudio(){
  return ((int)aSin.next() * volume)>>8;
  return ((int)bSin.next() * volume1)>>8; // shift back into range after multiplying by 8 bit value
}

What happens when you execute a return?


Code: [Select]
// map it to an 8 bit range for efficient calculations in updateAudio
  volume = sensor_value / 4;
  volume1 = sensor_value1 / 4;

LiamNaughton

i don't understand what you have done there?

AWOL

#9
Jun 25, 2015, 12:05 pm Last Edit: Jun 25, 2015, 12:57 pm by AWOL
In the first part, I haven't done anything except ask a question about your code.

In the second part, I've pointed out that an expensive "map()" can be replaced with a simple divide, which the compiler will probably simplify to an even less expensive "shift"

LiamNaughton

The code compiles without any errors but the second potentiometer does not do anything

AWOL

#11
Jun 25, 2015, 12:46 pm Last Edit: Jun 25, 2015, 12:47 pm by AWOL
Lots of things can compile without error, but not do what the author wanted or intended (or indeed, anything at all).
There's a question in my reply #7; finding out the answer would help you greatly.

1:1

Have you read up on what happens when you execute a return?
answer >> appears not :)

You can only return one instance of the declared return type - furthermore, it is the first the code encounters.

Coding like that indicates some fundamentals are lacking, which is all good (we've got to start somewhere!) but be careful you're not overestimating your skills, time to do some reading ...
The XY problem:  "the over-use of one line responses that simply link though to websites that describe the XY problem"

LiamNaughton

I see. When I swap the two lines around as shown, the second potentiometer works and not the first. I still cannot seem to find a way around this however.

Code: [Select]

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

AWOL

Quote
When I swap the two lines around as shown, the second potentiometer works and not the first. I
The first "return" will always be the one executed.

Go Up