In this project, my groupmates and I created a theremin using an Arduino Uno. Using an ultrasonic sensor, we could detect distance and then output a frequency to a speaker. The C++ code is below.
void setup()
{ pinMode(4, OUTPUT); pinMode(7, INPUT); pinMode(2, OUTPUT); pinMode(3, INPUT); pinMode(5, OUTPUT); } void loop() { digitalWrite(5, HIGH); while (digitalRead(3)) { double time = 0; digitalWrite(4, HIGH); digitalWrite(4, LOW); time = pulseIn(7, HIGH, 3500); if (time != 0) { double frequency = 100 * pow(2.0, (time / 1000.0)); noTone(2); tone(2, frequency, 200); } delay(75); } |