| 1 |
/* |
| 2 |
Keyboard |
| 3 |
|
| 4 |
Plays a pitch that changes based on a changing analog input |
| 5 |
|
| 6 |
circuit: |
| 7 |
- three force-sensing resistors from +5V to analog in 0 through 5 |
| 8 |
- three 10 kilohm resistors from analog in 0 through 5 to ground |
| 9 |
- 8 ohm speaker on digital pin 8 |
| 10 |
|
| 11 |
created 21 Jan 2010 |
| 12 |
modified 9 Apr 2012 |
| 13 |
by Tom Igoe |
| 14 |
|
| 15 |
This example code is in the public domain. |
| 16 |
|
| 17 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneKeyboard |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "pitches.h" |
| 21 |
|
| 22 |
const int threshold = 10; // minimum reading of the sensors that generates a note |
| 23 |
|
| 24 |
// notes to play, corresponding to the 3 sensors: |
| 25 |
int notes[] = { |
| 26 |
NOTE_A4, NOTE_B4, NOTE_C3 |
| 27 |
}; |
| 28 |
|
| 29 |
void setup() { |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
void loop() { |
| 34 |
for (int thisSensor = 0; thisSensor < 3; thisSensor++) { |
| 35 |
// get a sensor reading: |
| 36 |
int sensorReading = analogRead(thisSensor); |
| 37 |
|
| 38 |
// if the sensor is pressed hard enough: |
| 39 |
if (sensorReading > threshold) { |
| 40 |
// play the note corresponding to this sensor: |
| 41 |
tone(8, notes[thisSensor], 20); |
| 42 |
} |
| 43 |
} |
| 44 |
} |