-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3487e64
commit 2453a7f
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
// variable to hold sensor value | ||
int sensorValue; | ||
// variable to calibrate low value | ||
int sensorLow = 1023; | ||
// variable to calibrate high value | ||
int sensorHigh = 0; | ||
// LED pin | ||
const int ledPin = 13; | ||
|
||
void setup() { | ||
// Make the LED pin an output and turn it on | ||
pinMode(ledPin, OUTPUT); | ||
digitalWrite(ledPin, HIGH); | ||
|
||
// calibrate for the first five seconds after program runs | ||
while (millis() < 5000) { | ||
// record the maximum sensor value | ||
sensorValue = analogRead(A0); | ||
if (sensorValue > sensorHigh) { | ||
sensorHigh = sensorValue; | ||
} | ||
// record the minimum sensor value | ||
if (sensorValue < sensorLow) { | ||
sensorLow = sensorValue; | ||
} | ||
} | ||
// turn the LED off, signaling the end of the calibration period | ||
digitalWrite(ledPin, LOW); | ||
} | ||
|
||
void loop() { | ||
//read the input from A0 and store it in a variable | ||
sensorValue = analogRead(A0); | ||
|
||
// map the sensor values to a wide range of pitches | ||
int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000); | ||
|
||
// play the tone for 20 ms on pin 8 | ||
tone(8, pitch, 20); | ||
|
||
// wait for a moment | ||
delay(10); | ||
} |