Last active
August 29, 2015 14:12
-
-
Save Happsson/d97bc418fd4fa3177094 to your computer and use it in GitHub Desktop.
Theremin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int trigPin = 4; | |
const int echoPin = 2; | |
void setup(){ | |
Serial.begin(9600); | |
pinMode(trigPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
} | |
void loop() | |
{ | |
int d; | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
d = pulseIn(echoPin, HIGH); | |
d = d / 3; | |
Serial.println(d); | |
if(d < 100){ | |
tones(220); //A | |
}else if(d > 101 && d < 200){ | |
tones(233); //A# | |
}else if(d > 201 && d < 300){ | |
tones(247); //B | |
}else if(d > 301 && d < 400){ | |
tones(262); //C | |
}else if(d > 401 && d < 500){ | |
tones(277); //C# | |
}else if(d > 501 && d < 600){ | |
tones(294; //D | |
}else if(d > 601 && d < 700){ | |
tones(311); //D# | |
}else if(d > 701 && d < 800){ | |
tones(330); //E | |
}else if(d > 801 && d < 900){ | |
tones(349); //F | |
}else if(d > 901 && d < 1000){ | |
tones(370); //F# | |
}else if(d > 1001 && d < 1100){ | |
tones(392); //G | |
}else if(d > 1101 && d < 1200){ | |
tones(415); //G# | |
}else if(d > 1201 && d < 1300){ | |
tones(440); //A | |
} | |
} | |
void tones(int p){ | |
tone(8, p); | |
delay(100); | |
tone(8, p*2); | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment