Новогодняя песенка
- Войдите на сайт для отправки комментариев
Вс, 15/06/2014 - 14:12
Привет!=) Задали проект сделать, новогоднюю гирлянду, чтобы она пела и включалась на хлопки. Помогите пожалуйста с кодом для новогодней песенки (любой).
int speakerPin = 9;
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;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, 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);
}
}
}
void setup() {
pinMode(speakerPin, 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);
}
}
Не мешало бы показать схему включения ардуины и того, что превратит её в новогоднюю гирлянду и заставит срабатывать от хлопков. Можно конечно и по коду разобраться, что к чему. Но возникает вопрос - это надо Вам или тем, кто мог бы помочь?
А чем помочь то? Написать и отладить за Вас код?
непонятно, а что не работает? или там песенка не совсем новогодняя? Если дело в песенке так это на музыкальный форум...
Если нужно одновременно еще что-то делать(мигать светодиодами) то такой код конечно не прокатит - он будет только песенку выводить...
Я сегодня добрый поэтому вот код переделанный для функции tone()
int speakerPin = 9; // Динамик int inPin = 8; // Кнопка char notes[] = "c1c1g1g1a1a1g2f1f1e1e1d1d1c2 9"; // a space represents a rest int nsize = sizeof(notes); int tempo = 200; long offset = 0; long t_delay = 0; int i = 0; void playNote(char note, int duration) { char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', //Первая октава 'C', 'D', 'E', 'F', 'G', 'A', 'B' };//Вторая октава int tones[] = { 261, 293, 329, 349, 392, 440, 493, 523, 587, 659, 698, 784, 880, 987 }; // play the tone corresponding to the note name for (int i = 0; i < sizeof(names); i++) { if (names[i] == note) { tone(speakerPin, tones[i], duration*tempo); playPause(duration); } } } void playPause(int duration){ offset = micros(); t_delay = tempo*(duration*1000L+500L); } boolean isReady(){ return micros() - offset >= t_delay; } void setup() { pinMode(speakerPin, OUTPUT); pinMode(inPin, INPUT); digitalWrite(inPin, HIGH); } void loop() { if(isReady()&&i < nsize){ if (notes[i] == ' ') { playPause((notes[i+1]-'0')); // rest } else { playNote(notes[i], (notes[i+1]-'0')); } i+=2; } if(digitalRead(inPin) == LOW && i >= nsize)i = 0; }Как вседа пишу код для "обезьянок с клавиатурой"