1°) Emission de notes de musique :
Connecter le HP sur une broche digitale en ajoutant une résistance de 100 ohms.
Utilisez la commande tone (avec ici pin=8) :
tone(pin, frequency) ou tone(pin, frequency, duration)
Exemples de sketch :
http://arduino.cc/en/Tutorial/Tone3
et http://arduino.cc/en/Tutorial/Tone
2°) Lecture de fichier WAV :
Télécharger la librairie TMRpcm.h branchez une carte SD (cf. page SD) en ayant placé des fichiers au format wav et copiez le sketch suivant : (suivez le schéma ci-dessus décrit puis relier le HP en suivant les commentaires du sketch)
#include <SD.h> // need to include the SD library
//#define SD_ChipSelectPin 53 //example uses hardware SS pin 53 on Mega2560
//#define SD_ChipSelectPin 4 //using digital pin 4 on arduino nano 328, can use other pins
#include <TMRpcm.h> // also need to include this library...
TMRpcm tmrpcm; // create an object for use in this sketch
unsigned long time = 0;
void setup(){
tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
//Complimentary Output or Dual Speakers:
//pinMode(10,OUTPUT); Pin pairs: 9,10 Mega: 5-2,6-7,11-12,46-45
Serial.begin(9600);
pinMode(13,OUTPUT); //LED Connected to analog pin 0
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
tmrpcm.play("tada.wav"); //the sound file "music" will play each time the arduino powers up, or is reset
}
void loop(){
//blink the LED manually to demonstrate music playback is independant of main loop
if(tmrpcm.isPlaying() && millis() - time > 50 ) {
digitalWrite(13,!digitalRead(13));
time = millis();
}else
if(millis() - time > 500){
digitalWrite(13,!digitalRead(13));
time = millis();
}
if(Serial.available()){
switch(Serial.read()){
case 'd': tmrpcm.play("taime.wav"); break;
case 'P': tmrpcm.play("tada.wav"); break;
case 't': tmrpcm.play("theme.wav"); break;
case 'p': tmrpcm.pause(); break;
case '?': if(tmrpcm.isPlaying()){ Serial.println("A wav file is being played");} break;
case 'S': tmrpcm.stopPlayback(); break;
case '=': tmrpcm.volume(1); break;
case '-': tmrpcm.volume(0); break;
case '0': tmrpcm.quality(0); break;
case '1': tmrpcm.quality(1); break;
default: break;
}
}
}
Références :
http://www.instructables.com/id/Arduino-Basics-Making-Sound/?ALLSTEPS
http://arduino.cc/en/Tutorial/Tone
http://maxoffsky.com/maxoffsky-blog/how-to-play-wav-audio-files-with-arduino-uno-and-microsd-card/
Votre commentaire