2012-10-12 19:23:48 +02:00
|
|
|
/*
|
|
|
|
Arduino Starter Kit example
|
2017-07-15 00:30:34 +02:00
|
|
|
Project 7 - Keyboard
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-14 21:34:00 +02:00
|
|
|
This sketch is written to accompany Project 7 in the Arduino Starter Kit
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
Parts required:
|
2017-07-12 22:18:23 +02:00
|
|
|
- two 10 kilohm resistors
|
|
|
|
- 1 megohm resistor
|
|
|
|
- 220 ohm resistor
|
|
|
|
- four pushbuttons
|
|
|
|
- piezo
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
created 13 Sep 2012
|
2017-07-15 00:30:34 +02:00
|
|
|
by Scott Fitzgerald
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
http://www.arduino.cc/starterKit
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
This example code is part of the public domain.
|
2012-10-12 19:23:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
// create an array of notes
|
2017-07-14 21:34:00 +02:00
|
|
|
// the numbers below correspond to the frequencies of middle C, D, E, and F
|
2012-10-12 19:23:48 +02:00
|
|
|
int notes[] = {262, 294, 330, 349};
|
|
|
|
|
|
|
|
void setup() {
|
2013-10-21 09:58:40 +02:00
|
|
|
//start serial communication
|
2012-10-12 19:23:48 +02:00
|
|
|
Serial.begin(9600);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// create a local variable to hold the input on pin A0
|
|
|
|
int keyVal = analogRead(A0);
|
|
|
|
// send the value from A0 to the Serial Monitor
|
|
|
|
Serial.println(keyVal);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2012-10-12 19:23:48 +02:00
|
|
|
// play the note corresponding to each value on A0
|
2013-10-21 09:58:40 +02:00
|
|
|
if (keyVal == 1023) {
|
2012-10-12 19:23:48 +02:00
|
|
|
// play the first frequency in the array on pin 8
|
|
|
|
tone(8, notes[0]);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else if (keyVal >= 990 && keyVal <= 1010) {
|
2012-10-12 19:23:48 +02:00
|
|
|
// play the second frequency in the array on pin 8
|
|
|
|
tone(8, notes[1]);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else if (keyVal >= 505 && keyVal <= 515) {
|
2012-10-12 19:23:48 +02:00
|
|
|
// play the third frequency in the array on pin 8
|
|
|
|
tone(8, notes[2]);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else if (keyVal >= 5 && keyVal <= 10) {
|
2012-10-12 19:23:48 +02:00
|
|
|
// play the fourth frequency in the array on pin 8
|
|
|
|
tone(8, notes[3]);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2012-10-12 19:23:48 +02:00
|
|
|
// if the value is out of range, play no tone
|
|
|
|
noTone(8);
|
|
|
|
}
|
|
|
|
}
|