2012-10-12 19:23:48 +02:00
|
|
|
/*
|
|
|
|
Arduino Starter Kit example
|
|
|
|
Project 6 - Light Theremin
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-10-12 19:23:48 +02:00
|
|
|
This sketch is written to accompany Project 6 in the
|
|
|
|
Arduino Starter Kit
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-10-12 19:23:48 +02:00
|
|
|
Parts required:
|
|
|
|
photoresistor
|
2013-10-21 09:58:40 +02:00
|
|
|
10 kilohm resistor
|
2012-10-12 19:23:48 +02:00
|
|
|
piezo
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-10-12 19:23:48 +02:00
|
|
|
Created 13 September 2012
|
|
|
|
by Scott Fitzgerald
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-10-12 19:23:48 +02:00
|
|
|
http://arduino.cc/starterKit
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
|
|
This example code is part of the public domain
|
2012-10-12 19:23:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|