Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int speakerPin = 9;// piezo sounder connected to pin9
- const int btn1 = 2;
- const int btn2 = 3;
- const int led1 = 5;
- #define TUNE1 7
- const int notes_tune1[TUNE1] = {262, 262, 294, 262,349, 330, 0};
- const int beats_tune1[TUNE1] = {1, 1, 2, 2, 2, 4, 4 };
- #define TUNE2 15
- const int notes_tune2[TUNE2] = { 262, 262, 392, 392,440, 440, 392, 349,349, 330, 330, 294,294, 262, 0};
- const int beats_tune2[TUNE2] = {1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
- const int tempo = 300;
- //SETUP
- void setup()
- {
- pinMode(speakerPin, OUTPUT);
- pinMode(btn1, INPUT_PULLUP);
- pinMode(btn2, INPUT_PULLUP);
- pinMode(led1,OUTPUT);
- }
- void playTune1(){
- for (int i = 0; i < TUNE1; i++) {
- if (notes_tune1[i] == 0){
- delay(beats_tune1[i] * tempo); // rest
- }
- else {
- ourTone(notes_tune1[i], beats_tune1[i] * tempo);
- }
- delay(tempo / 2);
- }
- }
- void playTune2(){
- for (int i = 0; i < TUNE2; i++) {
- if (notes_tune2[i] == 0) {
- delay(beats_tune2[i] * tempo); // rest
- }
- else {
- ourTone(notes_tune2[i], beats_tune2[i] * tempo);
- }
- delay(tempo / 2);
- }
- }
- void ourTone(int freq, int duration) {
- tone(speakerPin, freq, duration);
- digitalWrite(led1,HIGH); //lights on
- delay(duration);
- digitalWrite(led1,LOW); //lights off
- noTone(speakerPin);
- }
- //LOOP
- void loop()
- {
- if(digitalRead(btn1) == LOW){
- playTune1();
- }
- if(digitalRead(btn2) == LOW){
- playTune2();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement