Go Down

Topic: how can I write the program abt 3 channels ?? (Read 379 times) previous topic - next topic

silver

int P1 = 9;
int P2 = 10;
int P3 = 11;

int length = 4; // the number of notes
char notes[] = "CDEF"; // a space represents a rest
int beats[] = { 1, 1, 1, 1 } ;
int tempo = 300;

void playTone(int tone, int duration) {
 for (long i = 0; i < duration * 1000L; i += tone * 2)
 if (tone > 1500 ){
   digitalWrite(P1, HIGH);
   digitalWrite(P2, LOW);
   digitalWrite(P3, LOW);}
 if (tone < 1500, tone > 1100 ){
   digitalWrite(P1, LOW);
   digitalWrite(P2, HIGH);
   digitalWrite(P3, LOW);}
 else{
   delayMicroseconds(tone);
   digitalWrite(P1, LOW);
   digitalWrite(P2, LOW);
   digitalWrite(P3, HIGH);
   delayMicroseconds(tone);}
}

void playNote(char note, int duration) {
 char names[] = { 'C', 'D', 'E', 'F', 'G', 'A', 'B', 'c' };
 int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
 
 // play the tone corresponding to the note name
 for (int i = 0; i < 8; i++) {
   if (names == note) {
     playTone(tones, duration);
   }
 }
}

void setup() {
 pinMode(P1, OUTPUT);
 pinMode(P2, OUTPUT);
 pinMode(P3, OUTPUT);
}

void loop() {
 for (int i = 0; i < length; i++) {
   if (notes == ' ') {
     delay(beats * tempo); // rest
   } else {
     playNote(notes, beats * tempo);
   }
   
   // pause between notes
   delay(tempo / 2);
 }
}

1. Can I write this to be a 3 channels PIANO?
2. the function 'CHAR' only can carry 1 character?
3. the timehigh range from XX to XX ....??

plz give me a hand>< thx all the brothers...

Grumpy_Mike

Please post code using the hash icon in the reply box.

Quote
Can I write this to be a 3 channels PIANO?

If you mean make the arduino sound like a piano then no it won't do this.

This program method will only play one note at a time.
For polyphonic programming see:-
http://www.arduino.cc/playground/Main/InterfacingWithHardware#Audio

Go Up