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?
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.
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?
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
}
#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
}
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?
// map it to an 8 bit range for efficient calculations in updateAudio
volume = sensor_value / 4;
volume1 = sensor_value1 / 4;
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"
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.
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 ...
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.
int updateAudio(){
return ((int)bSin.next() * volume1)>>8;
return ((int)aSin.next() * volume)>>8; // shift back into range after multiplying by 8 bit value
}