Skip to content

Instantly share code, notes, and snippets.

@matti
Last active December 19, 2015 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matti/c5a7fe881259bb9b90ee to your computer and use it in GitHub Desktop.
Save matti/c5a7fe881259bb9b90ee to your computer and use it in GitHub Desktop.
/*
Arduino Starter Kit example
Project 7 - Keyboard
This sketch is written to accompany Project 7 in the
Arduino Starter Kit
Parts required:
two 10 kilohm resistors
1 Megohm resistor
220 ohm resistor
4 pushbuttons
piezo
Created 13 September 2012
by Scott Fitzgerald
http://www.arduino.cc/starterKit
This example code is part of the public domain
*/
// create an array of notes
// the numbers below correspond to
// the frequencies of middle C, D, E, and F
int notes[] = {262, 294, 330, 349};
void setup() {
//start serial communication
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(2, HIGH);
}
void loop() {
// create a local variable to hold the input on pin A0
int keyVal = analogRead(A0);
delay(5);
int tmpVal = analogRead(A1);
delay(5);
Serial.println(tmpVal);
if (tmpVal < 150) {
digitalWrite(13, LOW);
noTone(8);
return;
}
digitalWrite(13, HIGH);
// send the value from A0 to the Serial Monitor
//Serial.println(keyVal);
// play the note corresponding to each value on A0
if (keyVal == 1023) {
// play the first frequency in the array on pin 8
tone(8, notes[0]);
} else if (keyVal >= 990 && keyVal <= 1010) {
// play the second frequency in the array on pin 8
tone(8, notes[1]);
} else if (keyVal >= 505 && keyVal <= 515) {
// play the third frequency in the array on pin 8
tone(8, notes[2]);
} else if (keyVal >= 5 && keyVal <= 10) {
// play the fourth frequency in the array on pin 8
tone(8, notes[3]);
} else {
// if the value is out of range, play no tone
noTone(8);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment