/* * Arduino + Sonar = Simple Theremin * * The duration of the ping until the echo arrives to * the sonar is fed directly to the DDS, thus making * a direct relation between distance and frequency. * * For details on how to use the Ping))) ultrasonic sensor * with arduino, please check: * http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor * * Joao Mouro (http://mouro.info) * 15/05/2009 */ int pingPin = 7; void setup() { initPWM(); sei(); } void loop() { uint32_t duration; // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. // We give a short LOW pulse beforehand to ensure a clean HIGH pulse. pinMode( pingPin, OUTPUT ); digitalWrite( pingPin, LOW ); delayMicroseconds( 2 ); digitalWrite( pingPin, HIGH ); delayMicroseconds( 5 ); digitalWrite( pingPin, LOW ); // The same pin is used to read the signal from the PING))): a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode( pingPin, INPUT ); duration = pulseIn( pingPin, HIGH ); setFrequency( duration ); delay(10); }