6. Learning the piezo "slide whistle" like a child learns recorder

Posted by Erich

Erich's picture

"Slide Whistle"

Using an FSR, a light sensitive resistor, a piezo speaker and a homemade slide "whistle", I relearned how frustrating it is to get the right notes to come out when you want.

Video: http://www.youtube.com/watch?v=RNeqS4zIRB4

The code breaks each section of the "whistle" into notes, ranging from low c to C. You then press on the button when you want the note to come out. The Serial monitor outputs what note you're playing.

 

 

/*   Erich Hacker
    TUI Hw5 - Piezo Speaker
     * note frequency period PW (timeHigh)
 * c        261 Hz        3830 1915
 * d        294 Hz        3400 1700
 * e        329 Hz        3038 1519
 * f        349 Hz        2864 1432
 ** g        392 Hz        2550 1275
 ** a        440 Hz        2272 1136
 ** b        493 Hz        2028 1014
 * C        523 Hz        1912 956
 
 */
 
int buttonPin = 4;
int photoPin = 5;
int speakerPin = 11;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
 
 
int val = 0;
int button = 0;
int count = 1;
 
void setup() {
  pinMode(speakerPin, OUTPUT); 
  pinMode(buttonPin, INPUT);
  pinMode(photoPin, INPUT);
  Serial.begin(9600);
}
 
void loop() {
  button = analogRead(buttonPin);
  //Serial.println(button);
  while(button > 1015){
    
    val = analogRead(photoPin);
    
    if(val<600){count=0;
    }else if(val<700){count=1;
    }else if(val<750){count=2;
    }else if(val<800){count=3;
    }else if(val<850){count=4;
    }else if(val<900){count=5;
    }else if(val<950){count=6;
    }else if(val>=950){count=7;
    }
    Serial.print("    Note=");
    Serial.println(names[count]);
    for( int i=0; i<75; i++ ) {  // play it for 50 cycles
      digitalWrite(speakerPin, LOW);
      delayMicroseconds(tones[count]);
      digitalWrite(speakerPin, HIGH);
      delayMicroseconds(tones[count]);
    }
    button = analogRead(buttonPin);
  } 
  digitalWrite(speakerPin, LOW);
}
 
Whistle circuit
Whistle
0
Your rating: None