Maybe this has already been covered but I can't find it. I'm new to programming and electronics but have always wanted to learn, so I'm working through the examples. In Project 6: Light Theremin on page 75, the book states that the variables should be set at
2. int sensorLow = 1023;
3. int sensorHigh = 0;
When I use these values the system does not calibrate correctly, and I get a weird result. It seems to me the initial variable settings should be:
2. int sensorLow = 0;
3. int sensorHigh = 1023;
I can monitor the High and Low values by introducing a Serial.begin(115200); and then printing the values. I've used the higher baud rate because the higher baud rate is undetectable to the human ear so it still sounds like a theremin. Here is what my code looks like now and it seems to work. With adjustments to the code the Low and High outputs in the serial monitor are 0 to 1023 which is what I would expect. When you use the code in the book the Low and High outputs are ~750 to 753 which is a pretty narrow range. Any suggestions?
//Light Theremin
int sensorValue;
int sensorLow = 0;
int sensorHigh = 1023;
const int ledPin = 13;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
while (millis() < 5000){
sensorValue = analogRead(A0);
if (sensorValue > sensorHigh){
sensorHigh = sensorValue;
}
if (sensorValue < sensorLow){
sensorLow = sensorValue;
}
}
digitalWrite(ledPin, LOW);
}
void loop() {
sensorValue = analogRead(A0);
int pitch = map(sensorValue, sensorLow, sensorHigh, 1, 4000);
tone(9, pitch, 20);
delay(10);
Serial.println("Sensor Values");
Serial.print("SensorLow = ");
Serial.print(sensorLow);
Serial.print(", SensorHigh = ");
Serial.print(sensorHigh);
Serial.print(", Sensor Value= ");
Serial.println(sensorValue);
}