source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/10.StarterKit_BasicKit/p06_LightTheremin/p06_LightTheremin.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 1.4 KB
Line 
1/*
2  Arduino Starter Kit example
3 Project 6  - Light Theremin
4
5 This sketch is written to accompany Project 6 in the
6 Arduino Starter Kit
7
8 Parts required:
9 photoresistor
10 10 kilohm resistor
11 piezo
12
13 Created 13 September 2012
14 by Scott Fitzgerald
15
16 http://www.arduino.cc/starterKit
17
18 This example code is part of the public domain
19*/
20
21// variable to hold sensor value
22int sensorValue;
23// variable to calibrate low value
24int sensorLow = 1023;
25// variable to calibrate high value
26int sensorHigh = 0;
27// LED pin
28const int ledPin = 13;
29
30void setup() {
31  // Make the LED pin an output and turn it on
32  pinMode(ledPin, OUTPUT);
33  digitalWrite(ledPin, HIGH);
34
35  // calibrate for the first five seconds after program runs
36  while (millis() < 5000) {
37    // record the maximum sensor value
38    sensorValue = analogRead(A0);
39    if (sensorValue > sensorHigh) {
40      sensorHigh = sensorValue;
41    }
42    // record the minimum sensor value
43    if (sensorValue < sensorLow) {
44      sensorLow = sensorValue;
45    }
46  }
47  // turn the LED off, signaling the end of the calibration period
48  digitalWrite(ledPin, LOW);
49}
50
51void loop() {
52  //read the input from A0 and store it in a variable
53  sensorValue = analogRead(A0);
54
55  // map the sensor values to a wide range of pitches
56  int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
57
58  // play the tone for 20 ms on pin 8
59  tone(8, pitch, 20);
60
61  // wait for a moment
62  delay(10);
63}
64
Note: See TracBrowser for help on using the repository browser.