2015年11月8日 星期日

Arduino 6:光學特雷明-- 聲音與光學的樂器

Light Theremin
光學特雷明




  1.                           // variable to hold sensor value
  2. int sensorValue;
  3.                           // variable to calibrate low value
  4. int sensorLow = 1023;
  5.                           // variable to calibrate high value
  6. int sensorHigh = 0;
  7.                           // LED pin
  8. const int ledPin = 13;

  9. void setup() {
  10.                            // Make the LED pin an output and turn it on
  11.   pinMode(ledPin, OUTPUT);
  12.   digitalWrite(ledPin, HIGH);
  13.                             // calibrate for the first five seconds after program runs
  14.   while (millis() < 5000) {
  15.                             //keep record of the maximum sensor value
  16.     sensorValue = analogRead(A0);
  17.     if (sensorValue > sensorHigh) {
  18.       sensorHigh = sensorValue;
  19.     }
  20.                              // record the minimum sensor value
  21.     if (sensorValue < sensorLow) {
  22.       sensorLow = sensorValue;
  23.     }
  24.   }
  25.                            // turn the LED off, signaling the end of the calibration period
  26.   digitalWrite(ledPin, LOW);
  27. }

  28. void loop() {
  29.                            //read the input from A0 and store it in a variable
  30.   sensorValue = analogRead(A0);

  31.                            // map the sensor values to a wide range of pitches
  32.   int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);

  33.                            // play the tone for 20 ms on   pin 8
  34.   tone(8, pitch, 20);
  35.                            // wait for a moment
  36.   delay(10);
  37. }


沒有留言:

張貼留言