Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- int speakerPin = 9;
- int servoPin = 3;
- Servo servo1;
- //sangen tatt fra https://gist.github.com/ianklatzco/9127560
- const int button1 = 6; //knapp 1
- const int button2 = 5; // knapp 2
- char notes[] = "gabygabyxzCDxzCDabywabywzCDEzCDEbywFCDEqywFGDEqi azbC"; // a space represents a rest
- int length = sizeof(notes); // the number of notes
- int beats[] = { 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 2,3,3,16,};
- int tempo = 75;
- void setup() {
- pinMode(button1, INPUT);
- pinMode(button2, INPUT);
- pinMode(12,OUTPUT);
- pinMode(13,OUTPUT);
- pinMode(8,OUTPUT);
- servo1.attach(servoPin);
- servo1.write(0);
- }
- void loop() {
- buttons();
- }
- void buttons()
- {
- if(digitalRead(button1) == LOW && digitalRead(button2) == HIGH)
- {
- playZelda();
- digitalWrite(12,HIGH);
- delay(200);
- digitalWrite(13,HIGH);
- delay(200);
- digitalWrite(8,HIGH);
- servo1.write(180);
- delay(4000);
- servo1.write(0);
- digitalWrite(8,LOW);
- delay(200);
- digitalWrite(12,LOW);
- delay(200);
- digitalWrite(13,LOW);
- }
- if(digitalRead(button2) == LOW && digitalRead(button1) == LOW)
- {
- servo1.write(189);
- delay(200);
- servo1.write(100);
- delay(200);
- servo1.write(0);
- delay(200);
- servo1.write(200);
- delay(200);
- servo1.write(69);
- delay(200);
- servo1.write(23);
- delay(200);
- servo1.write(0);
- delay(200);
- }
- }
- void playZelda()
- {
- pinMode(speakerPin, OUTPUT);
- 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);
- }
- delay(100);
- }
- 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', 'x', 'a', 'z', 'b', 'C', 'y', 'D', 'w', 'E', 'F', 'q', 'G', 'i' };
- // c=C4, C = C5. These values have been tuned.
- int tones[] = { 1898, 1690, 1500, 1420, 1265, 1194, 1126, 1063, 1001, 947, 893, 843, 795, 749, 710, 668, 630, 594 };
- // play the tone corresponding to the note name
- for (int i = 0; i < 18; i++) {
- if (names[i] == note) {
- playTone(tones[i], duration);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement