Components and supplies
1
Passive Buzzer
1
Arduino UNO
Apps and platforms
1
Arduino IDE
Project description
Code
CODE
c_cpp
The code
1 2 3const int c = 261; 4const int d = 294; 5const int e = 329; 6const int f = 349; 7const int g = 391; 8const int gS = 415; 9const int a = 440; 10const int aS = 455; 11const int b = 466; 12const int cH = 523; 13const int cSH = 554; 14const int dH = 587; 15const int dSH = 622; 16const int eH = 659; 17const int fH = 698; 18const int fSH = 740; 19const int gH = 784; 20const int gSH = 830; 21const int aH = 880; 22 23const int buzzerPin = 8; 24const int ledPin1 = 12; 25const int ledPin2 = 13; 26 27int counter = 0; 28 29void setup() 30{ 31 //Setup pin modes 32 pinMode(buzzerPin, OUTPUT); 33 pinMode(ledPin1, OUTPUT); 34 pinMode(ledPin2, OUTPUT); 35} 36 37void loop() 38{ 39 40 //Play first section 41 firstSection(); 42 43 //Play second section 44 secondSection(); 45 46 //Variant 1 47 beep(f, 250); 48 beep(gS, 500); 49 beep(f, 350); 50 beep(a, 125); 51 beep(cH, 500); 52 beep(a, 375); 53 beep(cH, 125); 54 beep(eH, 650); 55 56 delay(500); 57 58 //Repeat second section 59 secondSection(); 60 61 //Variant 2 62 beep(f, 250); 63 beep(gS, 500); 64 beep(f, 375); 65 beep(cH, 125); 66 beep(a, 500); 67 beep(f, 375); 68 beep(cH, 125); 69 beep(a, 650); 70 71 delay(650); 72} 73 74void beep(int note, int duration) 75{ 76 //Play tone on buzzerPin 77 tone(buzzerPin, note, duration); 78 79 //Play different LED depending on value of 'counter' 80 if(counter % 2 == 0) 81 { 82 digitalWrite(ledPin1, HIGH); 83 delay(duration); 84 digitalWrite(ledPin1, LOW); 85 }else 86 { 87 digitalWrite(ledPin2, HIGH); 88 delay(duration); 89 digitalWrite(ledPin2, LOW); 90 } 91 92 //Stop tone on buzzerPin 93 noTone(buzzerPin); 94 95 delay(50); 96 97 //Increment counter 98 counter++; 99} 100 101void firstSection() 102{ 103 beep(a, 500); 104 beep(a, 500); 105 beep(a, 500); 106 beep(f, 350); 107 beep(cH, 150); 108 beep(a, 500); 109 beep(f, 350); 110 beep(cH, 150); 111 beep(a, 650); 112 113 delay(500); 114 115 beep(eH, 500); 116 beep(eH, 500); 117 beep(eH, 500); 118 beep(fH, 350); 119 beep(cH, 150); 120 beep(gS, 500); 121 beep(f, 350); 122 beep(cH, 150); 123 beep(a, 650); 124 125 delay(500); 126} 127 128void secondSection() 129{ 130 beep(aH, 500); 131 beep(a, 300); 132 beep(a, 150); 133 beep(aH, 500); 134 beep(gSH, 325); 135 beep(gH, 175); 136 beep(fSH, 125); 137 beep(fH, 125); 138 beep(fSH, 250); 139 140 delay(325); 141 142 beep(aS, 250); 143 beep(dSH, 500); 144 beep(dH, 325); 145 beep(cSH, 175); 146 beep(cH, 125); 147 beep(b, 125); 148 beep(cH, 250); 149 150 delay(350); 151} 152
CODE
c_cpp
The code
1 2 3const int c = 261; 4const int d = 294; 5const int e = 329; 6const int f = 349; 7const int g = 391; 8const int gS = 415; 9const int a = 440; 10const int aS = 455; 11const int b = 466; 12const int cH = 523; 13const int cSH = 554; 14const int dH = 587; 15const int dSH = 622; 16const int eH = 659; 17const int fH = 698; 18const int fSH = 740; 19const int gH = 784; 20const int gSH = 830; 21const int aH = 880; 22 23const int buzzerPin = 8; 24const int ledPin1 = 12; 25const int ledPin2 = 13; 26 27int counter = 0; 28 29void setup() 30{ 31 //Setup pin modes 32 pinMode(buzzerPin, OUTPUT); 33 pinMode(ledPin1, OUTPUT); 34 pinMode(ledPin2, OUTPUT); 35} 36 37void loop() 38{ 39 40 //Play first section 41 firstSection(); 42 43 //Play second section 44 secondSection(); 45 46 //Variant 1 47 beep(f, 250); 48 beep(gS, 500); 49 beep(f, 350); 50 beep(a, 125); 51 beep(cH, 500); 52 beep(a, 375); 53 beep(cH, 125); 54 beep(eH, 650); 55 56 delay(500); 57 58 //Repeat second section 59 secondSection(); 60 61 //Variant 2 62 beep(f, 250); 63 beep(gS, 500); 64 beep(f, 375); 65 beep(cH, 125); 66 beep(a, 500); 67 beep(f, 375); 68 beep(cH, 125); 69 beep(a, 650); 70 71 delay(650); 72} 73 74void beep(int note, int duration) 75{ 76 //Play tone on buzzerPin 77 tone(buzzerPin, note, duration); 78 79 //Play different LED depending on value of 'counter' 80 if(counter % 2 == 0) 81 { 82 digitalWrite(ledPin1, HIGH); 83 delay(duration); 84 digitalWrite(ledPin1, LOW); 85 }else 86 { 87 digitalWrite(ledPin2, HIGH); 88 delay(duration); 89 digitalWrite(ledPin2, LOW); 90 } 91 92 //Stop tone on buzzerPin 93 noTone(buzzerPin); 94 95 delay(50); 96 97 //Increment counter 98 counter++; 99} 100 101void firstSection() 102{ 103 beep(a, 500); 104 beep(a, 500); 105 beep(a, 500); 106 beep(f, 350); 107 beep(cH, 150); 108 beep(a, 500); 109 beep(f, 350); 110 beep(cH, 150); 111 beep(a, 650); 112 113 delay(500); 114 115 beep(eH, 500); 116 beep(eH, 500); 117 beep(eH, 500); 118 beep(fH, 350); 119 beep(cH, 150); 120 beep(gS, 500); 121 beep(f, 350); 122 beep(cH, 150); 123 beep(a, 650); 124 125 delay(500); 126} 127 128void secondSection() 129{ 130 beep(aH, 500); 131 beep(a, 300); 132 beep(a, 150); 133 beep(aH, 500); 134 beep(gSH, 325); 135 beep(gH, 175); 136 beep(fSH, 125); 137 beep(fH, 125); 138 beep(fSH, 250); 139 140 delay(325); 141 142 beep(aS, 250); 143 beep(dSH, 500); 144 beep(dH, 325); 145 beep(cSH, 175); 146 beep(cH, 125); 147 beep(b, 125); 148 beep(cH, 250); 149 150 delay(350); 151} 152
Downloadable files
arduino_buzzer_XjouGXL30R.png
This is the schematic.
arduino_buzzer_XjouGXL30R.png

Comments
Only logged in users can leave comments