How to : Piano with recorder

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);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}

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);_
_
}_
_
}*_

Hi,
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:

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

Then you can create a function such as:

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

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
rcarduino.blogspot.com

There are couple links with sample code,

http://cuartielles.com/verkstad/en/Week3/KonockKnockBox

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! :slight_smile: