Está en la página 1de 11

/*

* LilyPad tutorial: sound


*
* Uses a LilyPad speaker module to produce simple musical notes
* For a chart of the frequencies of different notes see:
* http://www.phy.mtu.edu/~suits/notefreqs.html
*/
int ledPin = 13;
int speakerPin = 9;

// LED is connected to digital pin 13


// speaker connected to digital pin 9

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(speakerPin, OUTPUT);
}

// sets the ledPin to be an output


// sets the speakerPin to be an output

void loop()
// run over and over again
{
scale();
// call the scale() function
delay(1000); // delay for 1 second
}
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMillisecon
ds)
// the sound producing function
{
int x;
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin,LOW);
delayMicroseconds(delayAmount);
}
}
void scale ()
{
digitalWrite(ledPin,HIGH);
beep(speakerPin,2093,500);
linked to above) for 500ms
beep(speakerPin,2349,500);
beep(speakerPin,2637,500);
beep(speakerPin,2793,500);
beep(speakerPin,3136,500);
beep(speakerPin,3520,500);
beep(speakerPin,3951,500);
beep(speakerPin,4186,500);
digitalWrite(ledPin,LOW);
}

//turn on the LED


//C: play the note C (C7 from the chart
//D
//E
//F
//G
//A
//B
//C
//turn off the LED

_------------------------------------------------------------------------------------------------------

/*
* LilyPad tutorial: sound and light
*
* Uses a LilyPad speaker module to produce simple musical notes
* For a chart of the frequencies of different notes see:
* http://www.phy.mtu.edu/~suits/notefreqs.html
*/
int speakerPin = 7;
int speakerPin2 = 12;

// on ProtoSnap, buzzer is pin 7


// .. and 12.

int ledPin5 = 5; // LED is connected to digital pin 5


int ledPin6 = 6; // LED is connected to digital pin 6
int ledPinA2 = A2; // LED is connected to digital pin a2
int ledPinA3 = A3; // LED is connected to digital pin a3
int ledPinA4 = A4; // LED is connected to digital pin a4

void setup()
{
pinMode(ledPin5, OUTPUT); // sets the ledPin to be an output
pinMode(ledPin6, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinA2, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinA3, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinA4, OUTPUT); // sets the ledPin to be an output
pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output
pinMode(speakerPin2, OUTPUT); // we ll use this one as ground - neither
digitalWrite(speakerPin2,LOW); // pin does PWM, so it doesn t matter whic
h
}

void loop() // run over and over again


{

scale(); // call the scale() function


delay(1000); // delay for 1 second
}

void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMillisecon


ds)
// the sound producing function
{
int x;
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin,LOW);
delayMicroseconds(delayAmount);
}
}

void scale ()
{
digitalWrite(ledPin5,HIGH); //turn on the LED5
delay(100);
digitalWrite(ledPin6,HIGH); //turn on the LED6
delay(100);
digitalWrite(ledPinA2,HIGH); //turn on the LEDA2
delay(100);
digitalWrite(ledPinA3,HIGH); //turn on the LEDA3
delay(100);
digitalWrite(ledPinA4,HIGH); //turn on the LEDA4
delay(100);

beep(speakerPin,2093,500); //C: play the note C (C7 from the chart lin
ked to above) for 500ms
beep(speakerPin,2349,500); //D
beep(speakerPin,2637,500); //E
beep(speakerPin,2793,500); //F
beep(speakerPin,3136,500); //G
beep(speakerPin,3520,500); //A
beep(speakerPin,3951,500); //B
beep(speakerPin,4186,500); //C
beep(speakerPin,3951,500); //B
beep(speakerPin,3520,500); //A
beep(speakerPin,3136,500); //G
beep(speakerPin,2793,500); //F
beep(speakerPin,2637,500); //E
beep(speakerPin,2349,500); //D
digitalWrite(ledPinA4,LOW); //turn on the LED5
delay(100);
digitalWrite(ledPinA3,LOW); //turn on the LED6
delay(100);
digitalWrite(ledPinA2,LOW); //turn on the LEDA2
delay(100);
digitalWrite(ledPin6,LOW); //turn on the LEDA3
delay(100);
digitalWrite(ledPin5,LOW); //turn on the LEDA4

