Lab 5 - Potentiometers

 

ASSIGNMENT

Connect a Piezo Speaker and a Potentiometer and create and Arduino program to use the Pot value to affect the tone / rhythm of sound emitted by the Piezo speaker.

MATERIALS USED

1 Piezo Speaker, 2 Potentometers

ARDUINO CODE

 

/*
 * One pot changes the tone emitted by the Piezo speaker
 * One pot changes the rhythm (on/off rate) of the tone.
 * October 4, 2011
 */
 
int potPin_tone = 5;  // set the input pin for the potentiometer for frequency
int val_tone = 0;  // variable to store the value coming from the frequency pot
 
int potPin_delay = 2;  // set the input pin for the potentiometer for rhythm
int val_delay = 0;  // variable to store the value coming from the rhythm pot
 
int speakerPin = 6;  // set the speaker pin
 
void setup() {
  pinMode(speakerPin, OUTPUT); 
}
 
void loop() {
  
  val_tone = analogRead(potPin_tone);    // read value from the frequency pot, between 0 - 1024
  val_tone = val_tone + 950; // add 950 to pot value to bring it into the tone range
  
  if (949 < val_tone < 1000){
   val_tone = 956;
  }
  else if (999 < val_tone < 1060){
    val_tone = 1014;
  }
  else if (1059 < val_tone < 1136){
    val_tone = 1136;
  }
  else if (1135 < val_tone < 1275){
    val_tone = 1275;
  }
  else if (1274 < val_tone < 1432){
    val_tone = 1432;
  }
  else if (1431 < val_tone < 1519){
    val_tone = 1519;
  }
  else if (1518 < val_tone < 1700){
    val_tone = 1700;
  }
  else if (1699 < val_tone < 1975){
    val_tone = 1915;
  }
  else {
    val_tone = 956;
  }
   
  val_delay = analogRead(potPin_delay);  // read the value from the delay pot, between 0 - 1024
  val_delay = val_delay / 2;  //bring the delay value down to the range of 0 - 512
  
  // Set the speaker on for the val_delay number of cycles for the tone specified by the frequency pot
  while(val_delay > 0) {
    for( int i = 0; i < 20; i++ ) { 
      digitalWrite(speakerPin, HIGH);
      delayMicroseconds(val_tone);
    }
    delay(val_delay);    // wait for the delay amount    
    for( int i = 0; i < 20; i++ ) {  // play it for rhythm pot value number of cycles
      digitalWrite(speakerPin, LOW);
      delayMicroseconds(val_tone);
    }
  }
}
0
Your rating: None