Pages: [1]   Go Down
Author Topic: How to : Piano with recorder  (Read 521 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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:
Code:
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).
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged


Montreal
Offline Offline
Faraday Member
**
Karma: 30
Posts: 2602
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are couple links with sample code,
http://dduino.blogspot.ca/2011/12/arduino-tap-back.html
http://cuartielles.com/verkstad/en/Week3/KonockKnockBox

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

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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! smiley
Logged

Pages: [1]   Go Up
Jump to: