Skip to content

Instantly share code, notes, and snippets.

@Steve-Tech
Last active August 4, 2018 12:31
Show Gist options
  • Save Steve-Tech/4286e3091fb5534295a79cfa96ac6557 to your computer and use it in GitHub Desktop.
Save Steve-Tech/4286e3091fb5534295a79cfa96ac6557 to your computer and use it in GitHub Desktop.
A piano made with buttons written for Arduino.
/*
* Steve's Piano even though it's more of a keyboard.
* Wiring Diagram at https://steve.jas-team.net/howto/arduino/piano/
* By Steve
*/
#include "pitches.h" //Got this from https://www.arduino.cc/en/Tutorial/ToneMelody but should be included here
//Declaring Variables although they don't vary
const int KeyPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; //An Array for the pins of the keys
const int BuzzerPin = 10; //The pin for the buzzer
void setup()
{
Serial.begin(9600); //Begin Serial
Serial.print("Starting: ");
for (int i = 0; i <= 7; i++) {
pinMode(KeyPins[i], INPUT); //Set the pinMode for the keys to INPUT
Serial.print(i);
}
Serial.println("");
//Set the pinMode for the Buzzer and LED to OUTPUT
pinMode(BuzzerPin, OUTPUT);
}
void loop()
{
if (digitalRead(KeyPins[7])) {
Serial.println("Note: C"); //Show what note is being played in Serial
tone(BuzzerPin, NOTE_C4, 1000 / 4); //Play note on the Buzzer
}
if (digitalRead(KeyPins[6])) {
Serial.println("Note: D");
tone(BuzzerPin, NOTE_D4, 1000 / 4);
}
if (digitalRead(KeyPins[5])) {
Serial.println("Note: E");
tone(BuzzerPin, NOTE_E4, 1000 / 4);
}
if (digitalRead(KeyPins[4])) {
Serial.println("Note: F");
tone(BuzzerPin, NOTE_F4, 1000 / 4);
}
if (digitalRead(KeyPins[3])) {
Serial.println("Note: G");
tone(BuzzerPin, NOTE_G4, 1000 / 4);
}
if (digitalRead(KeyPins[2])) {
Serial.println("Note: A");
tone(BuzzerPin, NOTE_A4, 1000 / 4);
}
if (digitalRead(KeyPins[1])) {
Serial.println("Note: B");
tone(BuzzerPin, NOTE_B4, 1000 / 4);
}
if (digitalRead(KeyPins[0])) {
Serial.println("Note: C");
tone(BuzzerPin, NOTE_C5, 1000 / 4);
}
}
/*
* Steve's Piano with LEDs even though it's more of a keyboard.
* Wiring Diagram at https://steve.jas-team.net/howto/arduino/piano/
* By Steve
*/
#include "pitches.h" //Got this from https://www.arduino.cc/en/Tutorial/ToneMelody but should be included here
//Declaring Variables although they don't vary
const int KeyPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; //An Array for the pins of the keys
const int LEDPin[] = {11, 12, 13}; //An Array for the pins of 3 LEDs or an RGB LED
const int BuzzerPin = 10; //The pin for the buzzer
void setup()
{
Serial.begin(9600); //Begin Serial
Serial.print("Starting: ");
for (int i = 0; i <= 7; i++) {
pinMode(KeyPins[i], INPUT); //Set the pinMode for the keys to INPUT
Serial.print(i);
}
Serial.println("");
//Set the pinMode for the Buzzer and LED to OUTPUT
pinMode(BuzzerPin, OUTPUT);
pinMode(LEDPin[0], OUTPUT);
pinMode(LEDPin[1], OUTPUT);
pinMode(LEDPin[2], OUTPUT);
}
void loop()
{ //Turn off the LED if no keys are pressed
if (!(digitalRead(KeyPins[0]) && digitalRead(KeyPins[1]) && digitalRead(KeyPins[2]) && digitalRead(KeyPins[3]) && digitalRead(KeyPins[4]) && digitalRead(KeyPins[5]) && digitalRead(KeyPins[6])&& digitalRead(KeyPins[7]))){
digitalWrite(LEDPin[0], LOW);
digitalWrite(LEDPin[1], LOW);
digitalWrite(LEDPin[2], LOW);
}
if (digitalRead(KeyPins[7])) {
Serial.println("Note: C"); //Show what note is being played in Serial
tone(BuzzerPin, NOTE_C4, 1000 / 4); //Play note on the Buzzer
digitalWrite(LEDPin[0], HIGH); //Make the LED show different colours for different notes
digitalWrite(LEDPin[1], LOW);
digitalWrite(LEDPin[2], LOW);
}
if (digitalRead(KeyPins[6])) {
Serial.println("Note: D");
tone(BuzzerPin, NOTE_D4, 1000 / 4);
digitalWrite(LEDPin[0], HIGH);
digitalWrite(LEDPin[1], HIGH);
digitalWrite(LEDPin[2], LOW);
}
if (digitalRead(KeyPins[5])) {
Serial.println("Note: E");
tone(BuzzerPin, NOTE_E4, 1000 / 4);
digitalWrite(LEDPin[0], HIGH);
digitalWrite(LEDPin[1], HIGH);
digitalWrite(LEDPin[2], HIGH);
}
if (digitalRead(KeyPins[4])) {
Serial.println("Note: F");
tone(BuzzerPin, NOTE_F4, 1000 / 4);
digitalWrite(LEDPin[0], LOW);
digitalWrite(LEDPin[1], HIGH);
digitalWrite(LEDPin[2], HIGH);
}
if (digitalRead(KeyPins[3])) {
Serial.println("Note: G");
tone(BuzzerPin, NOTE_G4, 1000 / 4);
digitalWrite(LEDPin[0], LOW);
digitalWrite(LEDPin[1], LOW);
digitalWrite(LEDPin[2], HIGH);
}
if (digitalRead(KeyPins[2])) {
Serial.println("Note: A");
tone(BuzzerPin, NOTE_A4, 1000 / 4);
digitalWrite(LEDPin[0], HIGH);
digitalWrite(LEDPin[1], LOW);
digitalWrite(LEDPin[2], HIGH);
}
if (digitalRead(KeyPins[1])) {
Serial.println("Note: B");
tone(BuzzerPin, NOTE_B4, 1000 / 4);
digitalWrite(LEDPin[0], LOW);
digitalWrite(LEDPin[1], HIGH);
digitalWrite(LEDPin[2], LOW);
}
if (digitalRead(KeyPins[0])) {
Serial.println("Note: C");
tone(BuzzerPin, NOTE_C5, 1000 / 4);
digitalWrite(LEDPin[0], HIGH);
digitalWrite(LEDPin[1], HIGH);
digitalWrite(LEDPin[2], HIGH);
}
}
/*
* Steve's Piano with potentiometer control even though it's more of a keyboard.
* Wiring Diagram at https://steve.jas-team.net/howto/arduino/piano/
* By Steve
*/
//Declaring Constant Variables
const int KeyPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; //An Array for the pins of the keys
const int LEDPin[] = {11, 12, 13}; //An Array for the pins of an RGB LED
const int BuzzerPin = 10; //The pin for the buzzer
const int potPin = A0;
const boolean SerialEnableFreq = false; //Enable or disable the potentiometer reading on Serial
//Declaring Variables
int potC;
int potD;
int potE;
int potF;
int potG;
int potA;
int potB;
int potC2;
void setup()
{
Serial.begin(9600); //Begin Serial if Enabled
Serial.print("Starting: ");
for (int i = 0; i <= 7; i++) {
pinMode(KeyPins[i], INPUT); //Set the pinMode for the keys to INPUT
Serial.print(i);
}
Serial.println("");
//Set the pinMode for the Buzzer and LED to OUTPUT
pinMode(BuzzerPin, OUTPUT);
}
void loop()
{
if (SerialEnableFreq) {
Serial.println(analogRead(potPin));
}
//Read the Potentiometer, divide by 100 to make it single digits then multiply by frequencies of notes.
potC = analogRead(potPin) / 100 * 33;
potD = analogRead(potPin) / 100 * 36;
potE = analogRead(potPin) / 100 * 41;
potF = analogRead(potPin) / 100 * 44;
potG = analogRead(potPin) / 100 * 49;
potA = analogRead(potPin) / 100 * 55;
potB = analogRead(potPin) / 100 * 62;
potC2 = analogRead(potPin) / 100 * 66;
if (digitalRead(KeyPins[7])) {
Serial.println("Note: C"); //Show what note is being played in Serial although the Potentiometer probably changes this
tone(BuzzerPin, potC, 1000 / 4); //Play note on the Buzzer
}
if (digitalRead(KeyPins[6])) {
Serial.println("Note: D");
tone(BuzzerPin, potD, 1000 / 4);
}
if (digitalRead(KeyPins[5])) {
Serial.println("Note: E");
tone(BuzzerPin, potE, 1000 / 4);
}
if (digitalRead(KeyPins[4])) {
Serial.println("Note: F");
tone(BuzzerPin, potF, 1000 / 4);
}
if (digitalRead(KeyPins[3])) {
Serial.println("Note: G");
tone(BuzzerPin, potG, 1000 / 4);
}
if (digitalRead(KeyPins[2])) {
Serial.println("Note: A");
tone(BuzzerPin, potA, 1000 / 4);
}
if (digitalRead(KeyPins[1])) {
Serial.println("Note: B");
tone(BuzzerPin, potB, 1000 / 4);
}
if (digitalRead(KeyPins[0])) {
Serial.println("Note: C");
tone(BuzzerPin, potC2, 1000 / 4);
}
}
//Got this from https://www.arduino.cc/en/Tutorial/ToneMelody?action=sourceblock&num=2
/*************************************************
* Public Constants
*************************************************/
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment