Hi, I'm pretty new to Arduino and I've been working my way through examples and tutorials to get the hang of things. I have the ARDX kit and I was working on CIRC06 which uses a piezo element to play a song. The physical circuit was simple and I thought I had a good understanding of the sketch. But when I load the sketch that I wrote, nothing happens. This is the sketch I wrote:
int speakerPin=9;
int length=15;
char notes[]="ccggaagffeeddc ";
int beats[]={1,1,1,1,1,1,2,1,1,1,1,1,1,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};
for(int i=0;i<8;i++){
if(names[i]==note){
playTone(tones[i],duration);
}
}
}
void setup() {
pinMode(speakerPin,OUTPUT);
}
void loop() {
for(int i=0;i<length;i++){
if(notes[i]=' '){
delay(beats[i]*tempo);
}
else{
playNote(notes[i],beats[i]*tempo);
}
delay(tempo/2);
}
}
So after looking over it several times, I figured I'd just try copying the example code straight from the website to make sure. I got rid of the extra spaces and comments so I could more easily compare the code from the site to the one I wrote. This is the one from the site:
int speakerPin=9;
int length=15;
char notes[]="ccggaagffeeddc ";
int beats[]={1,1,1,1,1,1,2,1,1,1,1,1,1,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};
for(int i=0;i<8;i++){
if(names[i]==note){
playTone(tones[i],duration);
}
}
}
void setup() {
pinMode(speakerPin,OUTPUT);
}
void loop() {
for(int i=0;i<length;i++){
if(notes[i]=' '){
delay(beats[i]*tempo);
}
else{
playNote(notes[i],beats[i]*tempo);
}
delay(tempo/2);
}
}
Other than the name I assigned the output pin, they're identical. But the one from the site works, and mine doesn't. I noticed that even after I modified the one from the site, it takes up 85 bytes while the one I wrote only takes up 61 bytes. I feel like that has something to do with the types being used, but like I said, I looked over both codes and I don't see any real difference. Any suggestions?