The assignment for this week is add an output device to a microcontroller board you've designed and program it to do something. We covered examples of RGB color changing LEDs, Charlieplexing an array of LEDs, LCD displays, video, sound, DC motors, servo motors, stepper motors and more.I decided to make the speaker.
I started redesigning the speaker circuit by refering to the hello.speaker circuit using Eagle.I added a push button to control the sound.All components are placed and connections are made in schematic
Then using switch to board feature I made the board by arranging components and tracing the paths.
After drawing the inetrior I exported the board as png for milling.
Then as usual, I cut the circuit board using Fab Modules and the Modela mill and stuffed with required components.
I used example program in arduino-"toneMelody".I modified it to add a push button to control the tune.The tune notes are found online.We will need a boolean to test if the button is hit. After that we will need to define the number of the button pin and define a button state.If the button is pressed we want the boolean to change it's value so that we know that button was hit. Once it is hit we then activate the melody.Here is the program I used:
boolean buttonHit = false; // a boolean to test if the button is hit
int buttonPin = 1; // the number of the pushbutton pin
int buttonState = 0; //variable for reading the pushbutton status
#define NOTE_B0 31
#define NOTE_C4 262
#define NOTE_G3 196
#define NOTE_A3 220
#define NOTE_B3 247
int melody[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
buttonHit = true;
}
if (buttonHit == true) {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(3, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
buttonHit = false;
}
}
}
Then I soldered the speaker to a wire with 2*2 pinhead.First there was no output, then I found that there was a mistake with soldering of speaker.After resolder it and connected to battery by pressing the push button the output sound came from the speaker.
Here is a working video of it: