Lab 5: Auto Theremin

justinwang's picture

 

/* 
  Auto Theremin
  Created 4 October 2011
  By Justin Wang
*/
 
int potPin = 0; // input pin for photosensor
int speakerPin = 9;
 
// photosensor floor values for particular pitch
// to calibrate, change valToPitchc and valToPitchD
int valToPitchc = 340; // corresponds to open
int valToPitchd = 0;
int valToPitche = 0;
int valToPitchf = 0;
int valToPitchg = 0;
int valToPitcha = 0;
int valToPitchb = 0;
int valToPitchC = 0;
int valToPitchD = 14; // corresponds to closed
 
int tones[] = { 1911, 1703, 1517, 1432, 1276, 1136, 1012, 956, 851};
int count = 0;
 
int val = 0;
 
void setup() 
{
  pinMode(speakerPin, OUTPUT); 
  Serial.begin(9600);
  Serial.println("ready");
}
 
void loop()
{
  digitalWrite(speakerPin, LOW);
  val = analogRead(potPin); // read value from the sensor
  
  valToPitchg = (valToPitchc+valToPitchD)/2;
  valToPitche = (valToPitchc+valToPitchg)/2;
  valToPitchb = (valToPitchg+valToPitchD)/2;
  valToPitchd = (valToPitchc+valToPitche)/2;
  valToPitchf = (valToPitche+valToPitchg)/2;
  valToPitcha = (valToPitchg+valToPitchb)/2;
  valToPitchC = (valToPitchb+valToPitchD)/2;
 
  if (val > valToPitchD) count = 8;
  if (val > valToPitchC) count = 7;
  if (val > valToPitchb) count = 6;
  if (val > valToPitcha) count = 5;
  if (val > valToPitchg) count = 4;
  if (val > valToPitchf) count = 3;
  if (val > valToPitche) count = 2;
  if (val > valToPitchd) count = 1;
  if (val > valToPitchc) count = 0;
  if (val < valToPitchD) count = 0;
  
  for( int i=0; i<100; i++ ) // play for 200 cycles
  {  
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tones[count]);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tones[count]);
  }
  
  Serial.println(val);
}
photo2.JPG
photo1.JPG
0
Your rating: None