本帖最后由 dvd1478 于 2016-8-3 11:29 编辑
随手做的一个练习,红蓝绿三个LED灯,随着音乐的变化而发生变化。
音乐是由蜂鸣器产生的,响声的长度决定着音调
timeHigh = period / 2 = 1 / (2 * toneFrequency)
* 音的表示方法是 1 2 3 4 5 6 7
* 音的名称(也就是音名) C D E F G A B
* 音的唱名(也就是唱法) DO RE MI FA SOL LA SI
*
* 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
* C 523 Hz 1912 956
Edison 采用arduino的编译器进行下载
代码如下:
- /* Melody
- * (cleft) 2005 D. Cuartielles for K3
- *
- * 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:
- *
- * 音的表示方法是 1 2 3 4 5 6 7
- * 音的名称(也就是音名) C D E F G A B
- * 音的唱名(也就是唱法) DO RE MI FA SOL LA SI
- *
- * 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
- * C 523 Hz 1912 956
- *
- * http://www.arduino.cc/en/Tutorial/Melody
- */
- int speakerPin = 2; // Grove Buzzer connect to D3
- int length = 15; // the number of notes // 音调的数目
- char notes[] = "ccggaagffeeddc "; // a space represents a rest // 字符最后的空格代表乐曲间的休息
- int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
- int tempo = 300;
- /* 拍子的时长,
- 音乐里的牌子是更具乐曲要求而定的。如果说乐曲寻求规定的速度是每分60拍的话,那么没拍占用的时间就是60分之一,
- 也就是一秒,半拍为二分之一秒。若规定速度为每分钟120拍的话,那么灭一排站的时间是半秒,半拍十四分之一秒。
- 以此类推,这是拍值。当各种牌制定下来以后,各种拍之也就与牌子连在一起。比如,2/4拍,是以四分音符为一拍,每小节有两拍
- http://blog.sina.com.cn/s/blog_5ef4d679010172do.html
- */
- const int pinLedRed = 3;
- const int pinLedGreen = 4;
- const int pinLedBlue = 5;
- void selectRGB(int i)
- {
- switch (i)
- {
- case 0:
- digitalWrite(pinLedRed,HIGH);//红色LED亮
- digitalWrite(pinLedGreen,LOW);
- digitalWrite(pinLedBlue,LOW);
- break;
- case 1:
- digitalWrite(pinLedRed,HIGH);//红、绿色LED亮
- digitalWrite(pinLedGreen,HIGH);
- digitalWrite(pinLedBlue,LOW);
- break;
- case 2:
- digitalWrite(pinLedRed,LOW);//绿色LED亮
- digitalWrite(pinLedGreen,HIGH);
- digitalWrite(pinLedBlue,LOW);
- break;
- case 3:
- digitalWrite(pinLedRed,LOW);//绿、蓝色LED亮
- digitalWrite(pinLedGreen,HIGH);
- digitalWrite(pinLedBlue,HIGH);
- break;
- case 4:
- digitalWrite(pinLedRed,LOW);//蓝色LED亮
- digitalWrite(pinLedGreen,LOW);
- digitalWrite(pinLedBlue,HIGH);
- break;
- case 5:
- digitalWrite(pinLedRed,HIGH);//红、蓝色LED亮
- digitalWrite(pinLedGreen,LOW);
- digitalWrite(pinLedBlue,HIGH);
- break;
- case 6:
- digitalWrite(pinLedRed,HIGH);//红、绿、蓝色LED亮
- digitalWrite(pinLedGreen,HIGH);
- digitalWrite(pinLedBlue,HIGH);
- break;
- default:
- digitalWrite(pinLedRed,LOW);//全灭
- digitalWrite(pinLedGreen,LOW);
- digitalWrite(pinLedBlue,LOW);
- break;
- }
- }
- void playTone(int tone, int duration) {
- for (long i = 0; i < duration * 1000L; i += tone * 2) {
- digitalWrite(speakerPin, HIGH);
- //digitalWrite(pinLed, HIGH);
- delayMicroseconds(tone);
- digitalWrite(speakerPin, LOW);
- //digitalWrite(pinLed, LOW);
- 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[i] == note) {
- playTone(tones[i], duration);
- selectRGB(i);
- }
- }
- }
- void setup()
- {
- pinMode(speakerPin, OUTPUT);
- // Configure the LED's pin for output signals.
- pinMode(pinLedRed, OUTPUT);
- pinMode(pinLedGreen, OUTPUT);
- pinMode(pinLedBlue, OUTPUT);
- }
- void loop()
- {
- for (int i = 0; i < length; i++)
- {
- if (notes[i] == ' ')
- {
- delay(beats[i] * tempo); // rest
- }
- else
- {
- playNote(notes[i], beats[i] * tempo);
- }
- // pause between notes
- delay(tempo / 2);
- }
- }
复制代码
|