Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "application.h"
- // name the pins
- #define BUTTONPIN D2
- #define BUZZERPIN D4
- int press = 0;
- int oldPress = 0;
- int state = 0;
- int melody[] = {4186,2093,1047,523,262,131,65,33}; // notes in the melody
- int noteDurations[] = {4,8,8,4,4,4,4,4 }; // note durations
- int melody2[] = {33,131,65,262,1047,523,2093,4186}; // notes in the melody
- int noteDurations2[] = {2,4,4,3,3,2,1,1 }; // note durations
- // This routine runs only once upon reset
- void setup() {
- pinMode(BUTTONPIN, INPUT);
- pinMode(BUTTONPIN, OUTPUT);
- }
- void loop() {
- press = digitalRead(BUTTONPIN);
- if ( press == HIGH && oldPress == LOW) {
- state = 1 - state;
- if (state == 1) {
- tune1();
- }
- else {
- tune2();
- }
- }
- oldPress = press;
- }
- int tune1() {
- for (int thisNote = 0; thisNote < 8; thisNote++) { // ergodic all notes
- int noteDuration = 1000/noteDurations[thisNote]; // calculate the note duration
- tone(BUZZERPIN, melody[thisNote], noteDuration); // let speaker sonds
- int pauseBetweenNotes = noteDuration * 1.30; // set a minimum time between notes
- delay(pauseBetweenNotes); // delay for the while
- digitalWrite(BUZZERPIN,HIGH); // stop the tone playing/read button
- }
- }
- int tune2() {
- for (int thisNote = 0; thisNote < 8; thisNote++) { // ergodic all notes
- int noteDuration = 1000/noteDurations2[thisNote]; // calculate the note duration
- tone(BUZZERPIN, melody2[thisNote], noteDuration); // let speaker sonds
- int pauseBetweenNotes = noteDuration * 1.30; // set a minimum time between notes
- delay(pauseBetweenNotes); // delay for the while
- digitalWrite(BUZZERPIN,LOW); // stop the tone playing/read button
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement