Debian Codegraph @

examples/10.StarterKit_BasicKit/p06_LightTheremin/p06_LightTheremin.ino

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