#include #include #define DEBUG false // Set to true to enable Serial debug int TONE_VOLUME=20; // 1-20 #define TRIGGER_PIN 12 // Board pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 11 // Board pin tied to echo pin on the ultrasonic sensor. #define TRIGGER_PIN2 8 #define ECHO_PIN2 13 #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE); void setup() { if (DEBUG) { Serial.begin(115200); Serial.println("Theremino starting"); } } void loop() { delay(30); // Wait 30ms between pings (about 33 pings/sec). 29ms should be the shortest delay between pings. unsigned long uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). unsigned long uS2 = sonar2.ping(); // Send ping, get ping time in microseconds (uS). if (DEBUG){ Serial.println(uS); Serial.println(uS2); } if (uS > 3000) { // Range is about 0-30 cm from sensor toneAC(0); // Turn sound off when not in range if (DEBUG) Serial.println("No tone"); } else { int freq = 3000 - uS / 1.5; // Get sound frequency int vol= uS2/29/2; // TONE_VOLUME=map(vol, 1, 20, 1, 10); toneAC(freq ,TONE_VOLUME); // Play it! if (DEBUG) Serial.println(freq); } }