Basic Piezo Element Circuit

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?

Are you sure that you have posted the right programs ?

Other than the name I assigned the output pin, they're identical.

Program 1

int speakerPin=9;

Program 2

int speakerPin=9;

Neither of the programs will fit in 85 or 61 bytes. Where did you get that figure from ?

Fisher91:
But when I load the sketch that I wrote, nothing happens. This is the sketch I wrote:

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);
 }
}




Any suggestions?

The if(notes[i]=' ') does not do what you expected.

You probably want if(notes[i]==' '). what you have written is:

note[i]=' ';
if(' ') {
delay(yad yad)
}

Since ' ', the space character has a value of 32, and if() only cares whether it is zero or not zero. And since 32 is not zero, if() evaluates as true. It would never play a note. And since the compiler know 32 is always not zero, it drops(optimizes) the PlayTones statement out of the compiled program. That's why the two program have different compiled lengths.

Chuck.


Check out my Kickstarter Project Memory Panes an expansion RAM Shield for Mega2560's. It adds 1MB of RAM for those projects where 8KB is not enough.

I changed if(notes=' ') to if(notes==' ') and it worked perfectly. Thanks a lot for the help, that was driving me crazy.