X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=piezo%2Fpiezo_1_tonePitchFollower%2Fpiezo_1_tonePitchFollower_ino%2Fpiezo_1_tonePitchFollower_ino.ino;fp=piezo%2Fpiezo_1_tonePitchFollower%2Fpiezo_1_tonePitchFollower_ino%2Fpiezo_1_tonePitchFollower_ino.ino;h=b5e9d24547dda93e8a255fa0678be36b6e5caf13;hb=54de1d245a98a8032e8c41089158321f6be58b1c;hp=0000000000000000000000000000000000000000;hpb=ca9bc9014acdfae3ee07d19650c547f02b796994;p=sketchbook_andrea diff --git a/piezo/piezo_1_tonePitchFollower/piezo_1_tonePitchFollower_ino/piezo_1_tonePitchFollower_ino.ino b/piezo/piezo_1_tonePitchFollower/piezo_1_tonePitchFollower_ino/piezo_1_tonePitchFollower_ino.ino new file mode 100644 index 0000000..b5e9d24 --- /dev/null +++ b/piezo/piezo_1_tonePitchFollower/piezo_1_tonePitchFollower_ino/piezo_1_tonePitchFollower_ino.ino @@ -0,0 +1,64 @@ +/* +Pitch following + +The input from a photo resistor dictates the pitch of a piezo. + + + */ + +// These constants won't change: +const int sensorPin = A0; // pin that the sensor is attached to +const int ledPin = 9; // pin that the LED is attached to +int thisPitch ; + +// calibration variables: +int sensorValue = 0; // the sensor value +int sensorMin = 1023; // minimum sensor value +int sensorMax = 0; // maximum sensor value + + +void setup() { + // turn on LED to signal the start of the calibration period: + pinMode(13, OUTPUT); + digitalWrite(13, HIGH); + + // calibrate during the first five seconds + while (millis() < 5000) { + sensorValue = analogRead(sensorPin); + + // record the maximum sensor value + if (sensorValue > sensorMax) { + sensorMax = sensorValue; + } + + // record the minimum sensor value + if (sensorValue < sensorMin) { + sensorMin = sensorValue; + } + } + + // signal the end of the calibration period + digitalWrite(13, LOW); +} + +void loop() { + // read the sensor: + sensorValue = analogRead(sensorPin); + + // apply the calibration to the sensor reading + thisPitch = map(sensorValue, sensorMin, sensorMax, 120, 1500); + // map the analog input range (in this case, min - max from the photoresistor) + // to the output pitch range (120 - 1500Hz) + // change the minimum and maximum input numbers below + // depending on the range your sensor's giving: + + // in case the sensor value is outside the range seen during calibration + thisPitch = constrain(sensorValue, 120, 1500); + + + + // play the pitch: + tone(ledPin, thisPitch, 10); // Tone is built in function + delay(1); // delay in between notes +} +