My idea was to build an instrument that can emit at least two different sounds and play interesting tunes. Having written my program and while building my first circuit, I realised after some tests that my larger breadboard had broken connections inside and my speaker tunes were all over the place. So I changed my breadboard to the small one – thus making a REALLY tiny orchestra of just two instruments.
For this smaller version, I’m using 4 switches as piano keys to play tunes on a speaker and 2 servos with handles and larger metal plates attached to them, which act as two arms playing a cymbal. It was hard to come up with a role for the servos in this setup, but after finding several stainless steel tea and jars at home, the task was set.
Here is a schematic for the tiny orchestra:
I used 10K resistors for the switches.
And here is the code:
/* Project 4: Making a Musical Instrument A Tiny Orchestra Summary: 4 button switches are used to play tones on a speaker. Play them like on a piano keyboard, and you can compose a kiddy song of 4 notes. An additional element in this digital I/O circuit is the cymbal. The cymbal is made of 2 servo motors and handles with round metallic plates attached to their arms. When the servos sweep, their plates hit each other producing a sound resembling that of a cymbal. Use the tone function and chuck the servo position in a for loop. The tiny orchestra is ready to play. */ //import libraries #include "pitches.h" #include <Servo.h> //initialize global constants //this is the switch to sweep the servos thus playing the cymbal const int redSwitch_servoSwitch = 2; //these are the switches to control speaker sounds const int redSwitch = 3; const int yellowSwitch = 4; const int greenSwitch = 5; const int blueSwitch = 6; //initialize servos and set their original position to 0 Servo servoOne; Servo servoTwo; int pos = 0; //initialize speaker const int speaker = 10; void setup() { //set switch pins as input pins pinMode(redSwitch_servoSwitch, INPUT); pinMode(redSwitch, INPUT); pinMode(yellowSwitch, INPUT); pinMode(greenSwitch, INPUT); pinMode(blueSwitch, INPUT); //and speaker as output pin pinMode(speaker, OUTPUT); //attach servos to pins servoOne.attach(11); servoTwo.attach(12); } void loop() { //set the original state of the switches to 0 int ServoState = 0; int redState = 0; int yellowState = 0; int greenState = 0; int blueState = 0; //determine read input state for switches ServoState = digitalRead(redSwitch_servoSwitch); redState = digitalRead(redSwitch); yellowState = digitalRead(yellowSwitch); greenState = digitalRead(greenSwitch); blueState = digitalRead(blueSwitch); //control both servos with one if statement //servos sweep together to play cymbal if (ServoState == 1) { for (pos = 0; pos <= 180; pos += 1) { servoOne.write(pos); servoTwo.write(pos); delay(1); } for (pos = 180; pos >= 0; pos -= 1) { servoOne.write(pos); servoTwo.write(pos); delay(1); } } //state which button produces which sound in the speaker when pressed if (redState == 1) { tone(10, NOTE_G4, 250); } else if (yellowState == 1) { tone(10, NOTE_C4, 250); } else if (greenState == 1) { tone(10, NOTE_G3, 250); } else if (blueState == 1) { tone(10, NOTE_E3, 250); } }
The tiny orchestra worked out very well, although there was only space for one single hand to play it. It is a simple solution to the project, yet given the resources and the broken breadboard, I feel satisfied with the result. The structure of the cymbals could be improved with more durable materials that hold together tighter. It was very hard to place them in an angle relative to each other so that they could make a sound, and even in my circuit their contact could be smoother. Regarding the sounds, they could be better if played by someone experienced with a keyboard, but even under my hand’s control they played a soothing chaos. Let me know if people in the class would like to record a song at the end of the year. I think it could be fun.
Here are some videos of me playing the tiny avant garde orchestra:
Great documentation. Your schematic is very clear and your code is extremely well documented. Some minor comments:
1) Remember that the servo motor has 3 wires. Your schematic is missing the wire to 5V
2) Put inputs on the left and outputs on the right, so that information flows left to right: from sensors (switches) into Arduino and out to the actuators (speaker and servos).
3) The variable pos is global; there is no reason for this. Always try to keep variables as local as possible.
4) The variable pos is initialized when it is created, but in fact this is entirely unnecessary as it is overwritten when it is used in the for() loopswith a for() loop
5) Why are the servos moved in tiny steps? Why not go directly to 180, wait a moment, and then directly back to 0? Remember, just because the “sweep” example does that, does not mean that it’s the only way to move. Sweep does it intentionally so it moves slowly. For a cymbal, you probably want to move as quickly as possible.
6) Since your switches control the notes, you might use the note name instead of the switch color e.g. “const int G4_switch = 3”, and G4State
In spite of all those comments your code is excellent!