// Original Version
int sensorValue;
int sensorLow = 1023;
int sensorHigh = 0;
const int ledPin = 13;
void setup() {
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, 50, 4000);
tone(8, pitch, 20);
delay(10);
}
// Potentiometer version
int sensorValue;
int sensorLow = 1023;
int sensorHigh = 10;
const int ledPin = 13;
int const potPin = A0; // analog pin used to connect the potentiometer
int potVal; // variable to read the value from the analog pin
void setup() {
while (millis() < 5000) {
sensorValue = analogRead(13);
if (sensorValue > sensorLow) {
sensorLow = sensorValue;
}
if (sensorValue < sensorHigh) {
sensorHigh = sensorValue;
}
}
digitalWrite(ledPin, LOW);
}
void loop() {
potVal = analogRead(potPin); // read the value of the potentiometer
// print out the value to the Serial Monitor
Serial.print("potVal: ");
Serial.print(potVal);
sensorValue = analogRead(A0);
int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
tone(A0, pitch, 50);
delay(100);
}