================================================================================
=
Second version, with volume control set using analogWrite()

/* Play Melody
* ----------*
* Program to play melodies stored in an array, it requires to know
* about timing issues and about how to play tones.
*
* The calculation of the tones is made following the mathematical
* operation:
*
*
timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note
frequency
period PW (timeHigh)
* c
261 Hz
3830
1915
* d
294 Hz
3400
1700
* e
329 Hz
3038
1519
* f
349 Hz
2864
1432
* g
392 Hz
2550
1275
* a
440 Hz
2272
1136
* b
493 Hz
2028
1014
* C
523 Hz
1912
956
*
* (cleft) 2005 D. Cuartielles for K3
*/
int ledPin = 13;
int speakerOut = 9;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//
10
20
30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
analogWrite(speakerOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
analogWrite(speakerOut,500);
delayMicroseconds(tones[count2]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
analogWrite(speakerOut, 0);
delayMicroseconds(500);

}
}
}
}
}

void buzz(int targetPin, long frequency, long length) {


digitalWrite(13,HIGH);
long delayValue = 1000000/frequency/2; // calculate the delay value between tr
ansitions
//// 1 second's worth of microseconds, divided by the frequency, then split in
half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 1000; // calculate the number of cycles f
or proper timing
//// multiply frequency, which is really cycles per second, by the number of s
econds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the d
iaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the di
aphram
delayMicroseconds(delayValue); // wait again or the calculated delay value
}
digitalWrite(13,LOW);
================================================================================
================================================

digitalWrite(13,HIGH);
tone(speaker, NOTE_CS4,eigth);
delay(eigth);
digitalWrite(13,LOW);
digitalWrite(12,HIGH);
tone(speaker, NOTE_E4,eigth);
delay(eigth);
digitalWrite(12,LOW);
digitalWrite(11,HIGH);
tone(speaker, NOTE_CS4,eigth);
delay(eigth);
digitalWrite(11,LOW);
digitalWrite(10,HIGH);
tone(speaker, NOTE_B3,dottedquarter);
delay(dottedquarter);
digitalWrite(10,LOW);
delay(quarter);
----

void playNoteAndLight(int note,int length)


{
int ledNum = leds[led];
digitalWrite(ledNum,HIGH);
tone(speaker, note,length);
delay(length);
digitalWrite(ledNum,LOW);
led++;
if(led>num){
led = 0;
}

-------------------void loop()
{
/* jingle bells */
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_E4,half);
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_E4,half);
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_G4,quarter);
playNoteAndLight(NOTE_C4,dottedquarter);
playNoteAndLight(NOTE_D4,eighth);
playNoteAndLight(NOTE_E4,whole);
playNoteAndLight(NOTE_F4,quarter);
playNoteAndLight(NOTE_F4,quarter);
playNoteAndLight(NOTE_F4,quarter);
playNoteAndLight(NOTE_F4,quarter);
playNoteAndLight(NOTE_F4,quarter);
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_E4,half);
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_D4,quarter);
playNoteAndLight(NOTE_D4,quarter);
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_D4,half);
playNoteAndLight(NOTE_G4,half);
/* second verse */
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_E4,half);

playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_E4,half);
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_G4,quarter);
playNoteAndLight(NOTE_C4,dottedquarter);
playNoteAndLight(NOTE_D4,eighth);
playNoteAndLight(NOTE_E4,whole);
playNoteAndLight(NOTE_F4,quarter);
playNoteAndLight(NOTE_F4,quarter);
playNoteAndLight(NOTE_F4,quarter);
playNoteAndLight(NOTE_F4,quarter);
playNoteAndLight(NOTE_F4,quarter);
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_E4,quarter);
playNoteAndLight(NOTE_E4,eighth);
playNoteAndLight(NOTE_E4,eighth);
playNoteAndLight(NOTE_G4,quarter);
playNoteAndLight(NOTE_G4,quarter);
playNoteAndLight(NOTE_F4,quarter);
playNoteAndLight(NOTE_D4,quarter);
playNoteAndLight(NOTE_C4,whole);
}

