Transcription of 임베디드시스템개론 - mclab.silla.ac.kr
1 : Arduino Lecture #7: Piezzo Buzzer & Flex Sensor 2012. 4. 27 by `Piezzo Buzzer `To n e `Piezzo Sensor `Flex Sensor `Flex Sensor 2 Piezzo Buzzer (1)` Piezzo Buzzers`Piezoelectonics`piezein is greek for squeeze `Some crystals, when squeezed, make a spark Spark a quartz crystal, and it flexes`Piezo buzzers use this to make sound flex something back and forth, it moves air`Piezo buzzers don t have quartz crystals, but instead a kind of ceramic that also exhibits piezoelectric properties`Two wires, red & black` : black=ground`Apply an oscillating voltage to make a noise`The buzzer case supports the piezzoelement and has resonant cavity for sound3 Piezzo Buzzer (2)` Piezzo Buzzers`Piezzo element` ` ` 4 (1)` `Piezzo buzzer .`.
2 ` `Piezzo buzzer .` , .5 (2)` 6 (3)` 7/* Melody* This example uses a piezo speaker to play melodies. It sends* a square wave of the appropriate frequency to the piezo, generating* the corresponding tone.** The calculation of the tones is made following the mathematical* operation: timeHigh = period / 2 = 1 / (2 * toneFrequency) * where the different tones are described as in the table:** note frequency period 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* C523 Hz1912 956** */ (4)` 8int speakerPin = 7;int length = 8; // the number of noteschar notes[] = "cdefgabC "; // a space represents a restint tempo = 300;Int serByte;void playTone(int tone, int duration) {for (long i = 0; i < duration * 1000L; i += tone * 2) {digitalWrite(speakerPin, HIGH); delayMicroseconds(tone);digitalWrite(spe akerPin, LOW); delayMicroseconds(tone).}}
3 }}void playNote(char note, int duration) {static char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };static int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };// play the tone corresponding to the note namefor (int i = 0; i < 8; i++) {if (names[i] == note) {playTone(tones[i], duration); break;}} (5)` 9void setup() { (115200);pinMode(speakerPin, OUTPUT);for (int i = 0; i < length; i++) {playNote(notes[i], tempo);}}void loop() {while ( ()) {serByte = ();playNote(serByte, tempo); (serByte);}} (6)` ` 10 (7)` ` Piezzo buzzer .11 (8)` 12int ledPin = 13;int speakerOut = 7; byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'}; int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2 g2p8p8p8p";// 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 30int 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 = !}
4 StatePin;digitalWrite(ledPin, statePin); (9)` 13for (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);delayMicrose conds(tones[count2]);analogWrite(speaker Out, 0);delayMicroseconds(tones[count2]);} if (melody[count*2 + 1] == 'p') {// make a pause of a certain sizeanalogWrite(speakerOut, 0);delayMicroseconds(500);}}}}}Arduino Tone Library (1)`Arduino Tone Library`Arduino core tone `tone(pin, frequency, duration)`noTone(pin)` pin square-wave pulse (50% duty cycle) ` ` pin piezo buzzer `RTTTL (RingTone Text Transfer Language) 14 Arduino Tone Library (2)`Tone (1)` 15 Arduino Tone Library (3)`Tone (2)` 16/*Melody : Plays a melody circuit: 8-ohm speaker on digital pin 8 */#include " "// notes in the melody:int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};// note durations: 4 = quarter note, 8 = eighth note, etc.
5 :int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 };void setup() {// iterate over the notes of the melody:for (int thisNote = 0; thisNote < 8; thisNote++) {// to calculate the note duration, take one second divided by the note quarter note = 1000 / 4, eighth note = 1000/8, noteDuration = 1000/noteDurations[thisNote];tone(8, melody[thisNote],noteDuration);Arduino Tone Library (4)`Tone (3)` 17// to distinguish the notes, set a minimum time between the note's duration + 30% seems to work well:int pauseBetweenNotes = noteDuration * ;delay(pauseBetweenNotes);// stop the tone playing:noTone(8);}}void loop() {// no need to repeat the melody.}Arduino Tone Library (5)` (1)` 18 Arduino Tone Library (6)` (2)` 19void setup() {// nothing to do here}void loop() {// get a sensor reading:int sensorReading = analogRead(A0);// map the results from the sensor reading's range// to the desired pitch range:float frequency = map(sensorReading, 200, 900, 100, 1000);// change the pitch, play for 10 ms:tone(8, frequency, 10).}
6 }Arduino Tone Library (7)`RTTTL Player (1)`RTTTL(RingTone Text Transfer Language)` `RTTTL Player ` 20 Arduino Tone Library (8)`RTTTL Player (2)` 21/** : Public Constants**/#define NOTE_B0 31#define NOTE_C1 33#define NOTE_CS1 35#define NOTE_D1 37#define NOTE_DS1 39#define NOTE_E1 41#define NOTE_F1 44#define NOTE_FS1 46#define NOTE_G1 49#define NOTE_GS1 52#define NOTE_A1 55#define NOTE_AS1 58#define NOTE_B1 62#define NOTE_C2 65:::Arduino Tone Library (9)`RTTTL Player (3)` 22#include " #define OCTAVE_OFFSET 0int notes[] = { 0,NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7};//char *song = "The Simpsons:d=4,o=5,b=160 ,e6,f#6,8a6, ,e6,c6,8a,8f#,8f#,8f#,2g,8p,8p,8f#,8f#,8 f#,8g,a#.
7 ,8c6,8c6,8c6,c6";//char *song = "Indiana:d=4,o=5,b=250:e,8p,8f,8g,8p,1c6 ,8p.,d,8p,8e,1f,p.,g,8p,8a,8b,8p,1f6,p,a ,8p,8b,2c6,2d6,2e6,e,8p,8f,8g,8p,1c6,p,d 6,8p,8e6, ,g,8p,8g, ,8p,d6,8p,8g, ,8p,d6,8p,8g, ,8p,e6,8p,8d6,2c6";//char *song = "TakeOnMe:d=4,o=4,b=160:8f#5,8f#5,8f#5,8 d5,8p,8b,8p,8e5,8p,8e5,8p,8e5,8g#5,8g#5, 8a5,8b5,8a5,8a5,8a5,8e5,8p,8d5,8p,8f#5,8 p,8f#5,8p,8f#5,8e5,8e5,8f#5,8e5,8f#5,8f# 5,8f#5,8d5,8p,8b,8p,8e5,8p,8e5,8p,8e5,8g #5,8g#5,8a5,8b5,8a5,8a5,8a5,8e5,8p,8d5,8 p,8f#5,8p,8f#5,8p,8f#5,8e5,8e5";//char *song = "Entertainer:d=4,o=5,b=140:8d,8d#,8e,c6, 8e,c6,8e, ,8c6,8d6,8d#6,8e6,8c6,8d6,e6,8b,d6,2c6,p ,8d,8d#,8e,c6,8e,c6,8e, ,8p,8a,8g,8f#,8a,8c6,e6,8d6,8c6,8a,2d6"; //char *song = "Muppets:d=4,o=5,b=250:c6,c6,a,b,8a,b,g, p,c6,c6,a,8b,8a,8p,g.,p,e,e,g,f,8e,f,8c6 ,8c,8d,e,8e,8e,8p,8e,g,2p,c6,c6,a,b,8a,b ,g,p,c6,c6,a,8b,a,g.,p,e,e,g,f,8e,f,8c6, 8c,8d,e,8e,d,8d,c ;Arduino Tone Library (10)`RTTTL Player (4)` 23char *song = "Xfiles:d=4,o=5,b=125:e,b,a,b,d6,2b.
8 ,1p,e,b,a,b,e6,2b.,1p,g6,f#6,e6,d6,e6,2b .,1p,g6,f#6,e6,d6,f#6,2b.,1p,e,b,a,b,d6, 2b.,1p,e,b,a,b,e6,2b.,1p,e6,2b.";//char *song = "Looney:d=4,o=5,b=140:32p,c6,8f6,8e6,8d6 ,8c6,a.,8c6,8f6,8e6,8d6,8d#6, ,8e6,8e6,8c6,8d6,8c6,8e6,8c6,8d6,8a,8c6, 8g,8a#,8a,8f";//char *song = "20thCenFox:d=16,o=5,b=140:b,8p,b,b,2b,p ,c6,32p,b,32p,c6,32p,b,32p,c6,32p,b,8p,b ,b,b,32p,b,32p,b,32p,b,32p,b,32p,b,32p,b ,32p,g#,32p,a,32p,b,8p,b,b,2b,4p,8e,8g#, 8b,1c#6,8f#,8a,8c#6,1e6,8a,8c#6,8e6,1e6, 8b,8g#,8a,2b";//char *song = "Bond:d=4,o=5,b=80:32p,16c#6,32d#6,32d#6 ,16d#6,8d#6,16c#6,16c#6,16c#6,16c#6,32e6 ,32e6,16e6,8e6,16d#6,16d#6,16d#6,16c#6,3 2d#6,32d#6,16d#6,8d#6,16c#6,16c#6,16c#6, 16c#6,32e6,32e6,16e6,8e6,16d#6,16d6,16c# 6,16c#7, ,16g#6,16f#6,g#.6";//char *song = "MASH:d=8,o=5,b=140:4a,4g,f#,g,p,f#,p,g, p,f#,p,2e.,p,f#,e,4f#,e,f#,p,e,p,4d.,p,f #,4e,d,e,p,d,p,e,p,d,p,2c#.,p,d,c#,4d,c# ,d,p,e,p,4f#,p,a,p,4b,a,b,p,a,p,b,p,2a.
9 ,4p,a,b,a,4b,a,b,p,2a.,a,4f#,a,b,p,d6,p, ,d6,b,p,a,p,2b";//char *song = "StarWars:d=4,o=5,b=45:32p,32f#,32f#,32f #,8b.,8f#.6,32e6,32d#6,32c#6, ,16f#.6,32e6,32d#6,32c#6, ,16f#.6,32e6,32d#6,32e6,8c#.6,32f#,32f#, 32f#,8b.,8f#.6,32e6,32d#6,32c#6, ,16f#.6,32e6,32d#6,32c#6, ,16f#.6,32e6,32d#6,32e6,8c#6";//char *song = "GoodBad:d=4,o=5,b=56:32p,32a#,32d#6,32a #,32d#6,8a#.,16f#.,16g#.,d#,32a#,32d#6,3 2a#,32d#6,8a#.,16f#.,16g#.,c#6,32a#,32d# 6,32a#,32d#6,8a#.,16f#.,32f.,32d#.,c#,32 a#,32d#6,32a#,32d#6,8a#.,16g#.,d#";//cha r *song = "TopGun:d=4,o=4,b=31:32p,16c#,16g#,16g#, 32f#,32f,32f#,32f,16d#,16d#,32c#,32d#,16 f,32d#,32f,16f#,32f,32c#,16f,d#,16c#,16g #,16g#,32f#,32f,32f#,32f,16d#,16d#,32c#, 32d#,16f,32d#,32f,16f#,32f,32c#,g#";//ch ar *song = "A-Team:d=8,o=5,b=125:4d#6,a#,2d#6,16p,g #,4a#,4d#.,p,16g,16a#,d#6,a#,f6,2d#6,16p ,c#.6,16c6,16a#,g#.,2a#";//char *song = "Flinstones:d=4,o=5,b=40:32p,16f6,16a#,1 6a#6,32g6,16f6,16a#.
10 ,16f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16 c6,d6,16f6,16a#.,16a#6,32g6,16f6,16a#.,3 2f6,32f6,32d#6,32d6,32d6,32d#6,32f6,16a# ,16c6,a#,16a6, ,16a#6,32a6,32a6,32g6,32f#6,32a6,8g6,16g 6, ,32a6,32a6,32g6,32g6,32f6,32e6,32g6,8f6, 16f6,16a#.,16a#6,32g6,16f6,16a#.,16f6,32 d#6,32d6,32d6,32d#6,32f6,16a#, ,32d6,32d#6,32f6,16a#, ,32d6,32d#6,32f6,16a#6,16c7,8a#.6";//cha r *song = "Jeopardy:d=4,o=6,b=125:c,f,c,f5,c,f,2c, c,f,c,f,a.,8g,8f,8e,8d,8c#,c,f,c,f5,c,f, 2c,f.,8d,c,a#5,a5,g5,f5,p,d#,g#,d#,g#5,d #,g#,2d#,d#,g#,d#,g#, ,8a#,8g#,8g,8f,8e,d#,g#,d#,g#5,d#,g#,2d# ,g#.,8f,d#,c#,c,p,a#5,p,g#.5,d#,g# ;Arduino Tone Library (11)`RTTTL Player (5)` 24//char *song = "Gadget:d=16,o=5,b=50:32d#,32f,32f#,32g# ,a#,f#,a,f,g#,f#,32d#,32f,32f#,32g#,a#,d #6,4d6,32d#,32f,32f#,32g#,a#,f#,a,f,g#,f #,8d#";//char *song = "Smurfs:d=32,o=5,b=200:4c#6,16p,4f#6,p,1 6c#6,p,8d#6,p,8b,p,4g#,16p,4c#6,p,16a#,p ,8f#,p,8a#,p,4g#,4p,g#,p,a#,p,b,p,c6,p,4 c#6,16p,4f#6,p,16c#6,p,8d#6,p,8b,p,4g#,1 6p,4c#6,p,16a#,p,8b,p,8f,p,4f#";//char *song = "MahnaMahna:d=16,o=6,b=125:c#,c.