I am a student at the University of Cincinnati. For our digital design class we are required to modify a stuffed animal to interact in some way using the Sparkfun inventors kit and Arduino. For certain reasons our group decided to scrap our first idea and go with a simpler one. We want to combine the piezo speaker circuit and press button circuit from the manual to have it play a song when the button is pressed. We chose to make a stuffed penguin and have it play "When the Saints Go Marching In" so its a play on "March of the Penguins." I have successfully altered the code for the speaker circuit to play the song but I have been having trouble combining the two codes to make them work together, I have run into many flaws in the code. It seemed rather easy to just combine the two simple circuits and code but I can't seem to get it right.
Here is the Code for the Saints song. I tested it and it plays and works perfectly
Code:
int speakerPin = 9;
int length = 16; // the number of notes
char notes[] = "cefgcefgcefgeced "; // a space represents a rest
int beats[] = { 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 1, 2, 2, 2, 2,4 };
int tempo = 300;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names == note) {
_ playTone(tones*, duration);_
_ }_
_ }_
_}_
void setup() {
_ pinMode(speakerPin, OUTPUT);_
_}_
void loop() {
_ for (int i = 0; i < length; i++) {_
_ if (notes == ' ') {_
delay(beats _ tempo); // rest_
* } else {*
playNote(notes, beats * tempo);
* }*
* // pause between notes*
* delay(tempo / 2);*
* }*
}
------------------------------------------------------------------------------
here is the original code for circuit 06 (The button pressing one) taken from the manual online
Code:
/* * * * * * *
Button by DojoDave http://www.0j0.org
*Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a pushbutton attached to pin 7. http://www.arduino.cc/en/Tutorial/Button*_
/ int ledPin = 13;
// choose the pin for the LED // choose the input pin (for a pushbutton) // variable for reading the pin status*
int inputPin int val = 0;
= 2;
void setup() pinMode(ledPin, OUTPUT); pinMode(inputPin, INPUT);
}
{
void loop(){ val = digitalRead(inputPin); if (val == HIGH) {
// declare LED as output // declare pushbutton as input
// read input value // check if the input is HIGH // turn LED OFF
// turn LED ON
digitalWrite(ledPin, LOW); } else {
digitalWrite(ledPin, HIGH);
}
}
---------------------------------------------------------------------------------------
Here is my attempt at combining the codes. It has errors and the teaching assistant and I could not figure it out. I replaced where it said ledPin with speakerPin and made sure the pin numbers the speaker and button were connected to were correct but it kept giving errors in arduino.
Code:
/* * * * * *
Button by DojoDave http://www.0j0.org
Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a pushbutton attached to pin 7. http://www.arduino.cc/en/Tutorial/Button*_
_/ int speakerPin = 9;
// choose the pin for the LED // choose the input pin (for a pushbutton) // variable for reading the pin status*
int inputPin = 2;
int val = 0;
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(inputPin, INPUT);
}
void loop(){
* val = digitalRead(inputPin);
if (val == HIGH) {
// declare LED as output // declare pushbutton as input*
// read input value // check if the input is HIGH // turn LED OFF
// turn LED ON
digitalWrite(speakerPin, LOW); } else {
digitalWrite(speakerPin, HIGH);
}
{
int length = 16; // the number of notes
char notes[] = "cefgcefgcefgeced "; // a space represents a rest
int beats[] = { 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 1, 2, 2, 2, 2,4 };
int tempo = 300;
void playTone(int tone, int duration)
for (long i = 0; i < duration * 1000L; i += tone * 2) {
* digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);*_
}
void playNote(char note, int duration) {
* char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };*
* int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };*
* // play the tone corresponding to the note name*
* for (int i = 0; i < 8; i++) {*
_ if (names == note) {
playTone(tones*, duration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
if (notes == ' ') {_
delay(beats _ tempo); // rest_
* } else {*
playNote(notes, beats * tempo);
* }*
* // pause between notes*
* delay(tempo / 2);*
* }*
}
This is my first post, hopefully i did this correctly. Any help or suggestions would be greatly appreciated if possible, i have also posted this to the sparkfun forum in hopes of answers.