int
int
int
int
int

eighth = 250;
quarter = 500;
dottedquarter = 750;
half = 1000;
whole = 2000;

int
int
int
int
int

eighth = 0;
quarter = 2;
dottedquarter = 3;
half = 4;
whole = 8;

{
eighth = starter;
quarter = quarter*starter;
dottedquarter = dottedquarter*starter;
half = half*starter;
whole = whole*starter;
}

/*
Tom Haas, 20120611
electronic_birthday_card.ino
This sketch is for an interactive birthday card
that will play "Happy Birthday" and blink some LEDs
upon tripping a switch.
The switch is a reed switch, tripped by a magnet,
and the LEDs are yellow LEDs that blink in time
with the "Happy Birthday" song,
played by a piezo speaker.
Uses the "Tone" Arduino library by bhagman@roguerobotics.com.
In Tone.cpp,
#include <wiring.h> --> #include <Arduino.h>
in order for it to work on my install.
*/
#include <Tone.h>
#define ON HIGH
#define OFF LOW
// For input/output set up
int speakerPin = 9;
int ledPins[] = { 5, 6 };
int inputPin = 10; //reed switch
int inputVal = 0;
// Calculate number of LEDs
int ledCount = sizeof(ledPins) / sizeof(int);
Tone makeTone;
// Song to play
int notes[] = {
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, 0,
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4, 0,
NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_E4, NOTE_D4, 0,
NOTE_AS4, NOTE_AS4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4, 0 }; // notes to play;
see Tone.h for frequencies; 0 --> PAUSE
int songLength = sizeof(notes) / sizeof(int); // Calculate song length
int beats[] = {
1, 1, 2, 2, 2, 4, 2,
1, 1, 2, 2, 2, 4, 2,
1, 1, 2, 2, 2, 2, 4, 2,
1, 1, 2, 2, 2, 4, 4 }; // number of beats for each note
int numberBeats = sizeof(beats) / sizeof(int); // Calculate number of beats

int tempo = 200; // in milliseconds


// End song
void animateComponents(int note, int beat, int tempo, int leds[], int numberLeds
) {
makeTone.stop(); // speaker reset
ledSwitch(leds, numberLeds, ON);
makeTone.play(note); // play tone
delay(tempo * beat); // for specified number of beats
makeTone.stop(); // speaker reset
ledSwitch(leds, numberLeds, OFF);
delay(tempo / 2); // pause between notes
}
void playPause(int beat, int tempo) {
delay(tempo * beat); // pause for specified number of beats
delay(tempo / 2); // pause between notes
}
void ledSwitch(int leds[], int numberLeds, int pinVoltage) {
for (int i = 0; i < numberLeds; i++) {
digitalWrite(leds[i], pinVoltage); // turn on/off LED
}
}
void setup() {
for(int i = 0; i < ledCount; i++){
pinMode(ledPins[i], OUTPUT); // set up LEDs for writing
}
pinMode(inputPin, INPUT); // set up reed switch
makeTone.begin(speakerPin); // set up piezo speaker
}
void loop() {
inputVal = digitalRead(inputPin); // default (not switched) = HIGH
if (inputVal == LOW) { // do nothing until LOW, then play song
if (songLength > 0 && numberBeats > 0 && tempo > 0 && songLength == numberBe
ats) { // Simple song error check
for (int i = 0; i < songLength; i++) { // loop through song
if (notes[i] == 0) { // PAUSE
playPause(beats[i], tempo);
}
else {
animateComponents(notes[i], beats[i], tempo, ledPins, ledCount); // ma
ke sound & light up LEDs
}
}
}
else { // Play on song error
makeTone.stop();
makeTone.play(NOTE_C2);
delay(1000);
makeTone.play(NOTE_C1);
delay(2000);
makeTone.stop();
}
}
}

/* Simple Tone Keyboard


Gregg Horton 2011
*/
#include "pitches.h"
const int buttonPin = 2; // the number of the pushbutton pin
int note1 = NOTE_C4; // define note sound
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// sound tone
tone(8, note1);
}
else {
//turn off sound
noTone(8);
}
}

También podría gustarte