Basic Theremin: analogWrite() and tone() commands on the same pin

I was trying to build a basic theremin with my Arduino for didactic purpose. My idea was reading a potentiometer for the volume and a photoresistor for the pitch.

My code right now is:

int piezoPin = 5;
int sensorPin = 1;
int potPin = 2;

int sogliaMinima = 20;
int sogliaMassima = 160;

void setup () {
pinMode (piezoPin, OUTPUT);
Serial.begin (9600);
}

void loop () {
// first block of code working
int potLevel = analogRead (potPin);
int levelVolume = map (potLevel, 0, 1022, 0, 170);
analogWrite (piezoPin, levelVolume);

// second block of code working
int sensorValue = analogRead (sensorPin);
int pitchLevel = map (sensorValue, sogliaMinima, sogliaMassima, 100, 1000);
tone (piezoPin, pitchLevel);
}

This is not working, and it's a software issue not a circuit mistake. Both first block and second block are working if commenting one of them, but don't work together. So my question is: can I use tone() and analogWrite on the same pin?

So my question is: can I use tone() and analogWrite on the same pin?

No.
Analog Write generates a PWM signal, this is not compatible with with a square wave from the tone.

You should be able to do something with pulse-width to control the volume, but you'd have to write your own tone-like function. A square wave with a 50% duty cycle will give you the maximum volume. If you narrow the pluse width (but keep the same pitch/frequency by increasing the off-time) the sound will be quieter and the timbre will change.

the sound will be quieter and the timbre will change.

Give over, there will be some subtial change but not one that is easly noticed.

ok, thx for your replies.
I didn't mention something important, but maybe you can point me in the right direction. I did a test: instead of reading by the Arduino the voltage setted by the pot and using it to PWM the piezo, I connected directly the + of the piezo to the output of the potentiometer, and it worked. I thought "I'm powering it using the V setted by the potentiometer, so it must be right". And it was working. But how it's possible that's working when on the same time I'm PWM it with the analogWrite commanded by the value of the photoresistor?

It sounds like you were just providing a DC bias level to the piezo. The danger of connecting a pot to it as well as a PWM pin, is that the pot acts as a load for the pin. If the pot is at either end of the travel you will short the pin to either +5 or ground. As the pin is trying to pull the line between +5 and ground you will exceed the current rating on the pin and fry it.

reading a potentiometer for the volume ...

... I did a test: instead of reading by the Arduino the voltage setted by the pot...

Well that gives me a SIMPLE IDEA I should have thought-of in the 1st place! :blush: The most common use of a pot is a regular 'ol analog volume control.

Is the piezo connected directly to the Arduino output pin? You can simply wire a pot as a [u]volume control[/u] between the Arduino and the piezo. You'll need an "audio taper" pot, and I'd guess 1k should work.