| 1 |
/* |
| 2 |
Arduino Starter Kit example |
| 3 |
Project 7 - Keyboard |
| 4 |
|
| 5 |
This sketch is written to accompany Project 7 in the Arduino Starter Kit |
| 6 |
|
| 7 |
Parts required: |
| 8 |
- two 10 kilohm resistors |
| 9 |
- 1 megohm resistor |
| 10 |
- 220 ohm resistor |
| 11 |
- four pushbuttons |
| 12 |
- piezo |
| 13 |
|
| 14 |
created 13 Sep 2012 |
| 15 |
by Scott Fitzgerald |
| 16 |
|
| 17 |
https://store.arduino.cc/genuino-starter-kit |
| 18 |
|
| 19 |
This example code is part of the public domain. |
| 20 |
*/ |
| 21 |
|
| 22 |
// create an array of notes |
| 23 |
// the numbers below correspond to the frequencies of middle C, D, E, and F |
| 24 |
int notes[] = {262, 294, 330, 349}; |
| 25 |
|
| 26 |
void setup() { |
| 27 |
//start serial communication |
| 28 |
Serial.begin(9600); |
| 29 |
} |
| 30 |
|
| 31 |
void loop() { |
| 32 |
// create a local variable to hold the input on pin A0 |
| 33 |
int keyVal = analogRead(A0); |
| 34 |
// send the value from A0 to the Serial Monitor |
| 35 |
Serial.println(keyVal); |
| 36 |
|
| 37 |
// play the note corresponding to each value on A0 |
| 38 |
if (keyVal == 1023) { |
| 39 |
// play the first frequency in the array on pin 8 |
| 40 |
tone(8, notes[0]); |
| 41 |
} else if (keyVal >= 990 && keyVal <= 1010) { |
| 42 |
// play the second frequency in the array on pin 8 |
| 43 |
tone(8, notes[1]); |
| 44 |
} else if (keyVal >= 505 && keyVal <= 515) { |
| 45 |
// play the third frequency in the array on pin 8 |
| 46 |
tone(8, notes[2]); |
| 47 |
} else if (keyVal >= 5 && keyVal <= 10) { |
| 48 |
// play the fourth frequency in the array on pin 8 |
| 49 |
tone(8, notes[3]); |
| 50 |
} else { |
| 51 |
// if the value is out of range, play no tone |
| 52 |
noTone(8); |
| 53 |
} |
| 54 |
} |