Finished project 06 - Light Theremin. Everything went smooth (hardware and code) but the piezo sounds NOTHING like everyone else's I've seen on the web. Mine sounds like a very angry R2D2 drowning! Also, it's almost on or off ... if I block any light, it turns off.
To check the calcs and photo resistor, I added extra output to the serial monitor so I could see the sensor values and they look ok. But just in case, I changed to a different photo resistor ... same value ranges.
To check the piezo, I modified the code to just send a constant tone to the piezo and the sound was steady. Tried different frequencies and durations just to be sure and they sounded ok too. I'm admittedly still not sure what duration does but for values >10 I get a steady sound.
I thought maybe it was the light in my work area (soft light, dark room) so I tried taking it to different rooms with more light, but no change.
I'm not sure what else to check. If it helps, I can post a link to a video or something. Any advice?
Some piezo buzzers will output a tone without any pulses required., just off of 5V. Others need a wave to create sound. You probably have the wrong kind. Do you have any other buzzers?
Is your sound as if there's mains hum superimposed?
If so a simple lowpass filter on the LDR may help. Artificial lamps have a lot of 50 ( or 60 - depends where you live) Hz amplitude modulation. Are the sensor values stable ? or to they vary a lot - this would be a hint... LDR's are slow enought to reject most of this, but perhaps not all.
of course this may be a coding problem - but we haven't seen your code.
Thanks for the replies. I think I figured it out (see the end).
@Isaac96 - I'm using the piezo that came with my Arduino starter kit. There are no markings on it so I can't tell you much about it. I don't have access to any others (not without the kids' toys apart ).
@allanhurst - It's not really a hum, it's more like morse code. Not sure what to compare the sensor values against but I think you may be on to something ...
int sensorVal = 0;
int sensorLow = 1023;
int sensorHigh = 0;
int calibrationTimeMS = 5000;
//Set values for tone
int freqVal = 0;
int durationVal = 0;
//Set pin values
const int ledPin = 13;
const int sensorPin = A0;
const int tonePin = 8;
void setup() {
//Turn on LED pin to indicate calibration has started
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
//Calbrate sensor
while(millis() < calibrationTimeMS){
//Read the value
sensorVal = analogRead(sensorPin);
//Set sensor high
if(sensorVal > sensorHigh){
sensorHigh = sensorVal;
}
//Set sensor low
if (sensorVal < sensorLow){
sensorLow = sensorVal;
}
}
//Turn off LED to indicate calibration is complete
digitalWrite(ledPin, LOW);
//Serial output to show calibration value
Serial.begin(9600);
Serial.print("Calibration value: ");
Serial.println(sensorVal);
}
void loop() {
//Read the sensor value
sensorVal = analogRead(sensorPin);
//Set frequency (50-4000) ... based on sensor reading (0-1023)
freqVal = map(sensorVal, sensorLow, sensorHigh, 50, 4000);
//Generate tone (pin, frequency, duration)
tone(tonePin, freqVal, durationVal);
//Delay to give sound time to play
delay(10);
Serial.print("Sensor value: ");
Serial.println(sensorVal);
}
EUREKA! I just added feqVal to the Serial monitor and noticed big changes in the freqVal occurring with small changes in the sensorVal ... like 1 pt drop in sensorVal created an 800 pt drop in feqVal. Then I added the sensorLow and sensorHigh values to the Serial Monitor and saw they were about 17 and 21 (respectively). That confirms why map() was calculating BIG jumps for small changes in sensorVal. Then I realized ... DUH... RTFM ... I probably missed the end of the instructions that tell you to change the lighting conditions DURING calibration so the sensorLow and sensorHigh values get set to realistic LOW and HIGH values. Sure enough ... that was it.
Thanks to you both for asking me to post more detail ... that forced me to GET more detail which caused the problem to become clearer.