Documentos de Académico
Documentos de Profesional
Documentos de Cultura
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(speakerPin, 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);
}
_------------------------------------------------------------------------------------------------------
/*
* 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;
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 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);
}
}
}
}
}
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 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