Go Down

Topic: How to : Piano with recorder (Read 1 time) previous topic - next topic


Hi guys, I'm new to arduino. I built an arduino piano using the digital inputs as my push button switches. Now, my problem is, how can i record what I've been playing on the arduino piano without any shields. Somebody told me that I could do it by recording the key pressed and add a timer so it could be determined how long the was pressed. The problem is, I don't know how to put it into codes. Please help me. Please.. Thanks in advance!

Here's the code:

int inputPin1 = 13;
int inputPin2 = 12;
int inputPin3 = 11;
int inputPin4 = 10;
int inputPin5 = 9;
int inputPin6 = 8;
int inputPin7 = 7;
int inputPin8 = 6;
int inputPin9 = 5;
int inputPin10 = 4;
int inputPin11 = 3;
int inputPin12 = 2;
int speakerPin = A0;
int val = 0;
int length = 1; // the number of notes
char notes[] = { 'c', 'C', 'd', 'D', 'e', 'f', 'F', 'g', 'G', 'a', 'A', 'b' }; // a space represents a rest
int beats[] = { 1 };
int tempo = 300;

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    digitalWrite(speakerPin, LOW);

void playNote(char note, int duration) {
  char names[] = { 'c', 'C', 'd', 'D', 'e', 'f', 'F', 'g', 'G', 'a', 'A', 'b' };
  int tones[] = { 1911, 1804, 1702, 1607, 1517, 1432, 1351, 1276, 1204, 1136, 1073, 1012 };

  // play the tone corresponding to the note name 

for (int i = 0; i < 12; i++) {
  if (names == note) {     
    playTone(tones, duration);

void setup() {
  pinMode(speakerPin, OUTPUT);
  pinMode(inputPin1, INPUT);
  pinMode(inputPin2, INPUT);
  pinMode(inputPin3, INPUT);
  pinMode(inputPin4, INPUT);
  pinMode(inputPin5, INPUT);
  pinMode(inputPin6, INPUT);
  pinMode(inputPin7, INPUT);
  pinMode(inputPin8, INPUT);
  pinMode(inputPin9, INPUT);
  pinMode(inputPin10, INPUT);
  pinMode(inputPin11, INPUT);
  pinMode(inputPin12, INPUT);

void loop() {
  if (digitalRead(inputPin1) == HIGH) {
    playNote(notes[0], 300);
  } else if (digitalRead(inputPin2) == HIGH) {
    playNote(notes[1], 300);
  } else if (digitalRead(inputPin3) == HIGH) {
    playNote(notes[2], 300);
  } else if (digitalRead(inputPin4) == HIGH) {
    playNote(notes[3], 300);
  } else if (digitalRead(inputPin5) == HIGH) {
    playNote(notes[4], 300);
  } else if (digitalRead(inputPin6) == HIGH) {
    playNote(notes[5], 300);
  } else if (digitalRead(inputPin7) == HIGH) {
    playNote(notes[6], 300);
  } else if (digitalRead(inputPin8) == HIGH) {
    playNote(notes[7], 300);
  } else if (digitalRead(inputPin9) == HIGH) {
    playNote(notes[8], 300);
  } else if (digitalRead(inputPin10) == HIGH) {
    playNote(notes[9], 300);
  } else if (digitalRead(inputPin11) == HIGH) {
    playNote(notes[10], 300);
  } else if (digitalRead(inputPin12) == HIGH) {
    playNote(notes[11], 300);
  } else {
    digitalWrite(speakerPin, LOW);


I think you may send tone and duration on the serial line and capture them on the serial monitor on the PC. You already have this:
Code: [Select]

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    digitalWrite(speakerPin, LOW);

Then you can create a function such as:
Code: [Select]

void sendTone(int tone, int duration) {
  Serial.print(": ");
  Serial.print(", ");

And call the function immediately after playTone(). This will record the sound and duration along with its start time in milliseconds (you can also play with micros(), but I'm not sure the difference will be noticeable).

Please remember, when you post your code, to enclose it within [ code] tags [/ code] (the # button).


if i understand you want to play something on the Arduino using buttons connected to digital pins and then have the Arduino replay it ?

If so read up on arrays.

1) A very basic program with an array will allow you to record the sequence of notes played.

2) A next step would then be to record the length of each note played - i would do this by recording the start and stop time and then convert that into beats, it takes a lot less memory to store the beats than the actual start stop times, its also very easy to replay at different tempos then.

have a go at 1) first and then we can get into more detail on representing beats and fractions of a beat.

Duane B

Read this
then watch this



There are couple links with sample code,

probably, you can modify 1D [] array to 2D [][] and save time and note.


Hi guys! I really appreciate that you are trying to help me out. But I am really new to this stuff. I'm sorry but I cant apply the advices that you have given me. I understand that I also need to do some readings on my own but if you guys could already give me the exact and the ready to upload program codes for my piano with recorder, it would be highly appreciated. Thanks guys. I know that I'm asking too much but please I really need it. Thanks again! :)

Go Up