/**
Mini piano for Arduino.
You can control the colorful buttons with your keyboard:
After starting the simulation, click anywhere in the diagram to focus it.
Then press any key between 1 and 8 to play the piano (1 is the lowest note,
8 is the highest).
Copyright (C) 2021, Uri Shaked. Released under the MIT License.
*/
#include "pitches.h"
#define SPEAKER_PIN 8
const uint8_t buttonPins[] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const int buttonTones[] = {
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
int numTones;
int ledPin =13;
int tune1[] = {0,0,4,4,5,5,4,99,3,3,2,2,1,1,0,99};
//int s = sizeof(tune1);
int s =16;
int Speed;
void setup() {
Serial.begin(115200);
for (uint8_t i = 0; i < 8; i++)
{
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop()
{
numTones = s;
int pitch = 0;
for (uint8_t i = 0; i < numTones; i++)
{
Speed = 1023 - analogRead(A0)+50;
Serial.print(Speed);
pitch = buttonTones[tune1[i]];
Serial.print("numtones =");
Serial.print(numTones);
Serial.print("\t i = ");
Serial.print(i);
Serial.print ("\t pitch");
Serial.println(pitch);
if (pitch != 99)
{
tone(SPEAKER_PIN, pitch);
digitalWrite(ledPin,HIGH);
delay(50);
digitalWrite(ledPin,LOW);
delay(Speed);
noTone(SPEAKER_PIN);
delay(100);
}
else
{
noTone(SPEAKER_PIN);
delay(Speed);
}
}
}