Devices & Components
Arduino Uno Rev3
Piezo buzzer
Jumper wires (generic)
Resistor 10k ohm
Pushbutton switch 12mm
Breadboard (generic)
Project description
Code
Code
c_cpp
1// defines buttonPins 2const int buttonPin2 = 2; 3const int buttonPin3 = 3; 4const int buttonPin4 = 4; 5const int buttonPin5 = 5; 6const int buttonPin6 = 6; 7const int buttonPin7 = 7; 8const int buttonPin8 = 8; 9 10const int buzzer = 13; // This is the pin connected to the buzzer 11 12// defines the states of the buttons 13int buttonState2 = 0; 14int buttonState3 = 0; 15int buttonState4 = 0; 16int buttonState5 = 0; 17int buttonState6 = 0; 18int buttonState7 = 0; 19int buttonState8 = 0; 20 21void setup() { 22 // Set buzzer-pin as output: 23 pinMode(buzzer, OUTPUT); 24 // Set button-pins as input: 25 pinMode(buttonPin2, INPUT); 26 pinMode(buttonPin3, INPUT); 27 pinMode(buttonPin4, INPUT); 28 pinMode(buttonPin5, INPUT); 29 pinMode(buttonPin6, INPUT); 30 pinMode(buttonPin7, INPUT); 31 pinMode(buttonPin8, INPUT); 32 33} 34 35void loop() { 36 // reads button value: 37 buttonState2 = digitalRead(buttonPin2); 38 buttonState3 = digitalRead(buttonPin3); 39 buttonState4 = digitalRead(buttonPin4); 40 buttonState5 = digitalRead(buttonPin5); 41 buttonState6 = digitalRead(buttonPin6); 42 buttonState7 = digitalRead(buttonPin7); 43 buttonState8 = digitalRead(buttonPin8); 44 45 // Plays a specific frequency depending on which button are pressed 46 if (buttonState2 == HIGH) { 47 tone(buzzer, 440.000); // sends a frequency of 440Hz 48 } 49 else if (buttonState3 == HIGH) { 50 tone(buzzer, 391.995); 51 } 52 else if (buttonState4 == HIGH) { 53 tone(buzzer, 349.228); 54 } 55 else if (buttonState5 == HIGH) { 56 tone(buzzer, 329.628); 57 } 58 else if (buttonState6 == HIGH) { 59 tone(buzzer, 293.665); 60 } 61 else if (buttonState7 == HIGH) { 62 tone(buzzer, 261.626); 63 } 64 else if (buttonState8 == HIGH) { 65 66 // Plays a series of tones when the last button is pressed 67 68 tone(buzzer, 329.628); 69 noTone(buzzer); 70 delay(100); 71 tone(buzzer, 329.628); 72 delay(100); 73 tone(buzzer, 329.628); 74 delay(200); 75 tone(buzzer, 261.626); 76 delay(200); 77 noTone(buzzer); 78 delay(2000); 79 tone(buzzer, 329.628); 80 delay(200); 81 tone(buzzer, 97.9989); 82 delay(100); 83 } 84 else { 85 noTone(buzzer); // stops sending frequencies to the buzzer 86 } 87 88} 89
Downloadable files
Circuit schematic
Each button is connected to a 10k ohm resistor. The buzzer is connected to input 13 and ground.
Circuit schematic

Circuit schematic
Each button is connected to a 10k ohm resistor. The buzzer is connected to input 13 and ground.
Circuit schematic

Comments
Only logged in users can leave comments